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
Brief Walkthrough on Telegram Bot (Cont.)
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.
HTTP request on SaarVV
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.
Extra work is needed to perform a POST request
But if simply change the function from GET to POST, it won’t work:
Instead of getting data, a POST requestis typically used for forms, APIs, or uploading data. So we can’t simply ping a website with POST request. For example, if you are sending POST request to a website, you should locate the form and include the correct information. And for API, you should have correct header, user agent, etc.
How can I construct my own POST request?
TLDR: In hafas-client, there’s a lot profiles used by various public transportation networks. However, some of them are deprecated so be careful to verify before you use.
Here, we introduce another approach. We go to the Saarfahrplan website, and try to investigate how a POST request is constructed and sent.
$\text{Press F12}\Rightarrow\text{Network tab}\Rightarrow\text{Select filter}\textbf{Fetch/XHR}$ We can then inspect the details of the request.
We can see the URL here is not a website, but ends with .exe
, which indicates that is an API. Next, we specified the data type we are sending (JSON), and the user agent we are using (HAFAS). Another important attribute is the "auth": {"type": "AID", "aid": "yCW9qZFSye1wIv3gCzm5r7d2kJ3LIF"}
, which are used by the api to verify the connection.
Select the request we want to inspect, and then we can see attibutes like Content-Encoding and Content-Type. Also, we can see the entire request payload, which will be useful for our template when constructing the POST request.
Now back to our first development log, we talked about the following function:
1 |
|
Here, we basically visiting the API via Saarfahrplan by using its credentials and headers. On top of that we customized the attributes and methods so that we can fetch the customized data we want.
Session in Telegram Bot
Now back to our bot, since we implemented our commands seperately. They works fine when users execute them safely. However, we also need to be aware of unexpected usage. For example, what if our user started with the /depart
command, but instead of finishing it, he/she chooses to start another new /trip
command at the same time? When a station is parsed to the context.user_data
, which function should it send to? This is reason we need make sure the function is always passing data correctly.
1 |
|
Reference
UdS Fahrplan Bot Development Log (5) -- Explanations on bot sessions and requests