how to create Facebook messenger bot

how to create Facebook messenger botFacebook like Twitter gives its user the opportunity to create chatbots which mimic human conversation with other user using artificial intelligence. This can help you to be free from constantly being online on Facebook to reply message because you can set up your chatbots to reply to your message and your friend and family won’t know.

Facebook chatbots is very useful for business owner who have created a Facebook page for their business. You can use the chatbots to answer customer inquiry and you don’t have to be online every time or hire virtual assistance for this work because you are using Facebook chatbots.

Read More: https://craftmultimedia.com/english-101-for-blogger/

To set up your chatbots you need to know how to add codes but you don’t have to worry because in this article I would show the step by step on how to create your Facebook chatbots. You can outsource to professionals programmer if you are not sure that you can create the chatbots by yourself.

 

  • To create a Facebook app head over to https://developers.facebook.com/apps to create a developers account or if you already have developers account then just login to that account.
  • Input your details and click register now to create a developer account.
  • Now select the platform in which you want to create the Facebook messenger. Here I am selecting Android platform.
  • After selecting a platform you have to create a New App ID. Now fill the required information like Display Name, Contact Email & Category.
  • Then filling the captcha you will be redirected to your app dashboard.
  • On your dashboard go to the left sidebar click on Add Products >Messenger.
  • To get your Page access token “Select a Page” to generate a page access token. Copy and save the token on your computer.
  • In the Webhooks section click on “setup Webhooks”
  • To setup your webhooks Facebook ask you to call back URL, you can’t use localhost while developing Facebook chat bot messenger or you can use any free tunneling tools.
  • Now add your callback URL, Verify Token can be any string and checked the subscription fields.
  • In your webhook URL, you have to add code for verification to verify token.

This is the sample code for Javascript.

app.get(‘/webhook’, function(req, res) {

if (req.query[‘hub.mode’] === ‘subscribe’ &&

req.query[‘hub.verify_token’] === <VERIFY_TOKEN>) {

console.log(“Validating webhook”);

res.status(200).send(req.query[‘hub.challenge’]);

} else {

console.error(“Failed validation. Make sure the validation tokens match.”);

res.sendStatus(403);

}

});

  • Now we have to receive the messages we need to make POST calls to Facebook webhooks and all callback will be made to this webhook.

app.post(‘/webhook’, function (req, res) {

var data = req.body;

// Make sure this is a page subscription

if (data.object === ‘page’) {

// Iterate over each entry – there may be multiple if batched

data.entry.forEach(function(entry) {

var pageID = entry.id;

var timeOfEvent = entry.time;

// Iterate over each messaging event

entry.messaging.forEach(function(event) {

if (event.message) {

receivedMessage(event);

} else {

console.log(“Webhook received unknown event: “, event);

}

});

});

// Assume all went well.

//

// You must send back a 200, within 20 seconds, to let us know

// you’ve successfully received the callback. Otherwise, the request

// will time out and we will keep trying to resend.

res.sendStatus(200);

}

});

function receivedMessage(event) {

// Putting a stub for now, we’ll expand it in the following steps

console.log(“Message data: “, event.message);

}

 

 

  • You have to send the message back to the user,

function receivedMessage(event) {

var senderID = event.sender.id;

var recipientID = event.recipient.id;

var timeOfMessage = event.timestamp;

var message = event.message;

console.log(“Received message for user %d and page %d at %d with message:”,

senderID, recipientID, timeOfMessage);

console.log(JSON.stringify(message));

var messageId = message.mid;

var messageText = message.text;

var messageAttachments = message.attachments;

if (messageText) {

// If we receive a text message, check to see if it matches a keyword

// and send back the example. Otherwise, just echo the text we received.

switch (messageText) {

case ‘generic’:

sendGenericMessage(senderID);

break;

default:

sendTextMessage(senderID, messageText);

}

} else if (messageAttachments) {

sendTextMessage(senderID, “Message with attachment received”);

}

}

You have successfully created your Facebook chatbots, you can also set up Manychat messenger bot instead of the one we did earlier. Facebook messenger bot is easy to set up and you will discover that your work will become easier especially for those receiving large message per day.

 

 

 

Please follow and like us:
error

Leave a Reply

Your email address will not be published. Required fields are marked *