Building A Telegram Bot in 2024 With Python - by Erich Hohenstein - Level Up Coding
Building A Telegram Bot in 2024 With Python - by Erich Hohenstein - Level Up Coding
Open in app
38
Search
Get unlimited access to the best of Medium for less than $1/week. Become a member
https://levelup.gitconnected.com/building-a-telegram-bot-in-2024-with-python-17b483a7f6b9 1/15
2024/10/25 晚上9:38 Building a Telegram Bot in 2024 with Python | by Erich Hohenstein | Level Up Coding
Look
These last few weeks I have been getting to know more about how to use Large
language models (LLMs) in my own projects.
As you can see in this blog, I have found out that Ollama running Llama3 works
amazing in my Macbook Air M2.
thumbs up
https://levelup.gitconnected.com/building-a-telegram-bot-in-2024-with-python-17b483a7f6b9 2/15
2024/10/25 晚上9:38 Building a Telegram Bot in 2024 with Python | by Erich Hohenstein | Level Up Coding
1. Installations
This step is pretty simple. Just do:
2. The BotFather
The BotFather
This is the Bot manager that telegram has prepared for us to make our lives easier.
1. Hit start
https://levelup.gitconnected.com/building-a-telegram-bot-in-2024-with-python-17b483a7f6b9 3/15
2024/10/25 晚上9:38 Building a Telegram Bot in 2024 with Python | by Erich Hohenstein | Level Up Coding
3. Provide a name for the chatbot. It has to include the word “bot” in the name.
4. Once the bot name is accepted, you will receive an API TOKEN. Save it.
5. Message /setdescription and send the description for your bot. This will be shown
when users first open the chat.
It is pretty straight forward and you can make some modifications without knowing
how to code.
https://levelup.gitconnected.com/building-a-telegram-bot-in-2024-with-python-17b483a7f6b9 4/15
2024/10/25 晚上9:38 Building a Telegram Bot in 2024 with Python | by Erich Hohenstein | Level Up Coding
3. main.py
Create a new Python file and paste the following code:
https://levelup.gitconnected.com/building-a-telegram-bot-in-2024-with-python-17b483a7f6b9 5/15
2024/10/25 晚上9:38 Building a Telegram Bot in 2024 with Python | by Erich Hohenstein | Level Up Coding
if 'hi' in normalized_input:
return 'Hello!'
# Log errors
async def log_error(update: Update, context: ContextTypes.DEFAULT_TYPE):
print(f'Update {update} caused error {context.error}')
https://levelup.gitconnected.com/building-a-telegram-bot-in-2024-with-python-17b483a7f6b9 6/15
2024/10/25 晚上9:38 Building a Telegram Bot in 2024 with Python | by Erich Hohenstein | Level Up Coding
print('Starting polling...')
# Run the bot
app.run_polling(poll_interval=2)
4. BotFather II
Now, before running.
We need to set up the commands seen in the code: start and help.
These are all the “shortcuts” that your telegram bot shows whenever the user types
“\”.
This is what will be show as hints for the user whenever they check the available
commands.
5. Run main.py
Run the main.py file.
Once is running, if you go to your telegram app and chat with your new bot you can
try the following commands:
You can also send it any of the following messages and get a reply:
https://levelup.gitconnected.com/building-a-telegram-bot-in-2024-with-python-17b483a7f6b9 7/15
2024/10/25 晚上9:38 Building a Telegram Bot in 2024 with Python | by Erich Hohenstein | Level Up Coding
hi
6. Final comments
I was surprise how well prepared Telegram was for building chatbots.
Of course the chatbot example above is just a basic Telegram chatbot and it doesn’t
do much.
But in my following blogs, I will show how to integrate it with Ollama and Llama3 in
order to have an AI Chatbot right on Telegram.
Follow
Data Scientist | Python | Pandas | Data Mining | Web Scraping | Machine Learning | Power BI | NLP | SQL |
Tableau | HTML/CSS | DAX | Flask | Physics
Jun 14 68 8
Oct 9 2.1K 36
https://levelup.gitconnected.com/building-a-telegram-bot-in-2024-with-python-17b483a7f6b9 9/15
2024/10/25 晚上9:38 Building a Telegram Bot in 2024 with Python | by Erich Hohenstein | Level Up Coding
Oct 18 2.1K 41
https://levelup.gitconnected.com/building-a-telegram-bot-in-2024-with-python-17b483a7f6b9 10/15
2024/10/25 晚上9:38 Building a Telegram Bot in 2024 with Python | by Erich Hohenstein | Level Up Coding
Jun 29 51
Oct 15 220 2
https://levelup.gitconnected.com/building-a-telegram-bot-in-2024-with-python-17b483a7f6b9 11/15
2024/10/25 晚上9:38 Building a Telegram Bot in 2024 with Python | by Erich Hohenstein | Level Up Coding
Sep 8 149 1
Lists
https://levelup.gitconnected.com/building-a-telegram-bot-in-2024-with-python-17b483a7f6b9 12/15
2024/10/25 晚上9:38 Building a Telegram Bot in 2024 with Python | by Erich Hohenstein | Level Up Coding
Oct 7 769 5
Shivanshu Gupta
https://levelup.gitconnected.com/building-a-telegram-bot-in-2024-with-python-17b483a7f6b9 13/15
2024/10/25 晚上9:38 Building a Telegram Bot in 2024 with Python | by Erich Hohenstein | Level Up Coding
Oct 8 174 2
Oct 15 125 2
C. C. Python Programming
n8n is a powerful, low-code automation tool that’s rapidly gaining traction among businesses
and developers. With more than 220 pre-built…
Sep 10 57
https://levelup.gitconnected.com/building-a-telegram-bot-in-2024-with-python-17b483a7f6b9 15/15