Created
October 14, 2017 07:11
-
-
Save GeekyShiva/649b85398ec516b3d19d56d6a5a4954e to your computer and use it in GitHub Desktop.
Chatbot index.js file
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 express = require('express') | |
var bodyParser = require('body-parser') | |
var request = require('request') | |
var app = express() | |
app.set('port', (process.env.PORT || 5000)) | |
// Process application/x-www-form-urlencoded | |
app.use(bodyParser.urlencoded({extended: false})) | |
// Process application/json | |
app.use(bodyParser.json()) | |
// Index route | |
app.get('/', function (req, res) { | |
res.send('Hello world, I am a chat bot') | |
}) | |
// for Facebook verification | |
app.get('/webhook/', function (req, res) { | |
if (req.query['hub.verify_token'] === 'Aha_Moment_Labs') { | |
res.send(req.query['hub.challenge']) | |
} | |
res.send('Error, wrong token') | |
}) | |
// Spin up the server | |
app.listen(app.get('port'), function() { | |
console.log('running on port', app.get('port')) | |
}) | |
// API End Point - added by Stefan | |
app.post('/webhook/', function (req, res) { | |
messaging_events = req.body.entry[0].messaging | |
for (i = 0; i < messaging_events.length; i++) { | |
event = req.body.entry[0].messaging[i] | |
sender = event.sender.id | |
if (event.message && event.message.text) { | |
text = event.message.text | |
if (text === 'hi') { | |
sendGenericMessage(sender) | |
continue | |
} | |
sendTextMessage(sender, "parrot: " + text.substring(0, 200)) | |
} | |
if (event.postback) { | |
text = JSON.stringify(event.postback) | |
sendTextMessage(sender, "Postback received: "+text.substring(0, 200), token) | |
continue | |
} | |
} | |
res.sendStatus(200) | |
}) | |
var token = " enter token here" | |
// function to echo back messages - added by Stefan | |
function sendTextMessage(sender, text) { | |
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) | |
} | |
}) | |
} | |
// Send an test message back as two cards. | |
function sendGenericMessage(sender) { | |
messageData = { | |
"attachment": { | |
"type": "template", | |
"payload": { | |
"template_type": "generic", | |
"elements": [{ | |
"title": "Ai Chat Bot Communities", | |
"subtitle": "Communities to Follow", | |
"image_url": "http://1u88jj3r4db2x4txp44yqfj1.wpengine.netdna-cdn.com/wp-content/uploads/2016/04/chatbot-930x659.jpg", | |
"buttons": [{ | |
"type": "web_url", | |
"url": "https://www.facebook.com/groups/aichatbots/", | |
"title": "FB Chatbot Group" | |
}, { | |
"type": "web_url", | |
"url": "https://www.reddit.com/r/Chat_Bots/", | |
"title": "Chatbots on Reddit" | |
},{ | |
"type": "web_url", | |
"url": "https://twitter.com/aichatbots", | |
"title": "Chatbots on Twitter" | |
}], | |
}, { | |
"title": "Chatbots FAQ", | |
"subtitle": "Aking the Deep Questions", | |
"image_url": "https://tctechcrunch2011.files.wordpress.com/2016/04/facebook-chatbots.png?w=738", | |
"buttons": [{ | |
"type": "postback", | |
"title": "What's the benefit?", | |
"payload": "Chatbots make content interactive instead of static", | |
},{ | |
"type": "postback", | |
"title": "What can Chatbots do", | |
"payload": "One day Chatbots will control the Internet of Things! You will be able to control your homes temperature with a text", | |
}, { | |
"type": "postback", | |
"title": "The Future", | |
"payload": "Chatbots are fun! One day your BFF might be a Chatbot", | |
}], | |
}, { | |
"title": "Learning More", | |
"subtitle": "Aking the Deep Questions", | |
"image_url": "http://www.brandknewmag.com/wp-content/uploads/2015/12/cortana.jpg", | |
"buttons": [{ | |
"type": "postback", | |
"title": "AIML", | |
"payload": "Checkout Artificial Intelligence Mark Up Language. Its easier than you think!", | |
},{ | |
"type": "postback", | |
"title": "Machine Learning", | |
"payload": "Use python to teach your maching in 16D space in 15min", | |
}, { | |
"type": "postback", | |
"title": "Communities", | |
"payload": "Online communities & Meetups are the best way to stay ahead of the curve!", | |
}], | |
}] | |
} | |
} | |
} | |
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) | |
} | |
}) | |
} | |
deployed at https://git.heroku.com/evening-eyrie-79968.git
Curl -X POST "https://graph.facebook.com/v2.6/me/subscibed_app?access_token=EAAIysQgJZBtkBAJ0v6U6PcMlphkIL6mLZArqAy6DRKOACpT7RK3TiMdtjeAgJPHKZB02NRznt7uChyvliZAL9eL1cSPMG4fCc3cjiRwb2bwh6cW4kb1ZCSq8nN6VdgQHZBfFxuCfBAFv6RkCSoGiZCKWDT68yEz0C9DJhPoldcR8QZDZD"
{"error":{"message":"Unknown path components: /subscibed_app","type":"OAuthException","code":2500,"fbtrace_id":"Av3BExPyErh6p6WEaFFvnQH"}}
I cannot trigger to facebook, please correct me with this error, thanks !
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add Procfile with the following text : web: node index.js