Build Basic Chatbot with Golang & Messenger

Chatbot with Golang & Messenger

Introduction

Hi everyone! Still with me to share some experience, thought, or opinion about technology-related with the software engineering field. In this article, I want to share how to create a simple Chatbot on Messenger using Golang!

To give some context to what we should do, here are several points we will handle:

  • Have a Facebook account or create a new one.
  • Create a Facebook page.
  • Create a Facebook app (should be auto-created).
  • Create a webhook server.

Architecture Design

To receive a message sent by the end-user to the Messenger (Facebook page), we need to provide a Webhook server with REST API. The architecture design will look like this diagram:

Chatbot Architecture Design

Facebook Account

I guess nowadays people have a Facebook account. However if not, just visit https://www.facebook.com to easily Sign Up by clicking Create New Account button.

Create a Facebook Account

Facebook Page

Creating a Facebook page can be done through the menu on the homepage.

Facebook Page Menu

Then click on the Create New Page button to go to a Create a Page Form.

Create New Page Menu

Or just open this link https://www.facebook.com/pages/create/?ref_type=site_footer to directly go to Create a Page Form. Fill in the necessary information and finish it by clicking Create Page button.

Create New Page Form

In the page list, a new Page will be displayed.

New Page Created

Facebook App

Next, go to Meta for Developers page here https://developers.facebook.com and choose the My Apps menu.

Meta For Developers Page

Create a new app by clicking Create App button.

Create App

Choose Business type and click the Next button.

Choose App Type

Add details information of the App, last step click Create App to proceed.

Proceed Create App

Wait for a while, the page will be redirected to the detailed App after successfully created.

App Successfully Created

Webhook

Before adding Messenger & Webhook products into Facebook App, we can prepare a Webhook just to validate the callback URL. For the detailed info please refer to this documentation:

Because we will use Go for this tutorial, better to understand Go with this link:

Then, the example for Go code can be like this:

Actually, we need to deploy the live Webhook that can receive webhook events from the Messenger Platform. The code must be hosted on a public HTTPS server. But we can do it locally and make a tunnel from the public URL to our localhost using ngrok. To install and use ngrok please refer to this link:

First, run the Webhook server using this command:

go run .

Webhook will run on port 3000.

2022/01/03 00:36:30 http server listening at localhost:3000

Then, enabling the tunnel is very easy, just run this command:

ngrok http 3000

If ngrok successfully enables the tunnel, the output will look like this:

ngrok Enabling Tunnel

Test the tunnel by doing this simple curl:

curl --location --request GET 'https://e52b-2001-448a-400d-238a-d170-291d-3057-4e4a.ngrok.io/?hub.verify_token=12345&hub.challenge=challenge'

When the output same as hub.challenge the query param, then it’s working!

challenge⏎

Configure Messenger Product

After the Webhook is ready, the next step is to set up the Messenger Product. Find the Messenger product and click on the Set Up button.

Set Up Messenger Product

Linked the Messenger setting with the Facebook page that we’ve created from the previous section.

Add Facebook Page
Choose Facebook Page

Add the Callback URL using ngrok HTTPS URL to the Callback URL setting form.

Add Callback URL
Fill URL and Token

Because we will receive messages from the Messenger platform, add a subscription for the messages field.

Add Message Field Subscription

Use the same Facebook account that created the Facebook App to send a message to the Facebook Page messenger (we also can invite the other users to test this app). If our Webhook successfully received the message, it’s mean our Webhook is working!

Testing Receiving Message

Configure Webhook Product

The next part is sending the reply message from the end-user. In this sample, the configuration for Webhooks setting automatically filled after finishing the Messenger settings (with subscribing to message field). Just in case our configuration is not automatically set up, please refer to these settings.

Webhooks Configured

Generate Access Token

Based on this Send API documentation, we need Page Access Token included in the request in terms of sending the message.

Then, we need to go to the Messenger Settings page and do Generate Token. The token will hide after we close the pop-up. That’s why better to copy the token and save it.

Generate Token

Modify Webhook

To send a message to a specific user, we need the Token generated from the previous section and make an HTTP request to Facebook API. The final code will look like this:

The code above is just a simple Chatbot that will always send “Automatically Reply 🙌🏻” messages whatever type of messages are sent from the end-user (text, sticker, emoji, gif, files). Here is the result of the final code, after sending the message we got the reply! Yeay it’s working!!

Webhook Sent Reply Message

Closing

Building a Chabot for Messenger Platform is quite simple because there is a lot of well-written documentation from the official site. Also, no limitation for us to create, access Page and App so we can easily test our Chatbot.

Although the scope of this article is just limited to replying to the message, there is a lot of Messenger feature available so I hope you can explore more by yourself!

Thank you for reading! Hopefully, this article is useful for you!!

--

--

--

Software Engineer | Traveler | Guitarist | J-Lovers

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How Load Testing Brings Developers & Marketeers Together

Working Together with Git!

Breakdown into manageable task and use 5S rule to get started!

R and Python Plotting in Google Sheets

Deploying Clojure jars to Github Packages with Github Actions

Difference between static and dynamic libraries

Dan Finlay of Metamask — Volume 2 Ep 02 Recap

Learning Web Development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bismo Baruno

Bismo Baruno

Software Engineer | Traveler | Guitarist | J-Lovers

More from Medium

BasicAuth for APIs in GO

[Crazy Go Day]Access caching : Go-Gin + Redis

Build a REST API with Golang and MongoDB — Fiber Version

How To Install And Setup Go Language On Ubuntu (Complete Guide)

How To Install And Setup Go Language On Ubuntu (Complete Guide)