You can follow the documentation, where the Messagner team has prepared a clear guide written in node.js for the beginner.
Here is our 10 min guide to create an echo bot with botimize bot analyitic.
Messanger uses the web server to receive and send the message(text, emoji, pic). You need to have the authority to talk to the web service and then the bot have to approved by Facebook developer platform in order to speak the public.
You can easily git clone the whole project, and run the server somewhere else e.g. heroku.
Sign up an heroku account at https://www.heroku.com.
Create a project
npm init
install the package
npm install express request botimize body-parser
First, go to facebook developer dashboard Click create new app to create a new app or Add new app if you already have some apps exsiting.
In the project dashboard, click
Then click Messagner get start
Generate facebook_access_token
Keep this very long Facebook_Access_Token in a notebook, very important!
Go to botimize and create an account.
Create a new project by clicking new project.
See your Your_Botimize_Api_Key by clicking Project Setting
Create a node.js script (e.g. index.js) and copy this into it. Notice your have to replace Your_Facebook_Access_Token and Your_Botimize_Api_Key.
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var app = express();
Botimize_Api_Key = 'Your_Botimize_Api_Key'
FACEBOOK_ACCESS_TOKEN = 'Your_Facebook_Access_Token'
const botimize = require('botimize')(Botimize_Api_Key,'facebook');
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.listen((process.env.PORT || 3000));
// Facebook Webhook
app.get('/', function (req, res) {
if (req.query['hub.verify_token'] === 'testbot_verify_token') {
res.send(req.query['hub.challenge']);
} else {
res.send('Invalid verify token');
}
});
// handler receiving messages
app.post('/', function (req, res) {
var events = req.body.entry[0].messaging;
for (i = 0; i < events.length; i++) {
var event = events[i];
if (event.message) {
if(event.message.text){
botimize.logIncoming(req.body);
sendMessage(event.sender.id, {"text": event.message.text});
}
}
}
res.sendStatus(200);
});
// generic function sending messages
function sendMessage(recipientId, message) {
var options = {
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token: FACEBOOK_ACCESS_TOKEN},
method: 'POST',
json: {
recipient: {id: recipientId},
message: message,
}
};
request(options, function(error, response, body) {
botimize.logOutgoing(options);
if (error) {
console.log('Error sending message: ', error);
}
else if (response.body.error) {
console.log('Error: ', response.body.error);
}
});
};
git init
git add .
git commmit -m "create a heroku project"
heroku create
git push heroku master
There will show a heroku-url on screen after you push to heroku. Keep this Url (e.g. https://your_app_name.herokuapp.com).
- Go to the facebook developer dashboard again, set up the webhook by filling https://your_app_name.herokuapp.com into the field and subscribe to your page. Your password for the webhook is testbot_verify_token which has shown in the upper nodejs script.
- Now you can talk to your bot by clicking the "Send Message" on the fan page of the facebook.