How To Create A Telegram Bot Using Python
How To Create A Telegram Bot Using Python
3. Type /newbot , and follow the prompts to set up a new bot. The
BotFather will give you a token that you will use to authenticate your
bot and grant it access to the Telegram API.
Forum Donate
Note: Make sure you store the token securely. Anyone with your
token access can easily manipulate your bot.
Next, open your favorite code editor and create a .env file to store
your token as below:
export BOT_TOKEN=your-bot-token-here
After that, run the source .env command to read the environment
variables from the .env file.
Create a new bot.py file and paste the following code there:
import os
import telebot
BOT_TOKEN = os.environ.get('BOT_TOKEN')
bot = telebot.TeleBot(BOT_TOKEN)
In the above code, we use the os library in order to readForum
the Donate
environment variables stored in our system.
Support our charity and our mission. Donate to freeCodeCamp.org.
@bot.message_handler(commands=['start', 'hello'])
def send_welcome(message):
bot.reply_to(message, "Howdy, how are you doing?")
Let’s add another handler that echoes all incoming text messages back
to the sender.
You now have a simple bot that responds to the /start and /hello
commands with a static message and echoes all the other sent
messages. Add the following to the end of your file to launch the bot:
bot.infinity_polling()
That’s it! We have a Telegram bot ready. Let’s run the Python file and
go to Telegram to test the bot.
Search for the bot using its username if you’re unable to find it. You
can test it by sending the commands like /hello and /start and
other random texts.
Forum Donate
Note: All the message handlers are tested in the order in which they
were declared in the source file.
We are going to use the Horoscope API that I built in another tutorial.
If you wish to learn how to build one, you can go through this tutorial.
Make sure you explore the APIs here before getting started.
import requests
return response.json()
If you test the function, you will get an output similar to below:
Forum Donate
{
Support
"data":{our charity and our mission. Donate to freeCodeCamp.org.
"date": "Dec 15, 2022",
"horoscope_data": "Lie low during the day and try not to get caught
},
"status": 200,
"success": true
}
Note: You can explore more about the requests library in Python in
this tutorial.
@bot.message_handler(commands=['horoscope'])
def sign_handler(message):
text = "What's your zodiac sign?\nChoose one: *Aries*, *Taurus*, *Gem
sent_msg = bot.send_message(message.chat.id, text, parse_mode="Markdo
bot.register_next_step_handler(sent_msg, day_handler)
def day_handler(message):
sign = message.text
text = "What day do you want to know?\nChoose one: *TODAY*, *TOMORROW
sent_msg = bot.send_message(
message.chat.id, text, parse_mode="Markdown")
bot.register_next_step_handler(
sent_msg, fetch_horoscope, sign.capitalize())
This is the final function where we get the sign from the function
parameter and the day from the message.text attribute.
Bot Demo
Once you run the Python file, you can test this functionality. Here’s the
demo:
Here's a link to the GitHub repo for this project - feel free to check it
out.
You can also add more functionalities to the bot by exploring the
Telegram APIs.
Ashutosh Krishna
Application Developer at Thoughtworks India
If you read this far, thank the author to show them you care.
Say Thanks