python-telegram-bot development experience and notes
This blog post is trying to tell you:
- My personal experience and notes when using python-telegram-bot
- Explained with examples from @Mensaar_Bot and @udsfahrbot
python-telegram-bot development experience and notes
This blog post is trying to tell you:
- My personal experience and notes when using python-telegram-bot
- Explained with examples from @Mensaar_Bot and @udsfahrbot
A lightweight Telegram bot designed for Saarland University students — offering fast, clear, and focused public transport information using SaarVV and HAFAS APIs. — Try Now
UdS Fahrplan is a minimal alternative to the full Saarfahrplan app. No clutter. No overwhelming options. Just a clean Telegram interface for:
/trip
/depart
/home
Built with students and locals in mind.
MensaarLecker Development Log (3) -- Telegram Bot Deployment and Integration
This blog post is trying to tell you:
- My personal experience when developing a web crawler using Selenium
- Explained with examples from my Repository: MensaarLecker
- For further details, feel free to **Try the bot: @Mensaar_Bot
Previous post: MensaarLecker Development Log (2) – Web Developing and GitHub Workflow
After the website is published, we noticed that people now prefer to have lunch in HTW Campus Rotenbühl. Since their menu come from the same site, it is very easy to introduce new menu to our project.
Before we used two pages to store today’s menu and the menu history. And we think in general, all menu are simple texts, so we can put all contents into the index page without any problem. We can switch the visibility using JavaScript:
1 |
|
And for the whole HTML code again we stuffed in the python script to for our daily Github workflow to run.
1 |
|
UdS Fahrplan Bot Development Log (5) -- Explanations on bot sessions and requests
This blog post is trying to tell you:
- Why session is needed when multiple command using similar workflow?
- The details of HTTP request
- Detailed explanations on the implementation of udsfahrbot
Previous post: UdS Fahrplan Bot Development Log (4) – Implementation on /sethome and /home function
In the previous notes, we’ve already implemented all the commands we need! But there’s a question, why we need to format out query in f”{session}:{step}:{loc}”? Also, we haven’t talked about the function that connects to the SaarVV api.
We also have another study notes for all of the telegram bot projects in here, it gives you the basics on how to create your own bot and further descriptions on different functions and attributes on the package python-telegram-bot.
In Application layer, if we send a GET request to the URL, it returns the HTML of the URL. For example:
1 |
|
Then we are basically retriving the source code the website.
UdS Fahrplan Bot Development Log (4) -- Implementation on /sethome and /home function
This blog post is trying to tell you:
- How to build your own telegram bot
- Detailed explanations on the implementation of udsfahrbot
Previous post: UdS Fahrplan Bot Development Log (3) – Implementation on /trip and /depart function
In the previous notes, we’ve already implemented /trip
and /depart
based on this. Now, let’s start explaining them one by one.
We also have another study notes for all of the telegram bot projects in here, it gives you the basics on how to create your own bot and further descriptions on different functions and attributes on the package python-telegram-bot.
Similarly, our main function should have app.add_handler(CommandHandler("sethome", spawn))
and InlineKeyboardMarkup[]
ready, these components are defined in previous notes and they are reusable.
Next, our function has to be able to distinguish different users. Users who interact with the bot using command or tagged message will have their id stored in message.from_user.id
; and query.from_user.id
if users are interacting using buttons.
Then, we also need to create a database or storage file to read and write users’ stations. We decide the tool to use based on our use case and needs. Here, the data we need to store are user_id
, station_name
, and station_id
. Which the columns are stable and small size. Therefore, it can be handled by a simple JSON file. Before we store the station, remember to check if the user already stored any station before to avoid creating duplicates.
1 |
|
UdS Fahrplan Bot Development Log (3) -- Implementation on /trip and /Depart function
This blog post is trying to tell you:
- How to build your own telegram bot
- Detailed explanations on the implementation of udsfahrbot
Previous post: UdS Fahrplan Bot Development Log (2) – Planning for telegram bot
In the previous notes, we’ve talked about our motivation and planned functions we wanted to implement. Now, let’s start explaining them one by one.
We also have another study notes for all of the telegram bot projects in here, it gives you the basics on how to create your own bot and further descriptions on different functions and attributes on the package python-telegram-bot.
We start with the basic trip searching function just like the original app. We used CommandHandler
to create the command, InlineKeyboardMarkup
to create buttons for stations, CallbackQueryHandler
to handle the button actions, and update.message.reply_text
to send message back to the users.
1 |
|
When the user called the command /trip
, the bot will update the message into a list of buttons thanks to the InlineKeyboardMarkup
, it passes a string of message with three components, seperated with colon. f”{session}:{step}:{loc}”, it will then pass into handle_callback()
and used to indentify different command using session
, then it identifies the current state using steps
(since stations can be either start or destination), and eventually the station details in loc
will be passed to next step.
UdS Fahrplan Bot Development Log (2) -- Planning for telegram bot
This blog post is trying to tell you:
- The motivation of making my own telegram bot
- Is UdSfahrplan “Reinventing the wheel”?
Previous post: UdS Fahrplan Bot Development Log (1) – Fetching data from HAFAS and its APIs through POST requests
SaarFahrplan is a public transport app to provide real-time information and services related to public transportation in Saarland. Its target users are the people who live and travel in Saarland for general purpose. When it comes to a smaller group of users, for example Uni students, some functionalities might be redundant and we may optimized some common use case for better user experience.
Instead of searching different connections, Uni students’ timetable are usually consistent and repetitive. There are a few places where they always go, for example:
Another scenario will be when we are in somewhere new, we would like to know how to go back home (dormitory).
Based on the above use case, So we create our own app/bot that only accommodate them.
UdS Fahrplan Bot Development Log (1) -- Fetching data from HAFAS and its APIs through POST requests
This blog post is trying to tell you:
- My personal study notes on HAFAS, a public transport management system used around europe
The HaCon Timetable Information System (HAFAS) is a software for timetable information from the company HaCon (Hannover Consulting). – Wikipedia
Basically, the entire Germany, Luxembourg, and the surrounding countries/regions use HAFAS to obtain depatures and stations details. This centralized software system can be visited using APIs. Different service providers may create their own HAFAS backend endpoint that exposes a public transport API over HTTP for customized usage. For example, you can send your HTTP requests to Deutsche Bahn (DB) if you have the access of their API, which can be found in DB API Marketplace.
There are four basic functions that the system has provided:
TripSearch
– return connections from location A to location BStationBoard
– return all arrivals and departures of a certain stationLocGeoPos
– return list of stations in a give range of areaLocMatch
– return list of stations based on the keyword givenThese includes most of the functionalities for users. For example, when we tried to plan our journey on DB navigator, the app uses TripSearch
to show you connections; when we type the stations in the search box, the app LocMatch
to give you related results.
Update your browser to view this website correctly.&npsb;Update my browser now