From 4223b9dc5d8e7d9c0f3ca4460e729180de844f53 Mon Sep 17 00:00:00 2001 From: Vinicius Patrinhani Date: Mon, 4 Dec 2017 18:20:12 -0200 Subject: [PATCH] Remove botkit dependency for Google Actions --- lib/botkit/CoreBot.js | 3 +- lib/controllers/facebook/index.js | 7 +- lib/controllers/gactions/botkit/index.js | 124 ++++++++++++++++++++++ lib/controllers/gactions/botkit/worker.js | 113 ++++++++++++++++++++ lib/controllers/gactions/index.js | 7 +- 5 files changed, 244 insertions(+), 10 deletions(-) create mode 100644 lib/controllers/gactions/botkit/index.js create mode 100644 lib/controllers/gactions/botkit/worker.js diff --git a/lib/botkit/CoreBot.js b/lib/botkit/CoreBot.js index ab6a03d..a0b1610 100644 --- a/lib/botkit/CoreBot.js +++ b/lib/botkit/CoreBot.js @@ -753,7 +753,8 @@ function Botkit(configuration) { // is there any text? // or an attachment? (facebook) // or multiple attachments (slack) - if (message.text || message.attachments || message.attachment) { + // if (message.text || message.attachments || message.attachment) { + if (message) { var outbound = this.cloneMessage(message); var that = this; diff --git a/lib/controllers/facebook/index.js b/lib/controllers/facebook/index.js index 5735197..3797850 100644 --- a/lib/controllers/facebook/index.js +++ b/lib/controllers/facebook/index.js @@ -1,7 +1,6 @@ -var BotkitFacebook = require(__dirname + '/botkit'); -var path = require('path'); - -var BaseController = require('../base-controler'); +const BotkitFacebook = require(__dirname + '/botkit'); +const path = require('path'); +const BaseController = require('../base-controler'); module.exports = class FacebookController extends BaseController { get botkitType() { diff --git a/lib/controllers/gactions/botkit/index.js b/lib/controllers/gactions/botkit/index.js new file mode 100644 index 0000000..e9317d7 --- /dev/null +++ b/lib/controllers/gactions/botkit/index.js @@ -0,0 +1,124 @@ +const Botkit = require('../../../botkit/CoreBot'); +const logging = require('../../../logging'); +const request = require('request'); +const express = require('express'); +const bodyParser = require('body-parser'); +const querystring = require('querystring'); +const async = require('async'); + +function GActionsBot(configuration) { + + var ActionsSDKApp = require('actions-on-google').ActionsSdkApp; + + configuration = configuration || {}; + + configuration.logger = logging('abbott-framework:botkit:gactions'); + + // Create a core botkit bot + var gactions_botkit = Botkit(configuration || {}); + + // Set some default configurations unless they've already been set. + + var spawned_bots = []; + + // customize the bot definition, which will be used when new connections + // spawn! + gactions_botkit.defineBot(require(__dirname + '/worker.js')); + + // Middleware to track spawned bots and connect existing RTM bots to incoming webhooks + gactions_botkit.middleware.spawn.use(function (worker, next) { + + // lets first check and make sure we don't already have a bot + // for this team! If we already have an RTM connection, copy it + // into the new bot so it can be used for replies. + + var existing_bot = null; + if (worker.config.id) { + for (var b = 0; b < spawned_bots.length; b++) { + if (spawned_bots[b].config.id) { + if (spawned_bots[b].config.id == worker.config.id) { + // WAIT! We already have a bot spawned here. + // so instead of using the new one, use the exist one. + existing_bot = spawned_bots[b]; + } + } + } + } + + if (!existing_bot && worker.config.id) { + spawned_bots.push(worker); + } + next(); + + }); + + gactions_botkit.middleware.format.use(function (bot, message, platform_message, next) { + if (message.text) { + platform_message.text = message.text; + } + + if (message.richResponse) { + platform_message.richResponse = message.richResponse; + } + + if (message.carousel) { + platform_message.carousel = message.carousel; + } + + if (message.list) { + platform_message.list = message.list; + } + + platform_message.endConversation = message.endConversation || false; + platform_message.expectUserResponse = message.expectUserResponse || false; + + next(); + }); + + // set up a web route for receiving outgoing webhooks and/or commands + gactions_botkit.createWebhookEndpoints = function (webserver, authenticationTokens) { + + if (authenticationTokens !== undefined && arguments.length > 1 && arguments[1].length) { + secureWebhookEndpoints.apply(null, arguments); + } + + gactions_botkit.logger.info( + '** Serving webhook endpoints for commands and outgoing ' + + 'webhooks at: http://' + gactions_botkit.config.hostname + ':' + gactions_botkit.config.port + '/gactions/receive'); + webserver.post('/gactions/receive', function (req, res) { + // Now, pass the webhook into be processed + const assistant = new ActionsSDKApp({ request: req, response: res }); + + assistant.handleRequest(function (app) { + gactions_botkit.handleWebhookPayload(req, res, app); + }); + }); + + return gactions_botkit; + }; + + gactions_botkit.handleWebhookPayload = function (req, res, gactionsApp) { + let intent = gactionsApp.getIntent(); + + if (intent) { + var payload = { + type: intent, + text: gactionsApp.getRawInput(), + user: gactionsApp.getUser().userId, + channel: gactionsApp.getConversationId() + }; + + let bot = gactions_botkit.spawn({ + gactionsApp: gactionsApp + }); + + // Receive messages and trigger events from the Events API + // gactions_botkit.receiveMessage(bot, payload); + gactions_botkit.ingest(bot, payload, res); + } + }; + + return gactions_botkit; +} + +module.exports = GActionsBot; diff --git a/lib/controllers/gactions/botkit/worker.js b/lib/controllers/gactions/botkit/worker.js new file mode 100644 index 0000000..12139b5 --- /dev/null +++ b/lib/controllers/gactions/botkit/worker.js @@ -0,0 +1,113 @@ +const request = require('request'); + +module.exports = function (botkit, config) { + var bot = { + type: 'google', + botkit: botkit, + config: config || { + gactionsApp: null + }, + utterances: botkit.utterances, + identity: { // default identity values + id: null, + name: '', + } + }; + + bot.googleApp = bot.config.gactionsApp || null; + + // Set when destroy() is called - prevents a reconnect from completing + // if it was fired off prior to destroy being called + var destroyed = false; + + /** + * Shutdown and cleanup the spawned worker + */ + bot.destroy = function () { + // this prevents a startRTM from completing if it was fired off + // prior to destroy being called + destroyed = true; + + botkit.shutdown(); + }; + + bot.send = function (message, cb) { + var msgArgs = []; + + /* + * types: + * simple + * list + * carousel + */ + var messagetype = 'simple'; + + if (message.text) { + msgArgs.push(message.text); + } else { + if (message.richResponse) { + msgArgs.push(message.richResponse); + } + + if (message.carousel) { + msgArgs.push(message.carousel); + messagetype = 'carousel'; + } else if (message.list) { + msgArgs.push(message.list); + messagetype = 'list'; + } + } + + let fnResponse = null; + + if ((message.endConversation) && (!message.expectUserResponse)) { + fnResponse = bot.googleApp.tell; + + if (messagetype === 'list') { + fnResponse = bot.googleApp.tellWithList; + } else if (messagetype === 'carousel') { + fnResponse = bot.googleApp.tellWithCarousel; + } + } else { + fnResponse = bot.googleApp.ask; + + if (messagetype === 'list') { + fnResponse = bot.googleApp.askWithList; + } else if (messagetype === 'carousel') { + fnResponse = bot.googleApp.askWithCarousel; + } + } + + botkit.logger.debug('SAY', msgArgs); + fnResponse.apply(bot.googleApp, msgArgs); + if (cb) cb(); + }; + + bot.reply = function (src, resp, cb) { + var msg = {}; + + if (typeof (resp) == 'string') { + msg.text = resp; + } else { + msg = resp; + } + + msg.channel = src.channel; + + bot.say(msg, cb); + }; + + bot.startConversation = function (message, cb) { + botkit.startConversation(this, message, cb); + }; + + bot.createConversation = function (message, cb) { + botkit.createConversation(this, message, cb); + }; + + bot.findConversation = function (message, cb) { + cb(); + }; + + return bot; +}; diff --git a/lib/controllers/gactions/index.js b/lib/controllers/gactions/index.js index c7b6427..dd561ac 100644 --- a/lib/controllers/gactions/index.js +++ b/lib/controllers/gactions/index.js @@ -1,7 +1,5 @@ -var debug = require('debug')('abbott-framework:gactions'); - -var BotkitGActionsBot = require('@abbott-platform/botkit/lib/GActionsBot'); -var BaseController = require('../base-controler'); +const BotkitGActionsBot = require(__dirname + '/botkit'); +const BaseController = require('../base-controler'); module.exports = class GActionsController extends BaseController { get botkitType() { @@ -27,7 +25,6 @@ module.exports = class GActionsController extends BaseController { } constructor(abbottCore) { - debug('Initializing controller class...'); super('gactions', abbottCore, { __dirname: __dirname });