Building and Deploying a Telegram bot in Golang with tbot.
Tags: telegram, heroku, go, bots
Published: 2021-12-20 15:48:26

---
title: Building and Deploying a Telegram bot in Golang with tbot.
published: true
date: 2019-07-22 19:55:39 UTC
tags: telegram,heroku,golang,bots
canonical_url: 
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c4lqa14pdgh9oe8uf9l5.jpg
---

[Telegram](https://telegram.org) 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](https://golang.org) 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](https://github.com/yanzay/tbot) library.

#### Prerequisites

First of all you will need to have Go 1.12+ installed. Go can be downloaded and installed from [here](https://golang.org/dl/). [This guide](https://golang.org/doc/install) will walk you through installing and setting up Go.

Next, we will use [Visual Studio Code](https://code.visualstudio.com). The Go extension for VS Code can be found [here](https://marketplace.visualstudio.com/items?itemName=ms-vscode.Go) or by using the in-built extensions tool. You may also use [Sublime Text 3](https://www.sublimetext.com/3) coupled with the [Golang Build](https://github.com/golang/sublime-build) package.

Optionally, you will need to have the [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli) and [Git](https://git-scm.com/downloads) 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/<your-username>/<your-project> in it, like so:

```
go mod init github.com/sixpeteunder/rochambeau
```

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](https://t.me/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](https://t.me/rochambeau_bot) (More on that [here](https://www.wrpsa.com/why-do-people-call-rock-paper-scissors-roshambo/)).

#### 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):

{% gist https://gist.github.com/sixpeteunder/825258193c577d4c176fa99c587098d5.js %}

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

![](https://cdn-images-1.medium.com/max/1024/1*LEOooGyYuaCYs_r8tkli-Q.png)

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

```
go run github.com/sixpeteunder/rochambeau
```

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:

![](https://cdn-images-1.medium.com/max/794/1*PXZRhpOEwPVPxSj7jVQOuQ.jpeg)
_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:

![](https://cdn-images-1.medium.com/max/1024/1*7G5rEw8b5fMyHJMXM6BkHg.png)
_main.go_

![](https://cdn-images-1.medium.com/max/1024/1*SliEUycJzomCzTRJu2d24g.png)
_handlers.go_

![](https://cdn-images-1.medium.com/max/1024/1*N-NXP_HG_OZ2LkUGKMqAFg.png)
_func.go_

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

![](https://cdn-images-1.medium.com/max/795/1*5e-YWZ5lErJtHaMUeOMFoA.jpeg)
_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](https://t.me/rochambeau_bot) and find all the source code [here](https://github.com/sixpeteunder/rochambeau).

#### 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:

{% gist https://gist.github.com/sixpeteunder/b90c07f9edebc7a6962b5f78058cf320.js %}

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:

{% gist https://gist.github.com/sixpeteunder/eca308d99364a87fba61c3cf57ca776c.js %}

```
git init
git add -A
git commit -m "First commit"
```

Next, login to your Heroku account via your terminal

```
heroku login
```

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
```

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:

![](https://cdn-images-1.medium.com/max/1024/1*DK_ODH3rHFirjCxFgpmnLQ.png)
_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
```

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

![](https://cdn-images-1.medium.com/max/1024/1*y1i9VLL2f_YbVP3cfI_eLw.png)
_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.