Created
May 4, 2018 11:44
-
-
Save qdraw/7efb16e4222896b8dbd87b9065d143e4 to your computer and use it in GitHub Desktop.
Alexa Botkit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint esversion: 6 */ | |
var DEBUGKEY = "justtemp"; | |
var connectorName = "alexa"; | |
var AlexaRequest = require('alexa-req'); | |
const verifier = require('alexa-verifier'); | |
const path = require('path'); | |
var Botkit = require(path.join(__dirname, '..', 'node_modules','botkit','lib','CoreBot.js')); | |
function AlexaBot(configuration) { | |
// Create a core botkit bot | |
var controller = Botkit(configuration || {}); | |
// customize the bot definition, which will be used when new connections | |
// spawn! | |
controller.defineBot(function(botkit, config) { | |
var bot = { | |
botkit: botkit, | |
config: config || {}, | |
utterances: botkit.utterances, | |
}; | |
bot.send = function(message, cb) { | |
function done(err, res) { | |
if (cb) { | |
cb(err); | |
} | |
} | |
if (!message || !message.address) { | |
if (cb) { | |
cb(new Error('Outgoing message requires a valid address...')); | |
} | |
return; | |
} | |
// Copy message minus user & channel fields | |
var bf_message = {}; | |
for (var key in message) { | |
switch (key) { | |
case 'user': | |
case 'channel': | |
// ignore | |
break; | |
default: | |
bf_message[key] = message[key]; | |
break; | |
} | |
} | |
if (!bf_message.type) { | |
bf_message.type = 'message'; | |
} | |
// Ensure the message address has a valid conversation id. | |
if (!bf_message.address.conversation) { | |
bot.connector.startConversation(bf_message.address, function(err, adr) { | |
if (!err) { | |
// Send message through connector | |
bf_message.address = adr; | |
bot.connector.send([bf_message], done); | |
} else { | |
done(err); | |
} | |
}); | |
} else { | |
// Send message through connector | |
bot.connector.send([bf_message], done); | |
} | |
}; | |
bot.reply = function(src, resp, cb) { | |
var msg = {}; | |
if (typeof(resp) == 'string') { | |
msg.text = resp; | |
} else { | |
msg = resp; | |
} | |
msg.user = src.user; | |
msg.channel = src.channel; | |
msg.address = src.address; | |
msg.to = src.user; | |
bot.say(msg, cb); | |
}; | |
bot.findConversation = function(message, callback) { | |
botkit.debug('CUSTOM FIND CONVO', message.user, message.channel); | |
for (var t = 0; t < botkit.tasks.length; t++) { | |
for (var c = 0; c < botkit.tasks[t].convos.length; c++) { | |
if ( | |
botkit.tasks[t].convos[c].isActive() && | |
botkit.tasks[t].convos[c].source_message.user == message.user && | |
botkit.tasks[t].convos[c].source_message.channel == message.channel && | |
botkit.excludedEvents.indexOf(message.type) == -1 // this type of message should not be included | |
) { | |
botkit.debug('FOUND EXISTING CONVO!'); | |
callback(botkit.tasks[t].convos[c]); | |
return; | |
} | |
} | |
} | |
callback(); | |
}; | |
return bot; | |
}); | |
controller.middleware.ingest.use(function(bot, message, res, next) { | |
console.log("controller.middleware.ingest"); | |
// const alexa = new AlexaRequest(message.raw_message); | |
console.log(); | |
var reqbody = message.raw_message.raw_message; | |
var slots = {}; // added >> | |
if (reqbody.request !== undefined && reqbody.request.type !== "LaunchRequest" ) { | |
if ( reqbody.request.intent !== undefined ) { | |
slots = reqbody.request.intent.slots; | |
} | |
} | |
var accessToken = ""; | |
if (reqbody.session !== undefined) { | |
if (reqbody.session.user.accessToken !== undefined) { | |
accessToken = reqbody.session.user.accessToken | |
} | |
} | |
console.log("accessToken done"); | |
message = { | |
text: reqbody.request.type, | |
// intent: alexa.getIntentName(), //added | |
user: reqbody.session.user.userId, | |
channel: reqbody.session.user.userId, | |
timestamp: new Date(), | |
response: res, | |
// alexa: alexa, | |
accessToken: accessToken, // added | |
// slots: slots, // added | |
}; | |
// add slot values to message.text | |
if (slots !== undefined) { | |
if ( JSON.stringify(slots) !== "{}") { | |
message.text += " " + message.slots[Object.keys(message.slots)[0]].value; | |
} | |
} | |
next(); | |
}); | |
controller.middleware.normalize.use(function(bot, message, next) { | |
message.user = "hi"; | |
message.channel = "dsfklsnfd"; | |
message.text = "dsfsd"; | |
console.log("norm"); | |
next(); | |
}); | |
controller.middleware.categorize.use(function(bot, message, next) { | |
console.log("categorize"); | |
// messages in Slack that are sent in a 1:1 channel | |
// can be identified by the first letter of the channel ID | |
// if it is "D", this is a direct_message! | |
if (message.type == 'message_received') { | |
if (message.channel[0] == 'D') { | |
message.type = 'direct_message'; | |
} | |
} | |
// call next to proceed | |
next(); | |
}); | |
controller.middleware.receive.use(function(bot, message, next) { | |
console.log("receive"); | |
console.log(message); | |
next(); | |
}); | |
controller.middleware.format.use(function(bot, message, platform_message, next) { | |
// clone the incoming message | |
for (var k in message) { | |
platform_message[k] = message[k]; | |
} | |
console.log("controller.middleware.format"); | |
next(); | |
}); | |
// set up a web route for receiving outgoing webhooks and/or slash commands | |
controller.createWebhookEndpoints = function(webserver, bot, cb) { | |
var endpoint = '/'+connectorName+'/receive'; | |
// Listen for incoming events | |
controller.log( | |
'** Serving webhook endpoints for the '+ connectorName +' at: ' + | |
'http://' + controller.config.hostname + ':' + | |
controller.config.port + endpoint); | |
webserver.post(endpoint, function(req, res) { | |
console.log('GOT A MESSAGE HOOK'); | |
var signature = req.headers.signature; | |
var cert_url = req.headers.signaturecertchainurl; | |
var incomingMessageObject = { | |
'raw_message': req.body, | |
} | |
// Do some preflight checks | |
if (signature !== DEBUGKEY && cert_url !== DEBUGKEY) { | |
verifier(cert_url, signature, req.rawBody, function(err) { | |
if (err) { | |
console.error('error validating the alexa cert:', err); | |
// todo: this fails: timeout server error | |
res.status(401).json({ status: 'failure', reason: err }); | |
} | |
if (!err) { | |
controller.ingest(bot, incomingMessageObject, res); | |
} | |
}); | |
} | |
if (signature === DEBUGKEY && cert_url === DEBUGKEY) { | |
// console.log("incomingMessageObject.raw_message"); | |
// console.log(incomingMessageObject.raw_message); | |
controller.ingest(bot, incomingMessageObject, res); | |
} | |
}); | |
if (cb) { | |
cb(); | |
} | |
controller.startTicking(); | |
return controller; | |
}; | |
return controller; | |
} | |
module.exports = AlexaBot; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Alexa = require('./bot-alexa.js'); | |
Alexa.controller.createWebhookEndpoints(app, Alexa.bot,function() { | |
console.log('This bot is online!!!'); | |
}); | |
Alexa.controller.hears('(.*)', 'message_received', function(bot, message) { | |
console.log("dsfdf"); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint esversion: 6 */ | |
var alexa = require('./alexa-botkit.js'); | |
var AlexaResponse = require('alexa-response'); | |
const controller = alexa({ | |
debug: true | |
}); | |
const bot = controller.spawn({}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Initializing Botkit v0.6.14
GOT A MESSAGE HOOK
controller.middleware.ingest
accessToken done
norm
categorize
receive
{ raw_message:
{ raw_message:
{ version: '1.0',
session: [Object],
context: [Object],
request: [Object] } },
_pipeline: { stage: 'receive' },
user: 'hi',
channel: 'dsfklsnfd',
text: 'dsfsd',
type: 'message_received' }
debug: RECEIVED MESSAGE
debug: CUSTOM FIND CONVO hi dsfklsnfd