Facebook Chatbot Architecture

ankit

Use Case:

We required to build a standalone chatbot server. Since we need to incorporate a reasonable amount of intelligence and configurability into the solution to be reused by different customers for their customized auto responses, we feel it will be better in long run to design it as a standalone system. This will also address the volume/scale needs to respond to thousands of users trying to chat per FB page without impacting other customers and to tackle with any number of users.

Technologies/Architecture :

— Facebook Webhooks

— Amazon SNS + SQS

— Node JS : Publisher / Subscribers

— DynamoDB / MongoDB : to store Conversation data , also updated by Customers main server, each customer has their own custom data to respond user messages

Discuss your proeject

Prerequisites :

1] Configured your facebook page

2] Webhook

3] Generate token on facebook messenger for your page

4] Verified/whitelisted your page with necessary permissions for webhook implementation :

5] Your secured web page must be open for get and post apis (the same api you need create with get and post method)

Here, get method used by facebook webhook to verify your web page – facebook post some code and you need to give it back using the

hub_challenge and hub_verify_token

With nodejs you can use below code :

app.get(‘/chatmessages, function (req, res) {
if (req.query[‘hub.verify_token’] === ‘YOUR_VERIFY_TOKEN’) { // this token is your page

                                                                      token – authenticated page token for messenger
res.send(req.query[‘hub.challenge’]); // this is must required
} else {
res.send(‘Not authorized’);
}
});

To get user messages from facebook webhook :

app.post(‘/chatmessages, function (req, res) {
console.log(res);
});

Architecture Details:

Great, Now we have setup all things and our webpage started to get user messages.

But the real challenge comes here : (upto now facebook has done all things for you, we have just configured it’s webhook !!), Assume your facebook page messenger is much popular or many users comes to page and ask questions for that, your server must be ready with proper architecture, also same scenario applied for all customers.

As facebook webhook gives you user chat messages very fast and your messenger also needs to reply all users without missing any single message and also simultaneously to keep the real feel of chatbot without making the user feel of not getting immediate response or getting bored !!

For that, our server must be able/ready to take the load and scale accordingly, also our server must not lost any single message from any single user.

To achieve this we have followed below architecture :

1] Load Balancer :

As we have used amazon ec2 service, we configure our auto scaling, so now our server is ready to get any amount of requests. So, our main webhook configured here and it receives the messages from facebook.

2] SNS :

We have configured SNS (which is invoked when we get any request from facebook ,Which put the message id in SQS for process and simultaneously invoke one http request to our process server.

3] SQS :

Now, we have put each our user request to SQS (from my server) so, we dose not lost any single user request. (you can use other queuing system like Rabbitmq or any).

4] Code process and reply back to facebook server :

We have configured another ec2 with auto scaling to process the each user message and prepare response and post it back to user on facebook  page messenger.

So now, we have different server to take requests from facebook and then post it back !!facebook messenger chatbot

5] Process request and prepare response :

Now, based on each SNS request, process server process each user message request.

We have created nosql database for the storing response and apply some intelligence algorithm to prepare response as exactly question from user (or no match response, that is also random form many !!)

Note : You can apply the algorithm and data structure as per your need, it can be go at any level.. Also you can continuously update the data based on user experience/questions, once you start the actual chat box. But please aware your algorithm must be faster to process and give response ASAP.

6] After preparing each request, we post it back to facebook and also mark(delete) the SQS id.

7] For the missing or not processed SQS ids, you can run a cron or some script to process them or mark them clear.

So, we have configured chat bot with auto scale and also apply queue mechanism so we are ready to process huge number of requests without failing.

You can also use Lambda or other similar service for serverless architecture, about which we will talk later.

about the author

ankit