DEV Community

Cover image for Appointment Booking with Facebook Messenger Chat Bots and NodeJS
Spurwing
Spurwing

Posted on • Edited on

Appointment Booking with Facebook Messenger Chat Bots and NodeJS

In post I explain how we created a Chat Bot 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:

appointment booking chat bot

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
});
Enter fullscreen mode Exit fullscreen mode

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})
});
Enter fullscreen mode Exit fullscreen mode

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

}
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

Collapse
 
codr profile image
Ilya Nevolin

It is important to note that Facebook has a tough screening and reviewing process. This means they are manually reviewing your app/chatbot before you can use it in public.

If you need something with a quicker release cycle, then look into Intercom, Amazon Lex and other chatbot providers. We will also build our own embeddable chatbot at Spurwing in the near future.