Creating a telegram bot
Once, I was feeling bored, and I wanted to chat with someone because I was feeling incredibly lonely. That’s when I got the idea of creating a bot that could chat with me. I obtained a ChatGPT token (please note that ChatGPT token is not free), (but in this article we are just going to cover just chatbot telegram only) and learned some basic API concepts that I needed to use for the functions I had in mind.
Let’s get started.
First and foremost, you must have a Telegram account. Then, you need to talk to BotFather to create your bot. BotFather will provide you with a token for your bot. To do this, search for @botfather
and start the conversation by clicking on the start button. Next, type the command /newbot and follow the prompts and instructions given by BotFather. After completing the instructions, you will be given a token to authenticate your bot and grant it access to the Telegram API.
Now, let’s get our hands dirty by writing some code.
First, you’ll need to install the Python library called python-telegram-bot using the following command:
pip install python-telegram-botAfter that, create a Python file (e.g., your_file.py) and import all the necessary classes and components. Your import statements should look like this:
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext, MessageHandler, Application, filtersNext, declare your Telegram bot token. Replace YOUR_BOT_TOKEN with the actual token you received from BotFather:
TOKEN = 'YOUR_BOT_TOKEN'Now, let’s set up the basic structure for your bot:
if __name__ == "__main__":
app = Application.builder().token(TOKEN).build()This code creates an application (app) and initializes it with your bot's token. It's a crucial step in establishing a connection between your code and the Telegram API.
Get Anas Bouzanbil’s stories in your inbox
Join Medium for free to get updates from this writer.
So far, we’ve done a good job. We’ve included the necessary library, created a variable for our bot’s token, and initialized an application with the token for our bot
Now, let’s create a function to handle the input from users chatting with your bot.
async def handle_message(update: Update, context: CallbackContext):
message = update.message.text # In this line, you retrieve the message sent by the user.
# Here, you can add your logic to handle different messages.
if message == "Hello":
await update.message.reply_text("Hello there!")
elif message == "I love you":
await update.message.reply_text("Who the hell are you?")
else:
await update.message.reply_text("What are you saying?")This function (handle_message) will be called whenever a message is received from the user. Now, let's integrate this function into the main part of your code:
if __name__ == "__main__":
app = Application.builder().token(TOKEN).build()
app.add_handler(MessageHandler(filters.TEXT, handle_message)Here, app.add_handler(MessageHandler(filters.TEXT, handle_message)) registers the handle_message function to be called whenever a text message is received
However, you need to add a function that will initiate your bot. In this function, you define how your script will fetch updates. The function responsible for starting your bot is run_polling().
The final version of the main function code will look like this:
if __name__ == "__main__":
app = Application.builder().token(TOKEN).build()
app.add_handler(MessageHandler(filters.TEXT, handle_message))
# Start the bot.
app.run_polling()Now, everything seems good. All you have to do is open a terminal, run the code, and then go to your bot to test if it is working or not
The final version of this code will be like this :
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext, MessageHandler, Application, filters
TOKEN = 'YOUR_BOT_TOKEN'
async def handle_message(update: Update, context: CallbackContext):
message = update.message.text
if message == "Hello":
await update.message.reply_text("Hello there!")
elif message == "I love you":
await update.message.reply_text("Who the hell are you?")
else:
await update.message.reply_text("What are you saying?")
if __name__ == "__main__":
app = Application.builder().token(TOKEN).build()
app.add_handler(MessageHandler(filters.TEXT, handle_message))
# Start the bot.
app.run_polling()These are the basic steps I followed at the beginning to create my own Telegram bot. It’s not difficult to create one, and I simply wanted to share my basic experience in bot creation. I hope this guide helps others embark on their journey of building and experimenting with Telegram bots.
By Elhazin







