Project: Location Sharing Telegram Bot
In this segment, I will be explaining how you can build a Simple Telegram Bot with Golang. Before I start, I will explain the package choice for the bot. Bot Code Examples if you are interested in building a Telegram Bot there are many supported languages on the official Telegram documentation.
However, for Golang there are two different packages available. The first go-telegram-bot-api and the other telebot. In my project, I'll be using telebot as I found that the package had all the functionality and was easier for me to understand.
First to setup the bot, you'll need to install the package.
Then you will need to seek the Botfather's permission to build a bot. Contact @botfather, use the command
/newbot and the BotFather will ask you for a name and username, if it is not already available it will prompt you to enter a new username. Once successful it will generate the API token for your bot.
For a start, the minimal code required to get the bot working based on Telebot configuration is:
So lets talk about how to build a bot that accepts a shared location and stores the data into memory. I'll skip the explanation of the package naming and imports and talk about the bot code itself. First the setup of the bot, starts with this portion of the code.
Here we have to initialize the new Bot. I left the
URL blank as it will be fetching information directly from Telegram API.
Verbose is set to True for debugging purposes to show in your terminal what information you are receiving from the response. Once it is in production you can remove
Verbose or set it to False.
Token is the API key generated from BotFather. Of course you won't want to publish it and let everyone know your code so you should save it in an
.env file. Becareful of this are you will also need to add a
.gitignore to prevent your
.env file from being uploaded too.
.gitignore file. Add the following to it:
Poller has been modified from the original
Timeout: 10 * time.Second to
Timeout: 60 * time.Second. That is because I do not want to be constantly updating every 10 seconds to the API. In
telebot example the purpose of poller can be scaled into existing bot infrastructure or chain them together with a middleware.
And lastly a good habit in Golang is to always check for errors. And because the function
tb.NewBot returns an error.
telebot has a myriad of handlers think of it as routes in a http service. You can refer to this list here which shows the supported telebot handles.
For this example, i will be using only 2 handles. The first handle will be called when a
user first starts the bot.
It will check if the chat is private, and then reply back to the
user with a response and create a callback to generate keyboard buttons for the
makeButtons() purpose is to create 2 buttons one to allow the
user to share their current location. And another to allow the user to get a text link to view a map. This is the only way to get location data from a user, based on my understanding of how telegram works.
Next we will need to handle the
user response for sharing the location data. As
telebot stores location information as a
float32 we will need to cast the value into a
float64 to ensure compatibility with a database. We can ignore the casting by commenting out this portion of the code.
As you can see, telebot will handle
tb.OnLocation events from the
*tb.Message structure of data. Once again check if the chat is private. Cast the value to float64, and initialize the data first by casting the value, and then appending it into the array.
We will talk about the Post structure later.
Next up, we need to create a model to represent the data we will need to perhaps store it somewhere. Referencing to the Telegram Message Documentation. You can find that there some basic information about how data is parsed.
Compare it with the Telebot Package, you can see some resemblance of the structure. So what we need is a portion of the message that contains the information of the location and possibily the ID of the user.
We will create a new go file called
model.go this will contain the structure of the data we will need and also store it in an array on memory. this is for testing purposes
In an actual application, you will want to store the data into a database. Which I will not be covering in this example.
So when we combine the entire codebase together you will have something like this.
So thats about how the entire bot should work together. Of course we are still lacking on other core components to make your bot work, like a database and a http server to manage the map service.