Build Basic Chatbot with Golang & Messenger
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.
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:
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.
Creating a Facebook page can be done through the menu on the homepage.
Then click on the Create New Page button to go to a Create a Page Form.
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.
In the page list, a new Page will be displayed.
Next, go to Meta for Developers page here https://developers.facebook.com and choose the My Apps menu.
Create a new app by clicking Create App button.
Choose Business type and click the Next button.
Add details information of the App, last step click Create App to proceed.
Wait for a while, the page will be redirected to the detailed App after successfully created.
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:
Webhook Setup - Messenger Platform - Documentation - Facebook for Developers
The Messenger Platform is Language Agnostic To follow this guide, you will need to install Node.js, but you can write…
Because we will use Go for this tutorial, better to understand Go with this link:
The Go Programming Language
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
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:
ngrok - secure introspectable tunnels to localhost
ngrok secure introspectable tunnels to localhost webhook development tool and debugging tool
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:
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!
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.
Linked the Messenger setting with the Facebook page that we’ve created from the previous section.
Add the Callback URL using ngrok HTTPS URL to the Callback URL setting form.
Because we will receive messages from the Messenger platform, add a subscription for the messages field.
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!
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.
Generate Access Token
Based on this Send API documentation, we need Page Access Token included in the request in terms of sending the message.
Send API - Messenger Platform - Documentation - Facebook for Developers
The Send API is the main API used to send messages to users, including text, attachments, structured message templates…
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.
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!!
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!!