Creating a Telegram Bot: Full Beginner’s Guide 2021

Recently, I wanted to pick up python API development skills. I stumbled upon this great tutorial by Red Eye Coder Club. Four videos in this tutorial teach how to create a cryptocurrency bot which can to tell you a current bitcoin value right in the chat. Even if you are not interested in cryptocurrency, by following these steps, you will develop a fully operational bot with a webhook and backend logic that you will be able to adap to your needs later.

This tutorial has some blank spots, so in this article I will try to add all the missing pieces. Please make sure you complete all of the steps shown in the video. Write the code simultaneously with the narrator and this article will help you with bugs and missing info.

1. Environment setup

Installing Brew. This will be later needed to setup a virtual environment.

Copy-paste this line:

Installing virtualenv.

All set, now jumping back to the first video tutorial at 1:08.

  1. Create a folder that will serve as a repository for your bot🤖.
  2. Get into that folder on your terminal via “cd”. For example:

Then, for some reason, the command shown in the video did not work for me:

So, instead, I used these commands to get my virtual environment going:

This installs Python version 3.8 instead of 3.7 shown in the video, but don’t worry.

Following the video at 2:05:

At 2:29 mark, the video asks you to:

This is a command to open your bot directory in the Atom text editor.

I prefer using Visual Studio Code. If you are also using VS Code, make sure you added “Launching from command line” functionality as instructed here.

Then you will be able to do

Make sure you opened the bot in either Atom or VS Code before advancing to the next video.

You won’t be able to complete this project in one sitting, so to relaunch your local environment, run these commands:

2. Backend setup

The second video starts off by telling us how to get access to the crypto marketcap.

At 3:37, the video shows that the Documentation doc has a “Full Endpoints list” section; however, I was not able to find it anymore, so here please use the actual request url:

At 6:42, we are asked to run the script. To do that, you will need to install the scripting package to Atom first.

Go to Atom->Preferences->Install->Search ‘Script’->hit install.

Now when you navigate to the main.py file, you will be able to hit CMD+I to run it. The hotkey will be different on Windows and Linux.

To do the same in the VS Code, you will need to install a Python extension as advised here. Then you should be able to see a little green start button at your file. For some reason, I got this error in VS Code

To fix it, in the same terminal window in the VS Code, run:

You should be getting json response from the API in your editor now, so back to the video to finish setting up the backend for your telegram bot!

If you take a break and need to relaunch the environment:

3. Creating a Bot’s interface.

The third video, tells us how to start sending and receiving data in the telegram interface. At 0:55, the video shows us how to create a bot. Please choose the username carefully, as you won’t be able to change it in the future.

At 9:05, after running the script via CMD+I, the author views his server in the browser. You can do it by copy pasting the following line into your browser’s search bar:

At 9:37, we are asked to create a tunnel via serveo.net service. However, as of February 2021, this service is no longer available. I found that https://localhost.run/docs/ service is currently working which can do the same.

Here are the steps that you need to do before continuing with the video:

  1. Check if you have SSH key installed on your computer as explained here.

If you got any of the following in your output, you are all set:

  • id_rsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub

Make sure that you’ve added your SSH key to the ssh-agent according to this instruction or the tunnelling might not work.

If you don’t have these directories, then create an account at github.com and follow this instruction to generate a SSH key. Make sure that you’ve added your SSH key to the ssh-agent according to this instruction or the tunnelling might not work!

After configuring the SSH, you can run:

This should boot up your own server.

Now going back to 10:20 in the video to set up a webhook. At 11:09, make sure that you copy create a webhook url using the localhost.run service.

By default, localhost.run creates both http and https domains for your server, and if you copy the one from the browser, you will get the http one. So simply change the http to the https yourself.

Something like (I will cover the last 4 digits of my token):

Going back to 11:58. Make sure that your webhook stays connected.

For some reason I missed how some libraries were added in the video and got error. Pleas make sure that you’ve imported the necessary libraries:

Copy the steps in the video and you should be able to get your bot working!

To relaunch your bot after closing terminal:

4. Deploying your bot.

In the last video I have not encountered any difficulties but if you did, write a comment below and I will try to respond.

Conclusion

Congrats! You’ve completed the development of a telegram bot with a fully working Webhook and backend. This code serves as a great foundation for any of your future projects.

Product Manager, Drivers App. Wheely.com