<commit_before>var apiai = require('apiai')
var util = require('util')
var path = require('path')
var cache = require('./cache.js').getCache()

var apiai_token = process.env.APIAI_TOKEN
var errorChannel = process.env.HUBOT_ERRORS_CHANNEL
var SCORE_THRESHOLD = 0.75

var app = apiai(apiai_token)

module.exports = robot => {

    // ask api.ai directly
    robot.respond(/ai (.*)/i, res => {
        var msg = res.match[1]
        apiaiAsk(msg, res)
    })

    robot.catchAll(function (res) {
        // var regexp = new RegExp("^(?:" + robot.alias + "|" + robot.name + ") (.*)", "i") 
        var regex = new RegExp(robot.name + " (.*)", "i")
        if (res.message.text.match(regex)) { // captures only direct messages and not messages in channels 
            var msg = res.message.text.match(regex)[1]
            if (!msg.includes('has snoozed notifications. Send one anyway?')) {
                /* this ↑↑ happens sometimes when bot auto-post (with cron-jobs) 
                 * trello/github sumups and triggers api.ai but it shouldn't.
                 * Until a better solution is found, this should work.
                 */
                apiaiAsk(msg, res)
            }
            console.log('api.ai', msg)
        }
    })


    // sending a msg to api.ai from other scripts
    robot.on('ask-api.ai', function (msg, res) {
        apiaiAsk(msg, res)
    })

    // trigger an event to api.ai from other scripts
    robot.on('event-api.ai', function (eventName, userId) {
        // console.log(eventName, userId)
        apiaiEvent(eventName)
    })


    var activeAction = {}

    function apiaiAsk(msg, res) {
        var userId = res.message.user.id

        var options = {
            sessionId: userId
        }

        var request = app.textRequest(msg, options)

        request.on('response', function (response) {
            if (response.result.score < SCORE_THRESHOLD) {
                activeAction[response.sessionId] = response.result.action
                var intentName = response.result.metadata.intentName
                res.reply(`Do you mean ${intentName} ?`)
                apiaiEvent('notSure', res)
                return
            }
            else {
                responseCallback(response, res)
            }
        })

        request.on('error', function (error) {
            robot.messageRoom(errorChannel, `Error in ${path.basename(__filename)} script. Please check the Server Log for more details.`)
            robot.logger.error(error)
        })

        request.end()
    }


    function apiaiEvent(eventName, res, data = {}) {
        var userId = res.message.user.id

        var event = {
            name: eventName,
        }

        var options = {
            sessionId: userId
        }

        var request = app.eventRequest(event, options)

        request.on('response', function (response) {
            responseCallback(response, res)
        })

        request.on('error', function (error) {
            robot.messageRoom(errorChannel, `Error in ${path.basename(__filename)} script. Please check the Server Log for more details.`)
            robot.logger.error(error)
        })

        request.end()
    }

    function notSure(action, res) {
        res.reply('Do you mean ' + action + '?')
        apiaiEvent('notSure', res, { action: action })
    }

    function responseCallback(response, res) {
        var result = response.result
        var userId = res.message.user.id
        var roomid = res.message.room

        // console.log(response)
        if (result.score < SCORE_THRESHOLD) {
            apiaiEvent('uknownInput', userId)
            return 0
        }

        // if the action is completed, emit the data 
        var isComplete = !result.actionIncomplete
        if (isComplete) {
            var handled = robot.emit(result.action, result, res)
            if (!handled) {
                robot.logger.info('No scripts handled the api.ai action: ' + result.action)
            }
        }

        // reply back to user
        if (result.fulfillment.speech) {
            robot.messageRoom(roomid, result.fulfillment.speech)
        }
    }

    robot.on('notSure.yes', function (result, res) {
        var userid = res.message.user.id
        apiaiEvent(activeAction[userid], res)
    })

}<commit_msg>update api.ai<commit_after>var apiai = require('apiai')
var path = require('path')
var cache = require('./cache.js').getCache()

if (!process.env.APIAI_TOKEN) { return }
var apiai_token = process.env.APIAI_TOKEN
var errorChannel = process.env.HUBOT_ERRORS_CHANNEL
var SCORE_THRESHOLD = 0.75

var app = apiai(apiai_token)

module.exports = robot => {

    // ask api.ai directly
    robot.respond(/ai (.*)/i, res => {
        var msg = res.match[1]
        apiaiAsk(msg, res)
    })

    robot.catchAll(function (res) {
        // var regexp = new RegExp("^(?:" + robot.alias + "|" + robot.name + ") (.*)", "i") 
        var regex = new RegExp(robot.name + " (.*)", "i")
        if (res.message.text.match(regex)) { // captures only direct messages and not messages in channels 
            var msg = res.message.text.match(regex)[1]
            var userid = res.message.user.id
            if (userid != 'USLACKBOT') {
                // avoid msgs from slackbot (i.e. furling links). 
                apiaiAsk(msg, res)
            }
            console.log('api.ai', msg)
        }
    })


    // sending a msg to api.ai from other scripts
    robot.on('ask-api.ai', function (msg, res) {
        apiaiAsk(msg, res)
    })

    // trigger an event to api.ai from other scripts
    robot.on('event-api.ai', function (eventName, userId) {
        // console.log(eventName, userId)
        apiaiEvent(eventName)
    })


    var activeAction = {}

    function apiaiAsk(msg, res) {
        var userId = res.message.user.id

        var options = {
            sessionId: userId
        }

        var request = app.textRequest(msg, options)

        request.on('response', function (response) {
            if (response.result.score < SCORE_THRESHOLD) {
                activeAction[response.sessionId] = response.result.action
                var intentName = response.result.metadata.intentName
                res.reply(`Do you mean ${intentName} ?`)
                apiaiEvent('notSure', res)
                return
            }
            else {
                responseCallback(response, res)
            }
        })

        request.on('error', function (error) {
            robot.messageRoom(errorChannel, `Error in ${path.basename(__filename)} script. Please check the Server Log for more details.`)
            robot.logger.error(error)
        })

        request.end()
    }


    function apiaiEvent(eventName, res, data = {}) {
        var userId = res.message.user.id

        var event = {
            name: eventName,
        }

        var options = {
            sessionId: userId
        }

        var request = app.eventRequest(event, options)

        request.on('response', function (response) {
            responseCallback(response, res)
        })

        request.on('error', function (error) {
            robot.messageRoom(errorChannel, `Error in ${path.basename(__filename)} script. Please check the Server Log for more details.`)
            robot.logger.error(error)
        })

        request.end()
    }

    function notSure(action, res) {
        res.reply('Do you mean ' + action + '?')
        apiaiEvent('notSure', res, { action: action })
    }

    function responseCallback(response, res) {
        var result = response.result
        var userId = res.message.user.id
        var roomid = res.message.room

        // console.log(response)
        if (result.score < SCORE_THRESHOLD) {
            apiaiEvent('uknownInput', userId)
            return 0
        }

        // if the action is completed, emit the data 
        var isComplete = !result.actionIncomplete
        if (isComplete) {
            var handled = robot.emit(result.action, result, res)
            if (!handled) {
                robot.logger.info('No scripts handled the api.ai action: ' + result.action)
            }
        }

        // reply back to user
        if (result.fulfillment.speech) {
            robot.messageRoom(roomid, result.fulfillment.speech)
        }
    }

    robot.on('notSure.yes', function (result, res) {
        var userid = res.message.user.id
        apiaiEvent(activeAction[userid], res)
    })


    function missingEnvironmentForApi() {
        var missingAnything;
        missingAnything = false;
        if (process.env.APIAI_TOKEN == null) {
            robot.logger.warning("SomeAPI Client ID is missing: Ensure that SOMEAPI_CLIENT_ID is set.");
            missingAnything |= true;
        }
        return missingAnything;
    }

}


{'fraction_non_alphanumeric': 0.11860755187663001, 'fraction_numerical': 0.0013606984918925048, 'mean_word_length': 1.8908554572271385, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3259249', 'n_tokens_mistral': 2617, 'n_tokens_neox': 2547, 'n_words': 610}