I'm facing 2 issues when developing a Facebook messenger bot, and I'm a newbie to programming.
I followed FB's tutorial to add the code - welcome message and deployed it in heroku, but my bot didn't pop up the said message.
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 } if (text === 'button') { sendbuttonmessage(sender) continue } welcomemessage(sender) //sendbuttonmessage(sender) } if (event.postback) { let text = JSON.stringify(event.postback) sendTextMessage(sender, "Postback received: "+text.substring(0, 200), token) continue } } res.sendStatus(200) })
function welcomemessage (sender) { let messageData = { "setting_type":"call_to_actions", "thread_state":"new_thread", "call_to_actions":[ { "message":{ "text":"Welcome to My Company!" } } ] } 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) } }) }
- How do I pop up another button when ppl clicked them? For example:
send function
sendbuttonmessage(sender)
after they click the web url of the button.
function sendbuttonmessage (sender) { let messageData = { "attachment": { "type":"template", "payload":{ "template_type":"button", "text":"Welcome to Taikoo Place. How can we help?", "buttons":[ { "type":"web_url", "url":"https://peterapparel.parseapp.com", "title":"Show Website" }, { "type":"postback", "title":"Service Lift Booking", "payload":"what" //"payload":"USER_DEFINED_PAYLOAD" }, ] } } }