From f57fee27253e15a447b481589343dd3374bc924e Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Tue, 23 Aug 2016 16:11:57 +0700 Subject: [PATCH 01/25] Update index.js --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index b78f342b..be180baa 100644 --- a/index.js +++ b/index.js @@ -51,8 +51,8 @@ app.post('/webhook/', function (req, res) { // recommended to inject access tokens as environmental variables, e.g. -// const token = process.env.PAGE_ACCESS_TOKEN -const token = "" + const token = process.env.PAGE_ACCESS_TOKEN +//const token = "" function sendTextMessage(sender, text) { let messageData = { text:text } From 34877f1a124df5233807a635e68a475ed292175d Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Tue, 23 Aug 2016 16:14:33 +0700 Subject: [PATCH 02/25] Update index.js --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index be180baa..0125cdc6 100644 --- a/index.js +++ b/index.js @@ -51,8 +51,8 @@ app.post('/webhook/', function (req, res) { // recommended to inject access tokens as environmental variables, e.g. - const token = process.env.PAGE_ACCESS_TOKEN -//const token = "" +// const token = process.env.PAGE_ACCESS_TOKEN +const token = "EAAR8dpi5Ae4BANlcMZB1rK2zgS0pUDwDVZCgXA64T389NQl2ycT8KZBniXGgebFBq9N3honekW6kIzbWix4NX1pWLDeykpaDcs7AUYI6B4ZBWJkFfg83lFpmIXhBADWXhatEq9ZAXT61dnM2J7YVmvT4efglZCqFXOS5zT9ctpSgZDZD" function sendTextMessage(sender, text) { let messageData = { text:text } From 78a376a26420273ff07b6afb66cff78c28f8c129 Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Tue, 23 Aug 2016 16:24:21 +0700 Subject: [PATCH 03/25] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 0125cdc6..cc49846a 100644 --- a/index.js +++ b/index.js @@ -82,7 +82,7 @@ function sendGenericMessage(sender) { "template_type": "generic", "elements": [{ "title": "First card", - "subtitle": "Element #1 of an hscroll", + "subtitle": "Element #1 of an hscroll

Element #1 of an hscroll", "image_url": "http://messengerdemo.parseapp.com/img/rift.png", "buttons": [{ "type": "web_url", From a10ff3a78284c2ff9799d2107b384723451c996f Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Tue, 23 Aug 2016 16:57:13 +0700 Subject: [PATCH 04/25] Add files via upload --- bot.js | 141 ++++++++++++++++++++++++++++ const.js | 22 +++++ facebook.js | 63 +++++++++++++ index.js | 263 ++++++++++++++++++++++++++-------------------------- 4 files changed, 360 insertions(+), 129 deletions(-) create mode 100644 bot.js create mode 100644 const.js create mode 100644 facebook.js diff --git a/bot.js b/bot.js new file mode 100644 index 00000000..8e1ab243 --- /dev/null +++ b/bot.js @@ -0,0 +1,141 @@ +'use strict'; + +// Weather Example +// See https://wit.ai/sungkim/weather/stories and https://wit.ai/docs/quickstart +const Wit = require('node-wit').Wit; +const FB = require('./facebook.js'); +const Config = require('./const.js'); + +const firstEntityValue = (entities, entity) => { + const val = entities && entities[entity] && + Array.isArray(entities[entity]) && + entities[entity].length > 0 && + entities[entity][0].value; + if (!val) { + return null; + } + return typeof val === 'object' ? val.value : val; +}; + +// Bot actions +const actions = { + say(sessionId, context, message, cb) { + console.log(message); + console.log(sendGenericMessage(1234)); //test GenericMessage + // Bot testing mode, run cb() and return + if (require.main === module) { + cb(); + return; + } + + // Our bot has something to say! + // Let's retrieve the Facebook user whose session belongs to from context + // TODO: need to get Facebook user name + const recipientId = context._fbid_; + if (recipientId) { + // Yay, we found our recipient! + // Let's forward our bot response to her. + FB.fbMessage(recipientId, message, (err, data) => { + if (err) { + console.log( + 'Oops! An error occurred while forwarding the response to', + recipientId, + ':', + err + ); + } + // Let's give the wheel back to our bot + cb(); + }); + } else { + console.log('Oops! Couldn\'t find user in context:', context); + // Giving the wheel back to our bot + cb(); + } + }, + merge(sessionId, context, entities, message, cb) { + // Retrieve the location entity and store it into a context field + const loc = firstEntityValue(entities, 'location'); + if (loc) { + context.loc = loc; // store it in context + } + + cb(context); + }, + + error(sessionId, context, error) { + console.log(error.message); + }, + + // fetch-weather bot executes + ['fetch-weather'](sessionId, context, cb) { + // Here should go the api call, e.g.: + // context.forecast = apiCall(context.loc) + context.forecast = 'sunny'; + cb(context); + }, +}; +function sendGenericMessage(sender) { + let messageData = { + "attachment": { + "type": "template", + "payload": { + "template_type": "generic", + "elements": [{ + "title": "First card", + "subtitle": "Element #1 of an hscroll", + "image_url": "http://messengerdemo.parseapp.com/img/rift.png", + "buttons": [{ + "type": "web_url", + "url": "https://www.messenger.com", + "title": "web url" + }, { + "type": "postback", + "title": "Postback", + "payload": "Payload for first element in a generic bubble", + }], + }, { + "title": "Second card", + "subtitle": "Element #2 of an hscroll", + "image_url": "http://messengerdemo.parseapp.com/img/gearvr.png", + "buttons": [{ + "type": "postback", + "title": "Postback", + "payload": "Payload for second element in a generic bubble", + }], + }] + } + } + } + request({ + url: 'https://graph.facebook.com/v2.6/me/messages', + qs: {access_token:token}, + method: 'POST', + json: { + recipient: {id:sender}, + message: messageData, + } + }, function(error, response, body) { + if (error) { + console.log('Error sending messages: ', error) + } else if (response.body.error) { + console.log('Error: ', response.body.error) + } + }) + +} + +const getWit = () => { + return new Wit(Config.WIT_TOKEN, actions); +}; + +exports.getWit = getWit; + +// bot testing mode +// http://stackoverflow.com/questions/6398196 +if (require.main === module) { + console.log("Bot testing mode."); + + const client = getWit(); + client.interactive(); +} \ No newline at end of file diff --git a/const.js b/const.js new file mode 100644 index 00000000..d3a2a94b --- /dev/null +++ b/const.js @@ -0,0 +1,22 @@ +'use strict'; + +// Wit.ai parameters +const WIT_TOKEN = process.env.WIT_TOKEN; +//const WIT_TOKEN = "YVTD4SSYSXSSYNHGY3TZOG6PTQMP7UWF"; +if (!WIT_TOKEN) { + throw new Error('missing WIT_TOKEN'); +} + +// Messenger API parameters +const FB_PAGE_TOKEN = process.env.FB_PAGE_TOKEN; + +var FB_VERIFY_TOKEN = process.env.FB_VERIFY_TOKEN; +if (!FB_VERIFY_TOKEN) { + FB_VERIFY_TOKEN = "just_do_it"; +} + +module.exports = { + WIT_TOKEN: WIT_TOKEN, + FB_PAGE_TOKEN: FB_PAGE_TOKEN, + FB_VERIFY_TOKEN: FB_VERIFY_TOKEN, +}; \ No newline at end of file diff --git a/facebook.js b/facebook.js new file mode 100644 index 00000000..ec14b52d --- /dev/null +++ b/facebook.js @@ -0,0 +1,63 @@ +'use strict'; + +// See the Send API reference +// https://developers.facebook.com/docs/messenger-platform/send-api-reference +const request = require('request'); +const Config = require('./const.js'); + +const fbReq = request.defaults({ + uri: 'https://graph.facebook.com/me/messages', + method: 'POST', + json: true, + qs: { + access_token: Config.FB_PAGE_TOKEN + }, + headers: { + 'Content-Type': 'application/json' + }, +}); + + +const fbMessage = (recipientId, msg, cb) => { + const opts = { + form: { + recipient: { + id: recipientId, + }, + message: { + text: msg, + }, + }, + }; + + fbReq(opts, (err, resp, data) => { + if (cb) { + cb(err || data.error && data.error.message, data); + } + }); +}; + + +// See the Webhook reference +// https://developers.facebook.com/docs/messenger-platform/webhook-reference +const getFirstMessagingEntry = (body) => { + const val = body.object === 'page' && + body.entry && + Array.isArray(body.entry) && + body.entry.length > 0 && + body.entry[0] && + body.entry[0].messaging && + Array.isArray(body.entry[0].messaging) && + body.entry[0].messaging.length > 0 && + body.entry[0].messaging[0]; + + return val || null; +}; + + + +module.exports = { + getFirstMessagingEntry: getFirstMessagingEntry, + fbMessage: fbMessage, + fbReq: fbReq +}; \ No newline at end of file diff --git a/index.js b/index.js index cc49846a..db5445cd 100644 --- a/index.js +++ b/index.js @@ -1,129 +1,134 @@ -'use strict' - -const express = require('express') -const bodyParser = require('body-parser') -const request = require('request') -const app = express() - -app.set('port', (process.env.PORT || 5000)) - -// parse application/x-www-form-urlencoded -app.use(bodyParser.urlencoded({extended: false})) - -// parse application/json -app.use(bodyParser.json()) - -// index -app.get('/', function (req, res) { - res.send('hello world i am a secret bot') -}) - -// for facebook verification -app.get('/webhook/', function (req, res) { - if (req.query['hub.verify_token'] === 'my_voice_is_my_password_verify_me') { - res.send(req.query['hub.challenge']) - } - res.send('Error, wrong token') -}) - -// to post data -app.post('/webhook/', function (req, res) { - let messaging_events = req.body.entry[0].messaging - for (let i = 0; i < messaging_events.length; i++) { - let event = req.body.entry[0].messaging[i] - let sender = event.sender.id - if (event.message && event.message.text) { - let text = event.message.text - if (text === 'Generic') { - sendGenericMessage(sender) - continue - } - sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) - } - if (event.postback) { - let text = JSON.stringify(event.postback) - sendTextMessage(sender, "Postback received: "+text.substring(0, 200), token) - continue - } - } - res.sendStatus(200) -}) - - -// recommended to inject access tokens as environmental variables, e.g. -// const token = process.env.PAGE_ACCESS_TOKEN -const token = "EAAR8dpi5Ae4BANlcMZB1rK2zgS0pUDwDVZCgXA64T389NQl2ycT8KZBniXGgebFBq9N3honekW6kIzbWix4NX1pWLDeykpaDcs7AUYI6B4ZBWJkFfg83lFpmIXhBADWXhatEq9ZAXT61dnM2J7YVmvT4efglZCqFXOS5zT9ctpSgZDZD" - -function sendTextMessage(sender, text) { - let messageData = { text:text } - - request({ - url: 'https://graph.facebook.com/v2.6/me/messages', - qs: {access_token:token}, - method: 'POST', - json: { - recipient: {id:sender}, - message: messageData, - } - }, function(error, response, body) { - if (error) { - console.log('Error sending messages: ', error) - } else if (response.body.error) { - console.log('Error: ', response.body.error) - } - }) -} - -function sendGenericMessage(sender) { - let messageData = { - "attachment": { - "type": "template", - "payload": { - "template_type": "generic", - "elements": [{ - "title": "First card", - "subtitle": "Element #1 of an hscroll

Element #1 of an hscroll", - "image_url": "http://messengerdemo.parseapp.com/img/rift.png", - "buttons": [{ - "type": "web_url", - "url": "https://www.messenger.com", - "title": "web url" - }, { - "type": "postback", - "title": "Postback", - "payload": "Payload for first element in a generic bubble", - }], - }, { - "title": "Second card", - "subtitle": "Element #2 of an hscroll", - "image_url": "http://messengerdemo.parseapp.com/img/gearvr.png", - "buttons": [{ - "type": "postback", - "title": "Postback", - "payload": "Payload for second element in a generic bubble", - }], - }] - } - } - } - request({ - url: 'https://graph.facebook.com/v2.6/me/messages', - qs: {access_token:token}, - method: 'POST', - json: { - recipient: {id:sender}, - message: messageData, - } - }, function(error, response, body) { - if (error) { - console.log('Error sending messages: ', error) - } else if (response.body.error) { - console.log('Error: ', response.body.error) - } - }) -} - -// spin spin sugar -app.listen(app.get('port'), function() { - console.log('running on port', app.get('port')) -}) +'use strict' + +const express = require('express') +const bodyParser = require('body-parser') +const request = require('request') +const app = express() +//const bot = require('node-wit').Wit +const bot = require('./bot.js') + +// Setting up our bot +const wit = bot.getWit() + +app.set('port', (process.env.PORT || 5000)) + +// parse application/x-www-form-urlencoded +app.use(bodyParser.urlencoded({extended: false})) + +// parse application/json +app.use(bodyParser.json()) + +// index +app.get('/', function (req, res) { + res.send('hello world i am a secret bot') +}) + +// for facebook verification +app.get('/webhook/', function (req, res) { + if (req.query['hub.verify_token'] === 'my_voice_is_my_password_verify_me') { + res.send(req.query['hub.challenge']) + } + res.send('Error, wrong token') +}) + +// to post data +app.post('/webhook/', function (req, res) { + let messaging_events = req.body.entry[0].messaging + for (let i = 0; i < messaging_events.length; i++) { + let event = req.body.entry[0].messaging[i] + let sender = event.sender.id + if (event.message && event.message.text) { + let text = event.message.text + if (text === 'Generic') { + sendGenericMessage(sender) + continue + } + sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) + } + if (event.postback) { + let text = JSON.stringify(event.postback) + sendTextMessage(sender, "Postback received: "+text.substring(0, 200), token) + continue + } + } + res.sendStatus(200) +}) + + +// recommended to inject access tokens as environmental variables, e.g. +// const token = process.env.PAGE_ACCESS_TOKEN +const token = "" + +function sendTextMessage(sender, text) { + let messageData = { text:text } + + request({ + url: 'https://graph.facebook.com/v2.6/me/messages', + qs: {access_token:token}, + method: 'POST', + json: { + recipient: {id:sender}, + message: messageData, + } + }, function(error, response, body) { + if (error) { + console.log('Error sending messages: ', error) + } else if (response.body.error) { + console.log('Error: ', response.body.error) + } + }) +} + +function sendGenericMessage(sender) { + let messageData = { + "attachment": { + "type": "template", + "payload": { + "template_type": "generic", + "elements": [{ + "title": "First card", + "subtitle": "Element #1 of an hscroll", + "image_url": "http://messengerdemo.parseapp.com/img/rift.png", + "buttons": [{ + "type": "web_url", + "url": "https://www.messenger.com", + "title": "web url" + }, { + "type": "postback", + "title": "Postback", + "payload": "Payload for first element in a generic bubble", + }], + }, { + "title": "Second card", + "subtitle": "Element #2 of an hscroll", + "image_url": "http://messengerdemo.parseapp.com/img/gearvr.png", + "buttons": [{ + "type": "postback", + "title": "Postback", + "payload": "Payload for second element in a generic bubble", + }], + }] + } + } + } + request({ + url: 'https://graph.facebook.com/v2.6/me/messages', + qs: {access_token:token}, + method: 'POST', + json: { + recipient: {id:sender}, + message: messageData, + } + }, function(error, response, body) { + if (error) { + console.log('Error sending messages: ', error) + } else if (response.body.error) { + console.log('Error: ', response.body.error) + } + }) +} + +// spin spin sugar +app.listen(app.get('port'), function() { + console.log('running on port', app.get('port')) +}) From ae22a2a55eef15049bc61bf1d1903881420c5711 Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Tue, 23 Aug 2016 17:00:53 +0700 Subject: [PATCH 05/25] Update const.js --- const.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/const.js b/const.js index d3a2a94b..c1c65e67 100644 --- a/const.js +++ b/const.js @@ -1,8 +1,8 @@ 'use strict'; // Wit.ai parameters -const WIT_TOKEN = process.env.WIT_TOKEN; -//const WIT_TOKEN = "YVTD4SSYSXSSYNHGY3TZOG6PTQMP7UWF"; +//const WIT_TOKEN = process.env.WIT_TOKEN; +const WIT_TOKEN = "YVTD4SSYSXSSYNHGY3TZOG6PTQMP7UWF"; if (!WIT_TOKEN) { throw new Error('missing WIT_TOKEN'); } @@ -19,4 +19,4 @@ module.exports = { WIT_TOKEN: WIT_TOKEN, FB_PAGE_TOKEN: FB_PAGE_TOKEN, FB_VERIFY_TOKEN: FB_VERIFY_TOKEN, -}; \ No newline at end of file +}; From 9b3c048b161c7bd0f36865c761b82800eb434b96 Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Tue, 23 Aug 2016 17:09:25 +0700 Subject: [PATCH 06/25] Update package.json --- package.json | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index a65dbdf8..1b42b510 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,37 @@ { - "name": "secretbots", + "name": "fagbot2", "version": "1.0.0", - "description": "", + "description": "Simple Wit-Facebook", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "start": "node index.js", + "bot": "node bot.js", + "test": "jest" }, "author": "", - "license": "ISC", + "license": "MIT", + "jest": { + "scriptPreprocessor": "/node_modules/babel-jest", + "unmockedModulePathPatterns": ["core-js/.*", "sshpk/.*"] + }, "dependencies": { "body-parser": "^1.15.0", "express": "^4.13.4", - "request": "^2.71.0" - } + "node-wit": "^3.2.2", + "node-uuid": "^1.4.7", + "request": "^2.72.0", + "babel-core": "^6.0.0", + "babel-jest": "^6.0.1", + "babel-polyfill": "^6.1.4", + "bl": "^1.0.0", + "jest-cli": "^0.7.1" + }, + "repository": { + "type": "git", + "url": "https://github.com/hunkim/Wit-Facebook.git" + }, + "bugs": { + "url": "https://github.com/hunkim/Wit-Facebook/issues" + }, + "homepage": "https://github.com/hunkim/Wit-Facebook#readme" } From 49c3d22897b7e4c2f46fde5558775829fd18eee8 Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Tue, 23 Aug 2016 17:13:03 +0700 Subject: [PATCH 07/25] Update const.js --- const.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/const.js b/const.js index c1c65e67..90d44708 100644 --- a/const.js +++ b/const.js @@ -8,7 +8,7 @@ if (!WIT_TOKEN) { } // Messenger API parameters -const FB_PAGE_TOKEN = process.env.FB_PAGE_TOKEN; +const FB_PAGE_TOKEN = "EAAR8dpi5Ae4BANlcMZB1rK2zgS0pUDwDVZCgXA64T389NQl2ycT8KZBniXGgebFBq9N3honekW6kIzbWix4NX1pWLDeykpaDcs7AUYI6B4ZBWJkFfg83lFpmIXhBADWXhatEq9ZAXT61dnM2J7YVmvT4efglZCqFXOS5zT9ctpSgZDZD"; var FB_VERIFY_TOKEN = process.env.FB_VERIFY_TOKEN; if (!FB_VERIFY_TOKEN) { From 5d58a601264f3c7f19c776ff5f0769182ff2ebd8 Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Tue, 23 Aug 2016 17:15:07 +0700 Subject: [PATCH 08/25] Update const.js --- const.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/const.js b/const.js index 90d44708..e81464ff 100644 --- a/const.js +++ b/const.js @@ -10,9 +10,9 @@ if (!WIT_TOKEN) { // Messenger API parameters const FB_PAGE_TOKEN = "EAAR8dpi5Ae4BANlcMZB1rK2zgS0pUDwDVZCgXA64T389NQl2ycT8KZBniXGgebFBq9N3honekW6kIzbWix4NX1pWLDeykpaDcs7AUYI6B4ZBWJkFfg83lFpmIXhBADWXhatEq9ZAXT61dnM2J7YVmvT4efglZCqFXOS5zT9ctpSgZDZD"; -var FB_VERIFY_TOKEN = process.env.FB_VERIFY_TOKEN; +var FB_VERIFY_TOKEN = "my_voice_is_my_password_verify_me"; if (!FB_VERIFY_TOKEN) { - FB_VERIFY_TOKEN = "just_do_it"; + FB_VERIFY_TOKEN = "my_voice_is_my_password_verify_me"; } module.exports = { From 1ef47e148838da9a018953ca2386d67c5cd54507 Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Tue, 23 Aug 2016 17:20:16 +0700 Subject: [PATCH 09/25] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index db5445cd..63a09509 100644 --- a/index.js +++ b/index.js @@ -57,7 +57,7 @@ app.post('/webhook/', function (req, res) { // recommended to inject access tokens as environmental variables, e.g. // const token = process.env.PAGE_ACCESS_TOKEN -const token = "" +const token = "EAAR8dpi5Ae4BANlcMZB1rK2zgS0pUDwDVZCgXA64T389NQl2ycT8KZBniXGgebFBq9N3honekW6kIzbWix4NX1pWLDeykpaDcs7AUYI6B4ZBWJkFfg83lFpmIXhBADWXhatEq9ZAXT61dnM2J7YVmvT4efglZCqFXOS5zT9ctpSgZDZD" function sendTextMessage(sender, text) { let messageData = { text:text } From 27f4eff3db13b6e49321e32be580e5f5b98cfb5e Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Tue, 23 Aug 2016 17:26:26 +0700 Subject: [PATCH 10/25] Update index.js --- index.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/index.js b/index.js index 63a09509..97db2329 100644 --- a/index.js +++ b/index.js @@ -43,6 +43,39 @@ app.post('/webhook/', function (req, res) { sendGenericMessage(sender) continue } + const msg = messaging.message.text + if (msg) { + // We received a text message + + // Let's forward the message to the Wit.ai Bot Engine + // This will run all actions until our bot has nothing left to do + wit.runActions( + sessionId, // the user's current session + msg, // the user's message + sessions[sessionId].context, // the user's current session state + (error, context) => { + if (error) { + console.log('Oops! Got an error from Wit:', error); + } else { + // Our bot did everything it has to do. + // Now it's waiting for further messages to proceed. + console.log('Waiting for futher messages.'); + + // Based on the session state, you might want to reset the session. + // This depends heavily on the business logic of your bot. + // Example: + // if (context['done']) { + // delete sessions[sessionId]; + // } + + // Updating the user's current session state + sessions[sessionId].context = context; + sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)); + + } + } + ); + } sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) } if (event.postback) { From a5c5b90ab890694c753b1739b7b0e30d385256fd Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Tue, 23 Aug 2016 17:30:54 +0700 Subject: [PATCH 11/25] Update index.js --- index.js | 52 ++-------------------------------------------------- 1 file changed, 2 insertions(+), 50 deletions(-) diff --git a/index.js b/index.js index 97db2329..2a8c068d 100644 --- a/index.js +++ b/index.js @@ -43,8 +43,8 @@ app.post('/webhook/', function (req, res) { sendGenericMessage(sender) continue } - const msg = messaging.message.text - if (msg) { + + if (text) { // We received a text message // Let's forward the message to the Wit.ai Bot Engine @@ -112,54 +112,6 @@ function sendTextMessage(sender, text) { }) } -function sendGenericMessage(sender) { - let messageData = { - "attachment": { - "type": "template", - "payload": { - "template_type": "generic", - "elements": [{ - "title": "First card", - "subtitle": "Element #1 of an hscroll", - "image_url": "http://messengerdemo.parseapp.com/img/rift.png", - "buttons": [{ - "type": "web_url", - "url": "https://www.messenger.com", - "title": "web url" - }, { - "type": "postback", - "title": "Postback", - "payload": "Payload for first element in a generic bubble", - }], - }, { - "title": "Second card", - "subtitle": "Element #2 of an hscroll", - "image_url": "http://messengerdemo.parseapp.com/img/gearvr.png", - "buttons": [{ - "type": "postback", - "title": "Postback", - "payload": "Payload for second element in a generic bubble", - }], - }] - } - } - } - request({ - url: 'https://graph.facebook.com/v2.6/me/messages', - qs: {access_token:token}, - method: 'POST', - json: { - recipient: {id:sender}, - message: messageData, - } - }, function(error, response, body) { - if (error) { - console.log('Error sending messages: ', error) - } else if (response.body.error) { - console.log('Error: ', response.body.error) - } - }) -} // spin spin sugar app.listen(app.get('port'), function() { From 93bf97b76cc9dcc5455d1e20aff9a75067665004 Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Tue, 23 Aug 2016 17:34:56 +0700 Subject: [PATCH 12/25] Update index.js --- index.js | 67 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/index.js b/index.js index 2a8c068d..7aa2e335 100644 --- a/index.js +++ b/index.js @@ -43,39 +43,40 @@ app.post('/webhook/', function (req, res) { sendGenericMessage(sender) continue } - - if (text) { - // We received a text message - - // Let's forward the message to the Wit.ai Bot Engine - // This will run all actions until our bot has nothing left to do - wit.runActions( - sessionId, // the user's current session - msg, // the user's message - sessions[sessionId].context, // the user's current session state - (error, context) => { - if (error) { - console.log('Oops! Got an error from Wit:', error); - } else { - // Our bot did everything it has to do. - // Now it's waiting for further messages to proceed. - console.log('Waiting for futher messages.'); - - // Based on the session state, you might want to reset the session. - // This depends heavily on the business logic of your bot. - // Example: - // if (context['done']) { - // delete sessions[sessionId]; - // } - - // Updating the user's current session state - sessions[sessionId].context = context; - sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)); - - } - } - ); - } + else + { + // We received a text message + const sender = messaging.sender.id + const sessionId = findOrCreateSession(sender) + // Let's forward the message to the Wit.ai Bot Engine + // This will run all actions until our bot has nothing left to do + wit.runActions( + sessionId, // the user's current session + msg, // the user's message + sessions[sessionId].context, // the user's current session state + (error, context) => { + if (error) { + console.log('Oops! Got an error from Wit:', error); + } else { + // Our bot did everything it has to do. + // Now it's waiting for further messages to proceed. + console.log('Waiting for futher messages.'); + + // Based on the session state, you might want to reset the session. + // This depends heavily on the business logic of your bot. + // Example: + // if (context['done']) { + // delete sessions[sessionId]; + // } + + // Updating the user's current session state + sessions[sessionId].context = context; + sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)); + + } + } + ); + } sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) } if (event.postback) { From 1bc0aebd97404bf7b70ea1a9b0a6fdc18a573532 Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Tue, 23 Aug 2016 17:38:35 +0700 Subject: [PATCH 13/25] Update index.js --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 7aa2e335..64dacaf6 100644 --- a/index.js +++ b/index.js @@ -52,7 +52,7 @@ app.post('/webhook/', function (req, res) { // This will run all actions until our bot has nothing left to do wit.runActions( sessionId, // the user's current session - msg, // the user's message + text, // the user's message sessions[sessionId].context, // the user's current session state (error, context) => { if (error) { @@ -77,7 +77,7 @@ app.post('/webhook/', function (req, res) { } ); } - sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) + //sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) } if (event.postback) { let text = JSON.stringify(event.postback) From cfb46839c3c5f5eb94bc26bfa7cec354cff6e76d Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Tue, 23 Aug 2016 17:40:48 +0700 Subject: [PATCH 14/25] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 64dacaf6..8a9e93bd 100644 --- a/index.js +++ b/index.js @@ -46,7 +46,7 @@ app.post('/webhook/', function (req, res) { else { // We received a text message - const sender = messaging.sender.id + //const sender = messaging.sender.id const sessionId = findOrCreateSession(sender) // Let's forward the message to the Wit.ai Bot Engine // This will run all actions until our bot has nothing left to do From 9ef4705f4a22349da7074df9f4e36c78155cd965 Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Tue, 23 Aug 2016 17:44:45 +0700 Subject: [PATCH 15/25] Update index.js --- index.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 8a9e93bd..03fb8d65 100644 --- a/index.js +++ b/index.js @@ -112,7 +112,27 @@ function sendTextMessage(sender, text) { } }) } - +const findOrCreateSession = (fbid) => { + let sessionId; + // Let's see if we already have a session for the user fbid + Object.keys(sessions).forEach(k => { + if (sessions[k].fbid === fbid) { + // Yep, got it! + sessionId = k; + } + }); + if (!sessionId) { + // No session found for user fbid, let's create a new one + sessionId = new Date().toISOString(); + sessions[sessionId] = { + fbid: fbid, + context: { + _fbid_: fbid + } + }; // set context, _fid_ + } + return sessionId; +} // spin spin sugar app.listen(app.get('port'), function() { From 09ee17f3aea25c079c060fa407b7132b28219aa4 Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Thu, 25 Aug 2016 13:19:16 +0700 Subject: [PATCH 16/25] Add files via upload --- bot.js | 51 +-------------- const.js | 12 ++-- index.js | 196 ++++++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 166 insertions(+), 93 deletions(-) diff --git a/bot.js b/bot.js index 8e1ab243..d6587065 100644 --- a/bot.js +++ b/bot.js @@ -21,7 +21,7 @@ const firstEntityValue = (entities, entity) => { const actions = { say(sessionId, context, message, cb) { console.log(message); - console.log(sendGenericMessage(1234)); //test GenericMessage + //console.log(sendGenericMessage(1234)); //test GenericMessage // Bot testing mode, run cb() and return if (require.main === module) { cb(); @@ -75,55 +75,6 @@ const actions = { cb(context); }, }; -function sendGenericMessage(sender) { - let messageData = { - "attachment": { - "type": "template", - "payload": { - "template_type": "generic", - "elements": [{ - "title": "First card", - "subtitle": "Element #1 of an hscroll", - "image_url": "http://messengerdemo.parseapp.com/img/rift.png", - "buttons": [{ - "type": "web_url", - "url": "https://www.messenger.com", - "title": "web url" - }, { - "type": "postback", - "title": "Postback", - "payload": "Payload for first element in a generic bubble", - }], - }, { - "title": "Second card", - "subtitle": "Element #2 of an hscroll", - "image_url": "http://messengerdemo.parseapp.com/img/gearvr.png", - "buttons": [{ - "type": "postback", - "title": "Postback", - "payload": "Payload for second element in a generic bubble", - }], - }] - } - } - } - request({ - url: 'https://graph.facebook.com/v2.6/me/messages', - qs: {access_token:token}, - method: 'POST', - json: { - recipient: {id:sender}, - message: messageData, - } - }, function(error, response, body) { - if (error) { - console.log('Error sending messages: ', error) - } else if (response.body.error) { - console.log('Error: ', response.body.error) - } - }) - -} const getWit = () => { return new Wit(Config.WIT_TOKEN, actions); diff --git a/const.js b/const.js index e81464ff..d3a2a94b 100644 --- a/const.js +++ b/const.js @@ -1,22 +1,22 @@ 'use strict'; // Wit.ai parameters -//const WIT_TOKEN = process.env.WIT_TOKEN; -const WIT_TOKEN = "YVTD4SSYSXSSYNHGY3TZOG6PTQMP7UWF"; +const WIT_TOKEN = process.env.WIT_TOKEN; +//const WIT_TOKEN = "YVTD4SSYSXSSYNHGY3TZOG6PTQMP7UWF"; if (!WIT_TOKEN) { throw new Error('missing WIT_TOKEN'); } // Messenger API parameters -const FB_PAGE_TOKEN = "EAAR8dpi5Ae4BANlcMZB1rK2zgS0pUDwDVZCgXA64T389NQl2ycT8KZBniXGgebFBq9N3honekW6kIzbWix4NX1pWLDeykpaDcs7AUYI6B4ZBWJkFfg83lFpmIXhBADWXhatEq9ZAXT61dnM2J7YVmvT4efglZCqFXOS5zT9ctpSgZDZD"; +const FB_PAGE_TOKEN = process.env.FB_PAGE_TOKEN; -var FB_VERIFY_TOKEN = "my_voice_is_my_password_verify_me"; +var FB_VERIFY_TOKEN = process.env.FB_VERIFY_TOKEN; if (!FB_VERIFY_TOKEN) { - FB_VERIFY_TOKEN = "my_voice_is_my_password_verify_me"; + FB_VERIFY_TOKEN = "just_do_it"; } module.exports = { WIT_TOKEN: WIT_TOKEN, FB_PAGE_TOKEN: FB_PAGE_TOKEN, FB_VERIFY_TOKEN: FB_VERIFY_TOKEN, -}; +}; \ No newline at end of file diff --git a/index.js b/index.js index 03fb8d65..1d9a070a 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,7 @@ const request = require('request') const app = express() //const bot = require('node-wit').Wit const bot = require('./bot.js') - +const FB = require('./facebook.js') // Setting up our bot const wit = bot.getWit() @@ -23,11 +23,65 @@ app.get('/', function (req, res) { res.send('hello world i am a secret bot') }) +// for facebook verification +app.get('/webcheck/', function (req, res) { +const messaging = FB.getFirstMessagingEntry(req.body); + if (messaging && messaging.message) { + // Yay! We got a new message! + // We retrieve the Facebook user ID of the sender + const sender = messaging.sender.id; + console.log(messaging+sender); + // We retrieve the user's current session, or create one if it doesn't exist + // This is needed for our bot to figure out the conversation history + const sessionId = findOrCreateSession(sender); + // We retrieve the message content + const msg = messaging.message.text; + const atts = messaging.message.attachments; + if (atts) { + // We received an attachment + // Let's reply with an automatic message + FB.fbMessage( + sender, + 'Sorry I can only process text messages for now.' + ); + } else if (msg) { + // We received a text message + + // Let's forward the message to the Wit.ai Bot Engine + // This will run all actions until our bot has nothing left to do + wit.runActions( + sessionId, // the user's current session + msg, // the user's message + sessions[sessionId].context, // the user's current session state + (error, context) => { + if (error) { + console.log('Oops! Got an error from Wit:', error); + } else { + // Our bot did everything it has to do. + // Now it's waiting for further messages to proceed. + console.log('Waiting for futher messages.'); + // Based on the session state, you might want to reset the session. + // This depends heavily on the business logic of your bot. + // Example: + // if (context['done']) { + // delete sessions[sessionId]; + // } + // Updating the user's current session state + sessions[sessionId].context = context; + sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)); + } + } + ); + } + } + res.sendStatus(200); +}) // for facebook verification app.get('/webhook/', function (req, res) { if (req.query['hub.verify_token'] === 'my_voice_is_my_password_verify_me') { res.send(req.query['hub.challenge']) } + res.send('Error, wrong token') }) @@ -43,40 +97,7 @@ app.post('/webhook/', function (req, res) { sendGenericMessage(sender) continue } - else - { - // We received a text message - //const sender = messaging.sender.id - const sessionId = findOrCreateSession(sender) - // Let's forward the message to the Wit.ai Bot Engine - // This will run all actions until our bot has nothing left to do - wit.runActions( - sessionId, // the user's current session - text, // the user's message - sessions[sessionId].context, // the user's current session state - (error, context) => { - if (error) { - console.log('Oops! Got an error from Wit:', error); - } else { - // Our bot did everything it has to do. - // Now it's waiting for further messages to proceed. - console.log('Waiting for futher messages.'); - - // Based on the session state, you might want to reset the session. - // This depends heavily on the business logic of your bot. - // Example: - // if (context['done']) { - // delete sessions[sessionId]; - // } - - // Updating the user's current session state - sessions[sessionId].context = context; - sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)); - - } - } - ); - } + getwit(req) //sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) } if (event.postback) { @@ -112,6 +133,107 @@ function sendTextMessage(sender, text) { } }) } + +function sendGenericMessage(sender) { + let messageData = { + "attachment": { + "type": "template", + "payload": { + "template_type": "generic", + "elements": [{ + "title": "First card", + "subtitle": "Element #1 of an hscroll", + "image_url": "http://messengerdemo.parseapp.com/img/rift.png", + "buttons": [{ + "type": "web_url", + "url": "https://www.messenger.com", + "title": "web url" + }, { + "type": "postback", + "title": "Postback", + "payload": "Payload for first element in a generic bubble", + }], + }, { + "title": "Second card", + "subtitle": "Element #2 of an hscroll", + "image_url": "http://messengerdemo.parseapp.com/img/gearvr.png", + "buttons": [{ + "type": "postback", + "title": "Postback", + "payload": "Payload for second element in a generic bubble", + }], + }] + } + } + } + request({ + url: 'https://graph.facebook.com/v2.6/me/messages', + qs: {access_token:token}, + method: 'POST', + json: { + recipient: {id:sender}, + message: messageData, + } + }, function(error, response, body) { + if (error) { + console.log('Error sending messages: ', error) + } else if (response.body.error) { + console.log('Error: ', response.body.error) + } + }) +} +function getwit(req) +{ +const messaging = FB.getFirstMessagingEntry(req.body); + if (messaging && messaging.message) { + // Yay! We got a new message! + // We retrieve the Facebook user ID of the sender + const sender = messaging.sender.id; + //console.log(messaging+sender); + // We retrieve the user's current session, or create one if it doesn't exist + // This is needed for our bot to figure out the conversation history + const sessionId = findOrCreateSession(sender); + // We retrieve the message content + const msg = messaging.message.text; + const atts = messaging.message.attachments; + if (atts) { + // We received an attachment + // Let's reply with an automatic message + FB.fbMessage( + sender, + 'Sorry I can only process text messages for now.' + ); + } else if (msg) { + // We received a text message + + // Let's forward the message to the Wit.ai Bot Engine + // This will run all actions until our bot has nothing left to do + wit.runActions( + sessionId, // the user's current session + msg, // the user's message + sessions[sessionId].context, // the user's current session state + (error, context) => { + if (error) { + console.log('Oops! Got an error from Wit:', error); + } else { + // Our bot did everything it has to do. + // Now it's waiting for further messages to proceed. + console.log('Waiting for futher messages.'); + // Based on the session state, you might want to reset the session. + // This depends heavily on the business logic of your bot. + // Example: + // if (context['done']) { + // delete sessions[sessionId]; + // } + // Updating the user's current session state + sessions[sessionId].context = context; + sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)); + } + } + ); + } + } +} const findOrCreateSession = (fbid) => { let sessionId; // Let's see if we already have a session for the user fbid @@ -132,9 +254,9 @@ const findOrCreateSession = (fbid) => { }; // set context, _fid_ } return sessionId; -} +}; // spin spin sugar app.listen(app.get('port'), function() { console.log('running on port', app.get('port')) -}) +}) \ No newline at end of file From 8d6e4925aa5c3eca4f3ddded9593c77c386accee Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Thu, 25 Aug 2016 13:22:00 +0700 Subject: [PATCH 17/25] Update index.js --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 1d9a070a..ca5bba55 100644 --- a/index.js +++ b/index.js @@ -97,8 +97,8 @@ app.post('/webhook/', function (req, res) { sendGenericMessage(sender) continue } - getwit(req) - //sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) + //getwit(req) + sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) } if (event.postback) { let text = JSON.stringify(event.postback) @@ -259,4 +259,4 @@ const findOrCreateSession = (fbid) => { // spin spin sugar app.listen(app.get('port'), function() { console.log('running on port', app.get('port')) -}) \ No newline at end of file +}) From df53b1f819abac73185c43e8a651fbd56df0cfb2 Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Thu, 25 Aug 2016 13:26:24 +0700 Subject: [PATCH 18/25] Update const.js --- const.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/const.js b/const.js index d3a2a94b..c1c65e67 100644 --- a/const.js +++ b/const.js @@ -1,8 +1,8 @@ 'use strict'; // Wit.ai parameters -const WIT_TOKEN = process.env.WIT_TOKEN; -//const WIT_TOKEN = "YVTD4SSYSXSSYNHGY3TZOG6PTQMP7UWF"; +//const WIT_TOKEN = process.env.WIT_TOKEN; +const WIT_TOKEN = "YVTD4SSYSXSSYNHGY3TZOG6PTQMP7UWF"; if (!WIT_TOKEN) { throw new Error('missing WIT_TOKEN'); } @@ -19,4 +19,4 @@ module.exports = { WIT_TOKEN: WIT_TOKEN, FB_PAGE_TOKEN: FB_PAGE_TOKEN, FB_VERIFY_TOKEN: FB_VERIFY_TOKEN, -}; \ No newline at end of file +}; From e27f44b8f2d3c7890d0e93d6ed44fd5f9d7873eb Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Thu, 25 Aug 2016 13:36:44 +0700 Subject: [PATCH 19/25] Update index.js --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index ca5bba55..d534ea91 100644 --- a/index.js +++ b/index.js @@ -97,8 +97,8 @@ app.post('/webhook/', function (req, res) { sendGenericMessage(sender) continue } - //getwit(req) - sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) + getwit(text) + //sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) } if (event.postback) { let text = JSON.stringify(event.postback) @@ -182,9 +182,9 @@ function sendGenericMessage(sender) { } }) } -function getwit(req) +function getwit(messaging) { -const messaging = FB.getFirstMessagingEntry(req.body); +//const messaging = FB.getFirstMessagingEntry(req.body); if (messaging && messaging.message) { // Yay! We got a new message! // We retrieve the Facebook user ID of the sender From 76427ef4afc7c2a1496fb8a367478e0d9f20620d Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Thu, 25 Aug 2016 13:40:47 +0700 Subject: [PATCH 20/25] Update index.js --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index d534ea91..fd2c992a 100644 --- a/index.js +++ b/index.js @@ -228,6 +228,7 @@ function getwit(messaging) // Updating the user's current session state sessions[sessionId].context = context; sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)); + console.log(text.substring(0, 200)); } } ); From a3f709d5ddee67761046b7e06cf2f903dcb79220 Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Thu, 25 Aug 2016 15:22:12 +0700 Subject: [PATCH 21/25] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index fd2c992a..6f39ca28 100644 --- a/index.js +++ b/index.js @@ -97,7 +97,7 @@ app.post('/webhook/', function (req, res) { sendGenericMessage(sender) continue } - getwit(text) + getwit(messaging_events) //sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) } if (event.postback) { From 9cbf12dc6ae8cb5ddd9f27055c27b6d264208209 Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Thu, 25 Aug 2016 15:41:32 +0700 Subject: [PATCH 22/25] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 6f39ca28..685aa19f 100644 --- a/index.js +++ b/index.js @@ -189,7 +189,7 @@ function getwit(messaging) // Yay! We got a new message! // We retrieve the Facebook user ID of the sender const sender = messaging.sender.id; - //console.log(messaging+sender); + console.log(messaging+sender); // We retrieve the user's current session, or create one if it doesn't exist // This is needed for our bot to figure out the conversation history const sessionId = findOrCreateSession(sender); From 05e5d6bd0bf5c027bc8877fa75d11c4c3e38496e Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Thu, 25 Aug 2016 16:22:22 +0700 Subject: [PATCH 23/25] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 685aa19f..0d3bf196 100644 --- a/index.js +++ b/index.js @@ -97,7 +97,7 @@ app.post('/webhook/', function (req, res) { sendGenericMessage(sender) continue } - getwit(messaging_events) + getwit(event) //sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) } if (event.postback) { From ea73743fadbddf28bdc2c0808297feed0016c264 Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Thu, 25 Aug 2016 16:30:27 +0700 Subject: [PATCH 24/25] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 0d3bf196..207b84a6 100644 --- a/index.js +++ b/index.js @@ -97,7 +97,7 @@ app.post('/webhook/', function (req, res) { sendGenericMessage(sender) continue } - getwit(event) + getwit(event.message) //sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) } if (event.postback) { From 88590f02a6d86dd1f0e489a448ea106b619aef6c Mon Sep 17 00:00:00 2001 From: Worwae77 Date: Thu, 25 Aug 2016 16:39:34 +0700 Subject: [PATCH 25/25] Update index.js --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 207b84a6..b58cd788 100644 --- a/index.js +++ b/index.js @@ -97,8 +97,8 @@ app.post('/webhook/', function (req, res) { sendGenericMessage(sender) continue } - getwit(event.message) - //sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) + //getwit(event.message) + sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) } if (event.postback) { let text = JSON.stringify(event.postback)