Go Telegram Bot
Learn how to deploy a Go Telegram Bot from GitHub.
Deploy a Go Telegram Bot and learn how to host backend code on Code Capsules.
Register the Bot
Before you can create a Telegram bot, you need a Telegram user account. Open Telegram and create an account if you don't already have one.
When you've signed in to Telegram, search for BotFather (a bot for managing all other Telegram bots) and start a new chat with it.
Follow the steps below to register a new bot with the BotFather:
Type
/startand press send.Type
/newbotand press send.Choose a name for your bot.
Choose a username for your bot that ends in
bot.
The BotFather will respond with a message containing an access token for your newly created bot. This access token allows our application to:
Access the Telegram API.
Tell our bot what to do when receiving different messages from users.
To confirm that your bot has been created successfully, search for the bot's username. You should be able to see it and start a conversation with it. However, it won't respond because we haven't written the bot's logic yet.
Setup
Code Capsules connects to GitHub repositories to deploy applications. To follow this guide, you need a Code Capsules account and a GitHub account.
To demonstrate how to deploy a Go Telegram Bot with Code Capsules, we've provided an example bot in the Code Capsules GitHub repository.
Sign in to GitHub, and fork the example bot repository by clicking Fork at the top right of your screen and selecting your GitHub account as the destination.
Create a Space for Your Bot
Log in to your Code Capsules account and navigate to the Spaces tab. Then, click the yellow + icon at the bottom left of the screen to add a new Space.
Follow the prompts to choose your region and give your Space a name, then click Create Space.

Example instructions to go with numbered annotations:
Choose a Team. You can use a default "personal" Team if you're the only person working on this project, or a named Team if you're collaborating with others.
The name should remind you of the project, for example,
customer-apiornotetaking-app.Choose a country close to where most of your users reside.
Create the Capsule
A Capsule provides the server for hosting an application on Code Capsules.
To create a new Capsule for your Space, follow the instructions below:
Click the add Capsule + button in your Space.
Choose Backend for the Capsule type, and select your Team and Space, if not already populated.
Choose your payment plan and click Next.
Click the Configure Git for Code Capsules button and grant access to your forked
go-telegram-echobotrepository.Press Next.
Leave the Run Command blank.
Click Create Capsule.
Code Capsules automatically builds your application when you've finished creating the Capsule.
Once your application is live, you can view the build logs by selecting the Deploy tab and clicking the View build log link in the Builds section.

Add Environment Variables
Once the build is complete, we need to add the BOT_TOKEN and URL environment variables on the Config tab, under the Environment Variables section.
First, name the BOT_TOKEN variable and enter your Telegram access token (which you received from the BotFather when you registered the bot) as its Value.

Name the URL variable and set your bot's domain as its Value.
Get the domain by clicking the Capsule's Details tab and copying the URL in the Public URL section.
Paste the URL in the Value field.
Make sure the URL ends with a
/or the webhook will not be valid.

Confirm your changes by clicking on Save, then restart your Capsule by toggling the radio button in the top-right corner off and on again.
Set Up Webhook
The next step is to set up a webhook for your bot:
Click the Public URL link in your Capsule's Details tab.
In the new tab that opens, add
/setwebhookto the URL and pressenter/returnto visit the URL.If you see
webhook setup ok, then your bot is ready to chat!
Chat with the Bot
The bot can now respond to messages! Search for your bot on Telegram using the username you assigned it, and start a chat with it. The bot is programmed to respond to /start and echo any messages you send it.
If you'd like to deploy another application in a different language or framework, take a look at our other deployment guides.
Last updated
Was this helpful?