Skip to content

Commit

Permalink
Remove botkit dependency for Google Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
patrinhani-ciandt committed Dec 4, 2017
1 parent ffeba3d commit 4223b9d
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/botkit/CoreBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions lib/controllers/facebook/index.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
124 changes: 124 additions & 0 deletions lib/controllers/gactions/botkit/index.js
Original file line number Diff line number Diff line change
@@ -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;
113 changes: 113 additions & 0 deletions lib/controllers/gactions/botkit/worker.js
Original file line number Diff line number Diff line change
@@ -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;
};
7 changes: 2 additions & 5 deletions lib/controllers/gactions/index.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -27,7 +25,6 @@ module.exports = class GActionsController extends BaseController {
}

constructor(abbottCore) {
debug('Initializing controller class...');
super('gactions', abbottCore, {
__dirname: __dirname
});
Expand Down

0 comments on commit 4223b9d

Please sign in to comment.