Creating a Telegram Bot: Full Beginner’s Guide 2021 | by Evgeny Pavlov | Medium – Telegram 安装

Haley

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:

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Installing virtualenv.

$ brew install python3
$ pip3 install 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:
$ cd /Users/evgenypavlov/Documents/telegrambot 

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

? $ virtual venv --python=python3.7

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

$ virtualenv -p python3 /Users/evgenypavlov/Documents/telegrambot$ source /Users/evgenypavlov/Documents/telegrambot/bin/activate

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

Following the video at 2:05:

$ pip install flask requests$ mkdir bot
$ cd bot

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

atom .

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

code .

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:

$ cd /Users/evgenypavlov/Documents/telegrambot$ cd bot$ atom .CMD I in Atom to start the server

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:

https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest

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.

Installing scripting package

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.

Running the script

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

VC Code parsing error

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

sudo pip3 install requests

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:

$ cd /Users/evgenypavlov/Documents/telegrambot$ cd bot$ atom .CMD I in Atom to start the server$ virtualenv -p python3 /Users/evgenypavlov/Documents/telegrambot$ source /Users/evgenypavlov/Documents/telegrambot/bin/activate

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 usernamecarefully, 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:

http://localhost:5000/

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.
$ ls -al ~/.ssh

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:

sudo ssh -R 80:localhost:5000 localhost.run

This should boot up your own server.

Tunnel server boot up.
Server coming live.

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

https://api.telegram.org/bot1698970219:AAFDYe4Gm7NFSphXKGeRHYTh4QiAYcQ****/setWebhook?url=https://root-164b3679.localhost.run

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:

import json
import reimport requestsfrom flask import Flask
from flask import request
from flask import Response
Importing libraries

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

A working bot

To relaunch your bot after closing terminal:

$ cd /Users/evgenypavlov/Documents/telegrambot$ cd bot$ atom .CMD I in Atom to start the server$ virtualenv -p python3 /Users/evgenypavlov/Documents/telegrambot$ source /Users/evgenypavlov/Documents/telegrambot/bin/activate$ ssh -R 80:localhost:5000 localhost.runIn your browser to connect to a webhook again:
https://api.telegram.org/bot1698970219:AAFDYe4Gm7NFSphXKGeRHYTh4QiAYcQ****/setWebhook?url=https://your-localhost-url

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.

Ten articles before and after

819 Milliseconds to Minutes | – Telegram 下载

8.9 Milliseconds to Minutes | – Telegram 下载

200 Milliseconds to Minutes | – Telegram 下载

86000 Seconds to Hours | Telegram

72 Months to Years | – Telegram 下载

1095 Minutes to Days | Telegram

775 Minutes to Days | Telegram

How to Scrape and Extract Members from Telegram Group | by SoftwareReview | Best Software for PC & Mac | Medium – Telegram 安装

即使被屏蔽,Telegram 在伊朗也拥有4500万用户 | – Telegram 安装

Telegram 秘密聊天對話如何設定?限時訊息閱後即焚在哪裡?這裡教你如何設定 – – Telegram 安装

About Me

Pretium lorem primis senectus habitasse lectus donec ultricies tortor adipiscing fusce morbi volutpat pellentesque consectetur risus curae malesuada dignissim lacus convallis massa mauris.