{"size": 1359, "ext": "js", "max_stars_repo_path": "src/App.js", "max_stars_repo_name": "shinjuku-lt/timetable-bot", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/App.js", "max_issues_repo_name": "shinjuku-lt/timetable-bot", "max_issues_repo_issues_event_min_datetime": "2019-01-23T16:27:05.000Z", "max_issues_repo_issues_event_max_datetime": "2019-02-23T13:22:44.000Z", "max_forks_repo_path": "src/App.js", "max_forks_repo_name": "shinjuku-lt/timetable-bot", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 25, "max_forks_count": null, "avg_line_length": 25.1666666667, "max_line_length": 82, "alphanum_fraction": 0.5842531273}
const Botkit = require('botkit')
const Config = require('./Config')

/**
 * `BotKit` initial setting
 */
const controller = Botkit.slackbot({
    debug: false,
    json_file_store: Config.FILE_STORAGE_NAME,
})

const bot = controller
    .spawn({
        token: process.env.BOT_TOKEN,
    })
    .startRTM()

// `SlashRouter` setting
if (Config.IS_PRODUCTION) {
    controller.setupWebserver(Config.PORT, (err, webserver) => {
        controller.createWebhookEndpoints(controller.webserver)
        controller.createOauthEndpoints(controller.webserver, (err, req, res) => {
            if (err) {
                res.status(500).send('Error: ' + JSON.stringify(err))
            } else {
                res.send('Success')
            }
        })
    })

    controller.configureSlackApp({
        clientId: Config.SLACK_CLIENT_ID,
        clientSecret: Config.SLACK_CLIENT_SECRET,
        scopes: ['commands'],
    })

    // store `teamId` to BotKit.builtin file storage
    bot.api.team.info({}, (err, res) => {
        if (err) throw new Error(err.stack || JSON.stringify(err))
        // workaround: https://github.com/howdyai/botkit/issues/590
        res.team.bot = {
            id: 'boti',
            name: 'boti',
        }

        controller.saveTeam(res.team, () => {})
    })
}

module.exports = {
    controller: controller,
    bot: bot,
}
