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
New Features
Previous post: MensaarLecker Development Log (2) – Web Developing and GitHub Workflow
HTW menu
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.
New website layout
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 |
|
Telegram Bot – @Mensaar_Bot
On top of the webiste, we can reuse the scraping code to create a telegram bot. First, we need to create a bot and get its API key using botfather.
Onve you get the key, you bot is created and alive! However, it doesn’t do anything because we haven’t tell it what to do. So, we need to implement its functionalities. python-telegram-bot is a very good package that contains everything we need to control the bot.
RapidFuzz – Fuzzy Text detection for bot reply
Other than commands and buttons, we also want to make the bot to reply to text message for better interactions. When the bot is added to a group chat, users can interact to the bot by tagging the bot and sending messages. Any texts related to food, mensa and menu will be accpeted and perform the action.
This is done under fuzzy matching. Using fuzz.partial_ratio()
we can compare the similarity of users’ messages with our keyword list.
Brief Walkthrough on Telegram Bot
We also have another study notes for all of the telegram bot projects in here
CommandHandler – /start
The basic way to call a bot is to send a command. Commands are defined by CommandHandler
, which we can implement each commands’ functionalites respectively. To make our bot send message to users, we can use update.message.reply_text
.
1 |
|
InlineKeyboardMarkup – Buttons for actions
Sometimes, users may need further options to finish the command, here we used InlineKeyboardMarkup
to create possible options. There is another option ReplyKeyboardMarkup
that can create buttons by replacing your phone keyboard, but from user feedbacks we noticed that it may be annoying for group usage.
1 |
|
CallbackQueryHandler – Handle your buttons and actions
When a button from InlineKeyboardMarkup
is pressed, the data from callback_data
attribute is then passed. We can capture it by using CallbackQueryHandler
. Since we are using Query here, we need to use query.edit_message_text
.
1 |
|
MensaarLecker Development Log (3) -- Telegram Bot Deployment and Integration