How to create a Telegram bot with Python in under 10 min! | by Deniz Shabani | Medium – Telegram 安装

Haley

How to create a Telegram bot with Python in under 10 min!

This article explains in easy steps how to make your own Telegram bot.

All you need are the following:

  • Telegram account: if you do not have one already all you need to do is download Telegram on your phone and follow the steps to create your account. It should not take more than 2 minutes.
  • Environment for Python: you will need an integrated development environment(IDE) which supports Python. If you are a student you can use PyCharm free of charge with your student email. If you are not a student and do not want to spend money, then you should download Visual Studio Code. It is very easy to install and use. Your python version should be at least 3.6.8+ in order to support the modules that are be used. If you are installing it for the first time, then I would recommend to install the latest stable version.

Once you have finished the previous two steps then you are ready to start with making your very first Telegram bot.

Steps to create your bot:

  • Open Telegram and type on the search bar at the top “BotFather”
  • Click on the first result (the one with the blue tick) and press start.
  • You can either click on /newbot or type it and press send.
  • Afterwards you are asked to name the bot. You can choose anything.
  • Then you are required to choose a username. A username is a unique identifier for user on Telegram. The only thing you should be careful of is that the username must end in “bot”, as shown in the picture below.
  • After you choose a username which is not taken, you receive what is called the HTTP API. This is used by the Python scripts to communicate with your bot.

Now if you search for your bot in the search bar it should already be visible. However, since it is not yet configured the bot is not able to do anything. I am using PyCharm in my example, however, it is the same for Visual Studio Code as well.

Configuration steps for the bot:

  • Open your IDE and on the terminal at the bottom type

If you do not have pip installed on your computer then here is a very good explanation on how to install it.

  • Importing required libraries:

If you do not care about the explanation then just skip to the next step.
Updater: Contains the API key received from BotFather to specify which bot we use and want to add functionalities to.
Update: Invokes every time a bot receives an update: i.e. message or command and will send the user a message.
CallbackContext: in this example it is required when adding the dispatcher and works internally
CommandHandler: used to handle any command sent by the user to the bot. A command always starts with “/” i.e “/start”.
MessageHandler: used to handle any normal message sent by the user to the bot
Filters: filters normal text, commands, images, etc from a sent message.

  • Define functions for operation

updater: here you should input the API key received from BotFather.

start function: Displays the first message sent by the bot whenever the user presses “start”.

help function: here you should put information to help the user understand what the bot is capable of, i.e all the commands the bot supports.

  • Adding more functionalities to the bot

These are only optional commands. They are used as an example to show what the bot can do. You can add as many functions as you want and you can put as a reply text anything that you want. The unknown_text function is used when the bot receives some unknown messages and the unknown function is used to filter out all the unknown commands sent by the user and respond with the reply message in both cases.

  • Adding Handlers to handle our messages and commands

Whenever a user writes a command(the first parameter of CommandHandler) then the bot will respond with the reply message written inside the functions we wrote previosly(the second parameter of CommandHandler).

  • Running the bot
updater.start_polling()

This command will start the bot and you will be able to use it on Telegram. The bot only runs for as long as you leave the script running as well. If you wish to leave the bot running at all times then you can either leave the script running at all times, or the better way is for you to rent a server which would host your bot 24/7.

If you just want to copy paste the whole code to see how it works without understanding it first here is the full implementation:

If you liked this post and want to see more like this then buy me a coffee 😀

Ten articles before and after

452 Milliseconds to Minutes | – Telegram 下载

Telegram bot for Youtube learners: taking notes with subtitles | by Alexander Prismotrov | May, 2022 | Medium – Telegram 安装

Tools for Managing Chats and Channels in the Telegram | by KlaTel — Telegram, chatbots and more | Mar, 2022 | Medium – Telegram 安装

644 Minutes to Days | Telegram

Best Adult Telegram Groups to join in 2022 – Telegram Channels

中国手机号注册telegram账号的若干问题 | – Telegram 安装

TRX1 Dev Blog #9 (September 2021) | by Maksim Koltsov | Medium – Telegram 安装

1144 Minutes to Days | Telegram

Делаем бота для учета личных расходов на Python используя Google Spreadsheets | by Jamshed Djuraev | Medium – 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.