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 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
Python object-oriented programming (OOP) - A complete tutorial
By Udit Vashisht

Python object-oriented programming (OOP)

Object-oriented programming

Object-oriented programming also known as OOP is a programming paradigm that is based on objects having attributes (properties) and procedures (methods). The advantage of using Object-oriented programming(OOP) is that it helps in bundling the attributes and procedures into objects or modules. We can ...

Read More
Matplotlib Tutorial in Python | Chapter 2 | Extracting Data from CSVs and plotting Bar Charts.
By Udit Vashisht

Matplotlib Tutorial in Python

Chapter 2 | Extracting Data from CSVs and plotting Bar Charts

In the last chapter, we learned to draw simple plots in Matplotlib and further customizing it.In this chapter we will be learning to extract data from external sources like CSV and creating ...

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