DigitalOcean Referral Badge
Udit Vashisht
Author: Udit Vashisht


How to create a Telegram Bot using Python?

  • 2 minutes read
  • 393 Views
How to create a Telegram Bot using Python?

    Table of Contents

Create a Telegram Bot using Python

In this article, we will learn to create a simple telegram bot using python. First of all, we will create the API token for our telegram bot.

Connect with FatherBot

We can do this by opening the BotFather bot on telegram. Yes, we will be using a bot created by Telegram to start creating our own Bot. For this, you must have telegram installed on your machine. If you have it already installed, click on this link to start with. This will open a new Telegram Bot chat and clicking on ‘start’ will give you the following screen.

SCR-20220707-hey.png

Create a new bot using the Father Bot.

Now send /newbot message to the Fatherbot and choose your bot’s name and user name and you will get your API and copy it.

SCR-20220707-hir.png

Code your first Telegram Bot in Python

Now, we will quickly jump to the coding part of this tutorial. It is convenient to use a virtual environment (Blogpost/Video) for your project and we would be using python-decouple to save our token key securely instead of hard coding it in the code itself.

Once, you have created a desired folder and activated the virtual environment, you can install the requisite modules using the following command

pip install pyTelegramBotAPI python-decouple

Once, this is done, create a new .env file in the same folder, open it in your favorite editor and add your api-key in it:-

API=YOURAPIKEY

Now, create a new python file and start coding

import telebot
from decouple import config

#getting the key from .env file
API_KEY = config('API')

#creating the bot

bot = telebot.TeleBot(API_KEY)

#passing the 'hello' command to the bot

@bot.message_handler(commands=['hello'])

#getting the bot to output "Hello!"

def hello(message):
    bot.send_message(message.chat.id, "Hello! I am a bot created by SaralGyaan")

#looking out for messages
bot.polling()

The code above is self explanatory, now run the python file to start the bot and then open telegram on your phone or device and search for your bot with name or id which you created. For me, it is saralgyaan-bot or saralgyaan_bot. Send ‘/hello’ to your bot to get the reply

SCR-20220707-il6.png


Related Posts

Matplotlib Subplot in Python | Matplotlib Tutorial | Chapter 10
By Udit Vashisht

Matplotlib Subplot in Python | Chapter 10

In this Matplotlib Subplot tutorial, we will be learning to create Matplotlib Subplots. Till now, we have been using matplotlib.pyplot() to create the plots. But now, we will be using matplotlib.pyplot.subplots() (ptl.subplots()) to create Matplotlib Subplots.

Matplotlib.pyplot() or plt was automatically ...

Read More
Chapter 6 - Data Types & Variables
By Udit Vashisht

Variables and Identifiers in Python

If we go by the dictionary meaning ‘Variable’ is something which is ‘able to be changed or adapted’. Which is true to much extent in terms of Python programming language also. Variable is basically a reference to the memory location where an object is ...

Read More
Chapter 2 - Quick setup
By Udit Vashisht

How to install Python?

It doesn’t matter which OS you are using, Python3 comes with multiple OS compatibility.

Install Python on Windows PC

Follow the steps in this post to install Python on Windows PC.

Install Python on MacOS/Linux

Follow the steps in this post ...

Read More
Search
Tags
tech tutorials automate python beautifulsoup web scrapping webscrapping bs4 Strip Python3 programming Pythonanywhere free Online Hosting hindi til github today i learned Windows Installations Installation Learn Python in Hindi Python Tutorials Beginners macos installation guide linux SaralGyaan Saral Gyaan json in python JSON to CSV Convert json to csv python in hindi convert json csv in python remove background python mini projects background removal remove.bg tweepy Django Django tutorials Django for beginners Django Free tutorials Proxy Models User Models AbstractUser UserModel convert json to csv python json to csv python Variables Python cheats Quick tips == and is f string in python f-strings pep-498 formatting in python python f string smtplib python send email with attachment python send email automated emails python python send email gmail automated email sending passwords secrets environment variables if name == main Matplotlib tutorial Matplotlib lists pandas Scatter Plot Time Series Data Live plots Matplotlib Subplots Matplotlib Candlesticks plots Tutorial Logging unittest testing python test Object Oriented Programming Python OOP Database Database Migration Python 3.8 Walrus Operator Data Analysis Pandas Dataframe Pandas Series Dataframe index pandas index python pandas tutorial python pandas python pandas dataframe python f-strings padding how to flatten a nested json nested json to csv json to csv python pandas Pandas Tutorial insert rows pandas pandas append list line charts line plots in python Django proxy user model django custom user model django user model matplotlib marker size pytplot legends scatter plot python pandas python virtual environment virtualenv venv python python venv virtual environment in python python decorators bioinformatics fastafiles Fasta python list append append raspberry pi editor cron crontab Cowin Cowin api python dictionary Python basics dictionary python list list ios development listview navigationview swiftui ios mvvm swift environmentobject property wrapper @State @Environm popup @State ios15 alert automation instagram instaloader texteditor youtubeshorts textfield multi-line star rating reusable swift selenium selenium driver requests-html youtube youtube shorts python automation python tutorial algo trading nifty 50 nifty50 stock list nifty50 telegram telegram bot dictionary in Python how to learn python learn python