Creating A Chatbot Using Amazon Lex Service - Day Four - Facebook Messenger Integration

This is the last article of this series where we’ll see how to integrate the bot with Facebook Messenger and make it live with Messenger.

Introduction

In the last three articles of this series, we tried to create a chatbot using Amazon Lex services, we used AWS lambda functions and Slots to make our chatbot more interactive and richer. The concepts were introduced so that these services could be further leveraged to make a custom bot aligning to business needs. This is the last article of this series where we’ll see how to integrate the bot with Facebook Messenger and make it live with the Messenger.

Series Info

Following is the four-tutorial series to learn how to develop a chatbot using Amazon Lex services and deploy the same on Facebook Messenger.

  1. Creating a Chatbot using Amazon Lex Service: Day 1 (Initial Bot)
  2. Creating a Chatbot using Amazon Lex Service: Day 2 (AWS Lambda Functions)
  3. Creating a Chatbot using Amazon Lex Service: Day 3 (Slots in AWS Lex)
  4. Creating a Chatbot using Amazon Lex Service: Day 4 (Facebook Messenger Integration)

Deploy the Chatbot in Facebook Messenger

Our initial basic bot is completed, you can enhance it more as per your desire and requirement. In this section, we’ll see how to deploy the bot on Facebook messenger as a chatbot to your custom Facebook page.

So, the need here would be that suppose you have a Facebook page for your business or company and you need to deploy a feature of auto chatbot in that page so that any user can seek information or talk to that bot.

First things first, for deployment of the bot to the Facebook page, we need to have a Facebook page.

  1. Open the URL https://developer.facebook.com and sign in with your existing Facebook credentials or create a new account

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. I am signing in with my existing Facebook account. It will ask to create a Facebook developers account, and yes, we need to, so click Next.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. Provide the App Name (whatever you want) and contact email and click Next.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. Pass the security check and click Submit.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. It will show the welcome message and ask to add your first product. Click on “Add Your First Product” button.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. The next screen you’ll see is the Facebook developer’s dashboard. Since we need to set up the Facebook Messenger, click on the button to setup messenger as shown below.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. You need to have an access token and a secret key for that. So, navigate to Settings and scroll down to the section that says Token Generation. Select an existing page if you have one or create a new one as per your wish.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. Select the type of page you want to create; i.e. Community or Business.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. Provide the page name and the category.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. You can skip the steps like providing profile pic or choose to apply one.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. In the next step, you’ll see the page created.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. Now again go to settings and Token generation section and choose the newly created page.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. It will again prompt you for authentication. Do that.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. After the authentication, in the Token Generation section, we see now a Page Access token is generated. Copy that token and keep that at some location. We’ll soon need that.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. No, go to Settings->Basic to get the App Secret key as shown below. Copy and keep the App Secret key at some safe location, we’ll need that soon.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. Go back to the Amazon Lex service, it's time to publish your bot to the Facebook Messenger for the newly-created Facebook page. Click on Publish on the BotService.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. It will ask for the alias. Provide the alias name of your choice and remember it or store it somewhere.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. Click on publish, which may take some time.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. Once the bot is published, you’ll see the confirmation message and a blue colored button which says, “Go to channels”.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. This will show up different channels like Facebook, Kik, and Slack. Since we need to integrate that bot to Facebook, choose that. Provide the name of the channel, the alias that we created while publishing. Give a token name and remember it or store it. Provide the saved access token and the API secret key that we stored earlier.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. Click on Activate after providing all the information on that page.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. Once activated, you’ll get the Callback URL at the bottom itself. Copy that URL and save it.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. Again, go back to Facebook’s developer's account and click Settings. Go to Webhooks section and click Setup Webhooks to provide the webhook to that copied call-back URL in the last

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. In the New Page Subscription; i.e., the page that opened after clicking setup webhooks, provide the Callback URL and verify token name that we stored earlier. Check the options like messages, postbacks, and options as shown below. Click "Verify and Save".

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. Once the webhook setup is complete, select the page to subscribe to webhook events. So select your Facebook page there.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. Click on the “Subscribe” button.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. Once done, go back to the Facebook page and add a new button by clicking “Add a button” button as shown below.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. Provide details for all the steps; i.e. selecting the contact you feature for my messenger type. Select the option to send messages.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration

Live Chatbot on Facebook

After performing all the steps in the last section, we can say that our bot is successfully deployed and integrated with our Facebook page. Time to test it.

  1. Click on newly added “Send Message” button and from the context menu click “Test Button”.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. As soon as the Test Button link is clicked, the chatbot will open and note that the name of the chatbot is CodeBusiness. You type Hi, and it shows the message we provided in the error handling section.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration
  1. You type the utterances in the first intent and it replies back with what we tested in the Bot Service.

    Creating A Chatbot Using Amazon Lex Service - Facebook Messenger Integration

Yippie! Our bot seems to be working well. Like-wise you can test everything that we tested on the BotService on Lex like talking to a human, seeing slot information and more. You can also provide the tester permissions to the user you want to see this bot when they see your page on Facebook. I find it very cool.

Conclusion

In this detailed article series, we learned what a chatbot is, and how to create your own bot using Amazon’s Lex service. Deploy and integrate the bot with the Facebook messenger. The bot integration could also be done to other platforms like Slack, Twilio etc. It's just a matter of exploring and playing with it.