web: node index.js
Confirm all code with Git then create a new Heroku code and "push" the code to the cloud:
web: node index.js
7. Confirm all code with Git then create a new Heroku code and "push" the code to the cloud:
git init git add . git commit --message 'hello world' heroku create git push heroku master
1. Visit https://developers.facebook.com/apps/ to create or configure Facebook App or Page.
How to build a bot that automatically chat on Facebook Messenger Picture 2
2. On the application, switch to the Messenger tab and then click Setup Webhook . Here enter the URL of Heroku server and the token.
How to build a bot that automatically chat on Facebook Messenger Picture 3
3. Get Page Access Token and save this code.
How to build a bot that automatically chat on Facebook Messenger Picture 4
4. Go back to Terminal and enter the following command to activate the Facebook app to send the message. Note, use the token you used earlier.
curl -X POST "https://graph.facebook.com/v2.6/me/subscribed_apps?access_token="
1. Add API endpoint to index.js to perform message processing. Note, including the token you received earlier:
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 sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) } } res.sendStatus(200) }) var token = ""
2. Add a function to respond to the message:
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) } }) }
3. Confirm the code again and push to Heroku:
git add . git commit -m 'updated the bot to speak' git push heroku master
4. Access Facebook and click Message to start chatting.
How to build a bot that automatically chat on Facebook Messenger Picture 5
Facebook Messenger can send a Message message structure in the form of a card or button.
How to build a bot that automatically chat on Facebook Messenger Picture 6
1. Copy the code below some index.js to send a test message in the form of 2 cards:
function sendGenericMessage(sender) { 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) } }) }
2. Update Webhook API to search for special messages to activate on the card:
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 === 'Generic') { sendGenericMessage(sender) continue } sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) } } res.sendStatus(200) })
What happens when a user clicks on a button or card on Message? In this case, update the Webhook API again to send the postback function:
``` 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 === 'Generic') { sendGenericMessage(sender) continue } sendTextMessage(sender, "Text received, echo: " + 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) }) ```
Add Git, confirm and push up Heroku again.
Now you can chat with bot and enter "Generic" to see the bot.
Good luck!