DigitalOcean Referral Badge
Udit Vashisht
Author: Udit Vashisht


How to create a Telegram Bot using Python?

  • 2 minutes read
  • 45 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

Plot Time Series in Python | Matplotlib Tutorial | Chapter 8
By Udit Vashisht

Plot Time Series data in Python using Matplotlib

In this tutorial we will learn to create a scatter plot of time series data in Python using matplotlib.pyplot.plot_date(). We will use Pandas Dataframe to extract the time series data from a CSV file using pandas.read_csv().

The ...

Read More

Matplotlib Tutorial in Python | Chapter 1 | Introduction
By Udit Vashisht

Matplotlib Tutorial in Python

In this series of Matplotlib Tutorials in Python, we will cover all the concepts from beginners to expert level. Starting with how to install Matplotlib library to how to create the plots, this series is an exhaustive tutorial and by the end of this series you ...

Read More
What is a Dictionary in Python?
By Udit Vashisht

What is a Dictionary in Python?

Python Dictionary or Dictionary in Python is a data-structure in Python that is also known as associative array. A python dictionary is used to store data values in key:value pair. Each item has a key and a corresponding value that is expressed as a ...

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