Skip to content

Commit

Permalink
Merge branch 'release/0.1.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
webus committed Dec 11, 2017
2 parents 7f7c104 + 8452a83 commit cca7295
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 37 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "botstack",
"version": "0.1.4",
"version": "0.1.5",
"description": "Bot Stack",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -48,7 +48,6 @@
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^6.9.0",
"locus": "^2.0.1",
"mocha": "^3.4.2",
"nyc": "^11.0.3"
}
Expand Down
86 changes: 73 additions & 13 deletions src/botstack.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const lodash = require('lodash');
const restify = require('restify');
const rp = require('request-promise');

Expand Down Expand Up @@ -158,7 +158,7 @@ class BotStack {
return async function (req, res, next) { // eslint-disable-line func-names, no-unused-vars
res.end();
/* eslint-disable no-restricted-syntax, no-continue, no-await-in-loop */
for (const msg of lodash.get(req.body, 'messages', [])) {
for (const msg of _.get(req.body, 'messages', [])) {
// message schema
// https://docs.smooch.io/rest/?javascript#schema44
if (msg.role !== 'appUser') {
Expand All @@ -168,8 +168,8 @@ class BotStack {
module: 'botstack:smoochWebhook',
message: msg
});
const text = lodash.get(msg, 'text');
const authorID = lodash.get(msg, 'authorId');
const text = _.get(msg, 'text');
const authorID = _.get(msg, 'authorId');
let apiAiResponse = null;
let result = null;
try {
Expand Down Expand Up @@ -224,12 +224,20 @@ class BotStack {
return async function (req, res, next) { // eslint-disable-line no-unused-vars
res.end();
await self._syncFbMessageToBackChat(req); // eslint-disable-line no-underscore-dangle
const entries = req.body.entry;
const entries = _.get(req, 'body.entry', []);
for (const entry of entries) {
const messages = entry.messaging;
const messages = _.get(entry, 'messaging', []);
for (const message of messages) {
const senderID = message.sender.id;
const isEcho = !!lodash.get(message, 'message.is_echo');
// The sender object is not included for messaging_optins events triggered by the checkbox plugin.
// https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messaging_optins
let isMessagingOptins = false;
let recipientUserRef = null;
const senderID = _.get(message, 'sender.id');
if ((!senderID) && (_.has(message, 'optin.user_ref'))) {
isMessagingOptins = true;
recipientUserRef = _.get(message, 'optin.user_ref');
}
const isEcho = !!_.get(message, 'message.is_echo');
if (isEcho) {
continue; // eslint-disable-line no-continue
}
Expand All @@ -239,17 +247,18 @@ class BotStack {
});
const isNewSession = await sessionStore.checkExists(senderID);
const isPostbackMessage = !!message.postback;
const isQuickReplyPayload = !!lodash.get(message, 'message.quick_reply.payload');
const isTextMessage = !!(!isQuickReplyPayload && lodash.get(message, 'message.text'));
const isGeoLocationMessage = !!lodash.get(message, 'message.attachments[0].payload.coordinates');
const isQuickReplyPayload = !!_.get(message, 'message.quick_reply.payload');
const isTextMessage = !!(!isQuickReplyPayload && _.get(message, 'message.text'));
const isGeoLocationMessage = !!_.get(message, 'message.attachments[0].payload.coordinates');
log.debug('Detect kind of message', {
module: 'botstack:webhookPost',
senderID,
isNewSession,
isPostbackMessage,
isQuickReplyPayload,
isTextMessage,
isGeoLocationMessage
isGeoLocationMessage,
isMessagingOptins
});
await sessionStore.set(senderID);
if (isQuickReplyPayload) {
Expand All @@ -268,6 +277,8 @@ class BotStack {
} else {
await self.postbackMessage(message, senderID);
}
} else if (isMessagingOptins) {
await self.messagingOptins(message, recipientUserRef);
} else {
await self.fallback(message, senderID);
}
Expand Down Expand Up @@ -346,7 +357,7 @@ class BotStack {
}

async quickReplyPayload(message, senderID) {
const text = lodash.get(message, 'message.quick_reply.payload');
const text = _.get(message, 'message.quick_reply.payload');
this.log.debug('Process quick reply payload', {
module: 'botstack: quickReplyPayload',
senderId: senderID,
Expand Down Expand Up @@ -389,6 +400,55 @@ class BotStack {
});
}

async messagingOptins(message, recipientUserRef) {
// on first request we have:
// {
// "recipient":{
// "id":"<PAGE_ID>"
// },
// "timestamp":<UNIX_TIMESTAMP>,
// "optin":{
// "ref":"<PASS_THROUGH_PARAM>",
// "user_ref":"<UNIQUE_REF_PARAM>"
// }
// }

// next send message to user using user_ref param
// curl -X POST -H "Content-Type: application/json" -d '{
// "recipient": {
// "user_ref":"<UNIQUE_REF_PARAM>"
// },
// "message": {
// "text":"hello, world!"
// }
// }' "https://graph.facebook.com/v2.6/me/messages?access_token=<PAGE_ACCESS_TOKEN>"

// after got result:
// {
// "message_id": "mid.1456970487936:c34767dfe57ee6e339"
// }

// let's save this user_ref for future use...
//
this.log.debug('Process message opt-in payload', {
module: 'botstack:messagingOptins',
recipientUserRef
});
if (BotStackCheck('messagingOptins')) {
BotStackEvents.emit('messagingOptins', {
recipientUserRef,
message
});
return;
}
this.log.debug('Sending to Dialogflow', {
module: 'botstack:messagingOptins',
recipientUserRef,
message
});
await fb.reply(fb.textMessage('Hello!'), recipientUserRef, { params: { use_user_ref: true }});
}

startServer() {
const port = process.env.PORT || 1337;
const self = this;
Expand Down
45 changes: 28 additions & 17 deletions src/fb/reply.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
const _ = require('lodash');
const rp = require('request-promise');
const log = require('../log');

async function reply(message, senderId) {
async function reply(message, senderId, {messagingType='RESPONSE', params=null}={}) {
const logData = {};
const sendData = {
messaging_type: messagingType,
message
};

logData['module'] = 'botstack:fb';
logData['message'] = message;

if (_.get(params, 'use_user_ref') === true) {
logData['recipientUserRef'] = senderId;
sendData['recipient'] = {
user_ref: senderId
};
} else {
logData['senderId'] = senderId;
sendData['recipient'] = {
id: senderId
};
}

if (message == null) {
log.debug('This message ignored to send', {
module: 'botstack:fb',
senderId,
message
});
log.debug('This message ignored to send', logData);
return null;
} else {
log.debug('Sending message', logData);
}
log.debug('Sending message', {
module: 'botstack:fb',
senderId,
message
});

const reqData = {
url: 'https://graph.facebook.com/v2.9/me/messages',
qs: {
access_token: process.env.FB_PAGE_ACCESS_TOKEN
},
method: 'POST',
json: {
recipient: {
id: senderId
},
message
},
json: sendData,
resolveWithFullResponse: true
};
const res = await rp(reqData);
Expand Down
5 changes: 0 additions & 5 deletions src/webhook_post.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ const dashbot = require('dashbot')(process.env.DASHBOT_API_KEY).facebook;
const sessionStore = require('./session.js')();
const log = require('./log.js');

function processWelcomeMessage(messageText, senderID) {

}

module.exports = (req, res, next) => {
console.log(' ');
console.log('===Received a message from FB');
Expand Down Expand Up @@ -53,4 +49,3 @@ module.exports = (req, res, next) => {
console.log('===');
return next();
};

0 comments on commit cca7295

Please sign in to comment.