Skip to content

A Facebook messenger chat bot that aims to be as simple as possible to serve humans with their everyday tasks and space information

Notifications You must be signed in to change notification settings

aryankanwar/SpaceBot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spacebot A Facebook Messenger Bot

N|Solid

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

Bot can tell you

  • About ISS
  • About NASA
  • Temperature of your region
  • Latest News
  • Quotes,Jokes

🙌 Get set

Setup the Facebook App

  1. Create or configure a Facebook App or Page here https://developers.facebook.com/apps/

  2. 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.

  3. Get a Page Access Token and save this somewhere.

  4. 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>"

Setup the bot

Now that Facebook and Heroku can talk to each other we can code out the bot.

  1. 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: Alt text
    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
  2. 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)
    	    }
        })
    }
  3. Commit the code again and push to Heroku

    git add .
    git commit -m 'updated the bot to speak'
    git push heroku master
    
  4. Go to the Facebook Page and click on Message to start chatting!

Development

Want to contribute? Great!

License

MIT

About

A Facebook messenger chat bot that aims to be as simple as possible to serve humans with their everyday tasks and space information

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published