For event-organizers and business-owners, providing quick 24/7 customer service can be tough. I help run a hackathon for women and non-binary people and our Facebook page has to respond to so many questions, like when the event is happening, if we're looking for new organizing team members, when hacker applications open, and more! Answering these can get repetitive and tedious--if only there was a way for us developers to automate the process!

Never fear, this step-by-step tutorial will show you how to build an intelligent Facebook Messenger bot with Twilio Autopilot, Functions, and SendGrid in Node.js. If the Autopilot assistant gets stuck and the user wants to speak to a human, the assistant will hand off the conversation to the business, connecting them with a human via Email for a seamless customer experience.

Setup

Before you get started, you will need to have a Facebook Page for your brand or business. If you don't have one, follow these instructions to create one. To link your Facebook page with Twilio, you must install Facebook Messenger for Autopilot. In the Configure tab, scroll down to Properties and check Use in -> Programmable SMS Inbound as shown below.

Scroll down a bit to the Credentials section and click the Facebook Login button to link. Follow the directions to link your Facebook page with your Twilio Account until you see a page like this:

Again under Credentials, select the Facebook page you want to build your Bot for and click Save . Now go to your Autopilot console and either make a new Autopilot Assistant by clicking the red plus button or select one you've already made. Click Channels on the left-hand panel and scroll down to select Facebook Messenger. Copy that Configuration URL below.

Paste the URL in the Callback URL field back on your Facebook Messenger Configuration page and then click Save. Now take your phone or laptop to visit https://messenger.com or the Facebook Messenger mobile app. Send any message to your Facebook page. You should see a reply "This is your new Task."

Lastly, you will need a SendGrid account to get an API key. In the Twilio Functions Configuration section, save it as the environment variable SENDGRID_API_KEY like so:

Now it can be referenced with context.SENDGRID_API_KEY in any of your Twilio Functions.

Make your First Task

Autopilot lets you perform different "actions" in whatever job you want to get done with it. You can use "Say" to communicate something to the user, "Handoff" to pass on the communication to a human, "Redirect" to hit a webhook to perform something different next, "Collect" to gather data, and more.

From the Autopilot console go to Task Builder for your Autopilot assistant. Replace the "This is your new Task" text with the following (the text below is for our women's hackathon page.)