Spacebot is facebook messenger bot which tells you about NASA and ISS and other cool stuff. Spacebot is developed using NodeJs. Currently it is deployed on Heroku server
- About ISS
- About NASA
- Temperature of your region
- Latest News
- Quotes,Jokes
-
Create or configure a Facebook App or Page here https://developers.facebook.com/apps/
-
In the app go to Messenger tab then click Setup Webhook. Here you will put in the URL of your Heroku server and a token. Make sure to check all the subscription fields.
-
Get a Page Access Token and save this somewhere.
-
Go back to Terminal and type in this command to trigger the Facebook app to send messages. Remember to use the token you requested earlier.
curl -X POST "https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=<PAGE_ACCESS_TOKEN>"
Now that Facebook and Heroku can talk to each other we can code out the bot.
-
Add an API endpoint to index.js to process messages. Remember to also include the token we got earlier.
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 sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200)) } } res.sendStatus(200) }) const token = "<PAGE_ACCESS_TOKEN>"
Optional, but recommended: keep your app secrets out of version control!
- On Heroku, its easy to create dynamic runtime variables (known as config vars). This can be done in the Heroku dashboard UI for your app or from the command line:
heroku config:set FB_PAGE_ACCESS_TOKEN=fake-access-token-dhsa09uji4mlkasdfsd # view heroku config
- For local development: create an environmental variable in your current session or add to your shell config file.
# create env variable for current shell session export FB_PAGE_ACCESS_TOKEN=fake-access-token-dhsa09uji4mlkasdfsd # alternatively, you can add this line to your shell config # export FB_PAGE_ACCESS_TOKEN=fake-access-token-dhsa09uji4mlkasdfsd echo $FB_PAGE_ACCESS_TOKEN
config var
access at runtime
const token = process.env.FB_PAGE_ACCESS_TOKEN
-
Add a function to echo back messages
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) } }) }
-
Commit the code again and push to Heroku
git add . git commit -m 'updated the bot to speak' git push heroku master
-
Go to the Facebook Page and click on Message to start chatting!
Want to contribute? Great!
MIT