https://github.com/asadm/insult-bot-tutorial
A basic messenger bot.
Science Score: 26.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Committers with academic emails
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (11.1%) to scientific vocabulary
Repository
A basic messenger bot.
Basic Info
- Host: GitHub
- Owner: asadm
- License: mit
- Language: JavaScript
- Default Branch: master
- Size: 17.3 MB
Statistics
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
- Releases: 0
Metadata Files
README.md
🤖 Creating your own Facebook Messenger bot

Facebook recently opened up their Messenger platform to enable bots to converse with users through Facebook Apps and on Facebook Pages. This is a simpler version of jw84's bot tutorial.
You can read the documentation the Messenger team prepared but it's not very clear for beginners and intermediate hackers.
So instead here is how to create your own messenger bot in 15 minutes.
🙌 Get set
Messenger bots uses a web server to process messages it receives or to figure out what messages to send. You also need to have the bot be authenticated to speak with the web server and the bot approved by Facebook to speak with the public.
You can also skip the whole thing by git cloning this repository, running npm install, and run a server somewhere.
Build the server
You need a publicly accessible server URL. Heroku is a good free place. Install the Heroku toolbelt from here https://toolbelt.heroku.com to launch, stop and monitor instances. Sign up for free at https://www.heroku.com if you don't have an account yet.
Install Node from here https://nodejs.org, this will be the server environment. Then open up Terminal or Command Line Prompt and make sure you've got the very most recent version of npm by installing it again:
sudo npm install npm -gCreate a new folder somewhere and let's create a new Node project. Hit Enter to accept the defaults.
npm initInstall the additional Node dependencies. Express is for the server, request is for sending out messages and body-parser is to process messages.
npm install express request body-parser --saveCreate an index.js file in the folder and copy this into it. We will start by authenticating the bot.
```javascript 'use strict'
const express = require('express') const bodyParser = require('body-parser') const request = require('request') const 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.verifytoken'] === 'BOTTOKEN') { 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')) }) ```
Make a file called Procfile and copy this. This is so Heroku can know what file to run.
web: node index.jsCommit all the code with Git then create a new Heroku instance and push the code to the cloud.
git init git add . git commit --message 'hello world' heroku create git push heroku master
Setup the Facebook App
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.

You need a new Facebook page for your bot (or can use an existing one). Get a Page Access Token and save this somewhere. Also subscribe the webhook for your page.

Go back to Terminal and type in this command to trigger the Facebbook app to send messages. Remember to use the token you requested earlier. This should return a JSON success.
bash curl -X POST "https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=PAGE_TOKEN"
Setup the bot
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.
```javascript app.post('/webhook/', function (req, res) { let messagingevents = req.body.entry[0].messaging for (let i = 0; i < messagingevents.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 = "
" ``` 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:
```bash
heroku config:set FBPAGEACCESS_TOKEN=fake-access-token-dhsa09uji4mlkasdfsd
view
heroku config ```
- 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:
- For local development: create an [environmental variable](https://en.wikipedia.org/wiki/Environment_variable) in your current session or add to your shell config file.
```bash
# 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
``` javascript
const token = process.env.FB_PAGE_ACCESS_TOKEN
```
Add a function to echo back messages
javascript 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 masterGo to the Facebook Page and click on Message to start chatting!

📡 How to share your bot
Add a chat button to your webpage
Go here to learn how to add a chat button your page.
Create a shortlink
You can use https://m.me/
💡 What's next?
You can learn how to get your bot approved for public use here.
You can also connect an AI brain to your bot here
Read about all things chat bots with the ChatBots Magazine here
You can also design Messenger bots in Sketch with the Bots UI Kit!
Owner
- Name: Asad Memon
- Login: asadm
- Kind: user
- Location: San Francisco Bay Area
- Website: https://asadmemon.com
- Repositories: 13
- Profile: https://github.com/asadm
GitHub Events
Total
Last Year
Committers
Last synced: about 1 year ago
Top Committers
| Name | Commits | |
|---|---|---|
| Asad Memon | a****d@r****o | 6 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: about 1 year ago
All Time
- Total issues: 0
- Total pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Total issue authors: 0
- Total pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 0
- Average time to close issues: N/A
- Average time to close pull requests: N/A
- Issue authors: 0
- Pull request authors: 0
- Average comments per issue: 0
- Average comments per pull request: 0
- Merged pull requests: 0
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels
Dependencies
- body-parser ^1.15.0
- express ^4.13.4
- request ^2.71.0