DEV Community

Cover image for Building and Deploying a Telegram bot in Golang with tbot.
pete
pete

Posted on • Originally published at Medium on

Building and Deploying a Telegram bot in Golang with tbot.

Telegram is, in Telegram’s own words, a cloud-based mobile and desktop messaging app with a focus on security and speed. With its built-in “bots” and an open API, it seems to have been built from the ground up with developers and tinkerers in mind.

The Go Programming Language is, to once more use its developers’ own words, an open source programming language that makes it easy to build simple, reliable, and efficient software. It has been lauded for its speed and simplicity among other things.

The Bot

Telegram bots are extremely versatile and can have use cases that range from accepting payments for services to home automation. However, for the sake of this tutorial, we will build a bot that plays rock, paper, scissors with you, should you find yourself so lonely. For this task we will use the tbot library.

Prerequisites

First of all you will need to have Go 1.12+ installed. Go can be downloaded and installed from here. This guide will walk you through installing and setting up Go.

Next, we will use Visual Studio Code. The Go extension for VS Code can be found here or by using the in-built extensions tool. You may also use Sublime Text 3 coupled with the Golang Build package.

Optionally, you will need to have the Heroku CLI and Git installed, if you are interested in deploying your bot to the cloud.

Building the bot

Set up

I will build this project using Go modules. Therefore, you will need to create your project folder somewhere outside GOPATH, and run go mod init github.com// in it, like so:

go mod init github.com/sixpeteunder/rochambeau
Enter fullscreen mode Exit fullscreen mode

This will create go.mod and go.sum files in your project folder. With that set up, Go will automatically fetch all dependencies for you.

Next, you will need to have a chat with The BotFather. Send /newbot and follow the instructions to create your bot, after which you will be provided with a bot token. The bot I am going to build here can be found under the username @rochambeau_bot (More on that here).

Building the bot

First create a .env file to hold your token (you may want to exclude it from your version control system if you are using one):

Next, lets create a basic bot. Create a file main.go and add the following code to it:

Run the program using the go run command, like so:

go run github.com/sixpeteunder/rochambeau
Enter fullscreen mode Exit fullscreen mode

On the first build, the godotenv and tbot libraries will be downloaded to your computer.

Here, we have created a new tbot server, which we then set up to pass any message whose content matches “\start” to the startHandler function, which responds with some predefined text:


Talking to Rochambeau

Now let’s flesh out our bot and make it actually play the game. We will need least one more command to start the game, as well as some buttons for user input. We will also need a function that generates a random option for the bot and compares it to the players choice.

Let’s go ahead and implement this. For readability, I have split my code across three files; main.go, handlers.go and func.go. This may not be the best project structure but it will do, for the purpose of this tutorial. This is how my project looks as at now:


main.go


handlers.go


func.go

Your bot should now be able to play rock, paper scissors.


Playing against Rochambeau

I will leave it unto you to figure out how to add extra functionality to your bot. For my project, I have added the ability to keep track of scores. You can interact with the bot here and find all the source code here.

Optional: Deployment

We have built our bot, and it works fine, but only as long as your computer has an internet connection. If we want our bot to be available all the time, we will need to have it deployed somewhere in the cloud. I will use Heroku for this purpose.

First of all, add a Procfile to your project, like so:

Swap out “rochambeau” for the name of your project. Next, you will need to track your project using Git, if you were not already doing so. Remember to create a .gitignore file and add your .env to it:


git init
git add -A
git commit -m "First commit"
Enter fullscreen mode Exit fullscreen mode

Next, login to your Heroku account via your terminal

heroku login
Enter fullscreen mode Exit fullscreen mode

This will open a browser window, where you will be prompted to log in. After that is done, create and deploy your Heroku project using these commands:

heroku create rochambeau-bot
git push heroku master
Enter fullscreen mode Exit fullscreen mode

Before the bot is up and running, we will have to make one final modification to the code. In order to get Heroku to listen for updates, we will need to add a webhook to our bot. To do this, we edit our main function in main.go so that it looks like:


main.go — func main()

Swap out the link for the one you were provided by Heroku. Commit and deploy the changes to Heroku.

git commit -m "Added webhook"
git push heroku master
Enter fullscreen mode Exit fullscreen mode

Lastly, you will need to add your TELEGRAM_TOKEN to your Heroku environment before your bot can run properly.


Heroku settings page

That does it! Your bot should now be up and running. If you are on Heroku’s free plan, your bot might take a while to start up after being left idle for a while. Should you find anything that can be improved on or fixed, don’t hesitate to leave a comment, and I will try to reply to them all.

Oldest comments (0)