Appointment Scheduling with Facebook Messenger Chat Bots and NodeJS

Ilya Nevolin
Geek Culture
Published in
3 min readMar 28, 2021

--

In this post, I explain how we created a ChatBot with integrated Appointment booking using the Facebook Messenger API. This is a NodeJS implementation but can easily be reproduced in any other modern language.

Facebook is quite flexible and provides many cool features. The messenger can be added to your own Facebook page, but can also be embedded directly onto your own website or app. For educational purposes we will use it on our Facebook page. The full code is located on our GitHub repository at the bottom of this post.

Here’s a basic demo of how it works:

Getting API Credentials

To get started with the Facebook Messenger API, you can follow an official guide. You need to obtain three important credentials: App Secret Key, Page Access Token and Callback user token. The last token is a user defined value.

Implementation

During one of the steps you’ll have to provide a webhook URL to your server, this allows Facebook to verify the connection but also validate your user defined token:

// GET request
router.get('/spurwing-fbbot/', (req, res) => {
// verify token and send back the challenge
});

Once your Facebook App has been created and the webhook successfully verified by Facebook, we can start implementing and testing our page’s messenger and chat bot:

// POST request
router.post('/spurwing-fbbot/', async (req, res) => {
verifyRequestSignature(req, res) // make sure it really is Facebook's message for (const e of req.body.entry) {
if (e.messaging)
for (const m of e.messaging) {
await fb_msg_process(m.sender.id, m.message)
}
}
res.send({success:1})
});

The code above is a very simple router implementation for receiving user messages through the messenger. Next we need to process the user text and reply properly:

async function fb_msg_process(senderId, msg) {  // default fall-back message
let resp = {text: "I don't know what you mean :("}
if (msg && msg.text) {
let text = msg.text.toLowerCase();
if (msg.quick_reply && msg.quick_reply.payload)
text = msg.quick_reply.payload;
switch(true) {
case /^book$/.test(text):
resp = await fb_msg_process_funcs.book(text);
break;
case /^book_day:.+$/.test(text):
resp = await fb_msg_process_funcs.book_day(text);
break;
case /^book_slot:.+$/.test(text):
resp = await fb_msg_process_funcs.book_slot(text);
break;
}
}
fb_msg_reply(senderId, resp) // send a reply back}

The code above parses and handles the received messages based on their context. The animated gif at the top shows this exact logic in action.

The appointment booking and scheduling logic is provided by our Spurwing API (NodeJS library). It allows us to list all available days, then all available time slots for a given day, and finally book an appointment on a selected time slot. The full code of this implementation is located in index.js at our GitHub repository here.

Conclusion

This is a very simple yet effective chat bot implementation using Facebook Messenger API. But it does miss a few key details:

  • All dates and times are relative to your server, and not relative to the user’s timezone. Facebook has advanced messaging features which you can enable to receive the user’s actual timezone.
  • Alternatively you can ask the user’s timezone in the chat yourself.
  • The number of quick reply buttons is limited. But the number of available days and/or time slots can exceed this limit. Custom logic should be implemented to provide more flexible scheduling options.

It’s up to the developers to decide on how to handle a user’s timezone and the quick reply inputs. The latter can be achieved by manually entering a time slot and giving feedback on its availability, maybe even reaching out to NLP strategies for more complex language parsing. But if you’re a novice programmer keep it simple and easy.

For more booking and calendar solutions visit our Github account.

--

--

Ilya Nevolin
Geek Culture

Appointment Scheduling API and Calendar Management Solutions for Startups & Enterprises