-
Notifications
You must be signed in to change notification settings - Fork 362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Telegram Bot for notifying new questions #1198
base: master
Are you sure you want to change the base?
Conversation
During a contest it might be useful to be notified on the smartphone when a new question arrives. This is especially true for long contests where keeping the admin page open is not really feasible. This patch sends a Telegram message to a chat when a new question arrives and it is approved (i.e. not too long). It just uses "requests" for interacting with the Telgram API, avoiding additional dependencies and it's disabled by default. The actual notification is sent in a separate greenlet avoiding to slow down the request handler if the Internet connection or the Telegram servers are slow. To enable the bot you have to set the Telegram token and the chat id in cms.conf (`telegram_bot_token` and `telegram_bot_chat_id`).
377f9cc
to
d8489d4
Compare
if bool(self.telegram_bot_token) ^ bool(self.telegram_bot_chat_id): | ||
raise ConfigError("Both telegram_bot_token and telegram_bot_chat_id " | ||
"should be set or left null") | ||
if self.telegram_bot_chat_id: | ||
if type(self.telegram_bot_chat_id) != int: | ||
raise ConfigError("telegram_bot_chat_id should be an integer") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These checks are probably better moved together with the other Telegram code to improve locality. This should be easier with the below suggestion.
Thanks for the feature! I feel it might not be the best solution to put Telegram message sending directly in
An alternative suggestion would be to add a separate service in Perhaps @stefano-maggiolo also has an opinion on this. |
Agree, this was more like a proof of concept that tried to minimize the code changes. CWS clearly is not the best place for this feature (especially since in our setup the CWSs don't even have Internet access!). I'm keen on the idea of a separate service for the Telegram bot, especially since it enables richer features. I'm imagining the possibility to answer to the questions directly from Telegram (again, especially useful for long-running contests). On the other hand this doesn't look like as simple as making an HTTP request when a question arrives. As you noted, keeping track of which notification has already been sent, noticing when a new question arrives, retrying on failure, and so on is less trivial and may require more invasive changes. |
I was going to leave a note about this, but now it definitely seems like this should be of interest: @t-lenz had the same idea some time ago and went to the trouble of implementing a feature-rich Telegram Bot (in the German CMS fork, most of it is in this file). This has about every feature we could think of, like allowing you to answer a question or to make an announcement. It's been very reliable and useful over the last couple of years. The bot's protocol:
EDIT: Sorry, I had mentioned the wrong username and, therefore, person. |
Indeed, it is better to keep this feature isolated, and not alter the DB structure. New (and missed) questions can be found with DB polling, similar to the existing |
I agree on what has been said, a separate service would be nice. I don't think there is a need to add booleans "has been sent" to the question schema: if the service is running then it is sending new questions, if not, to start it you have to be interacting with CMS and thus you can check the questions there. |
During a contest it might be useful to be notified on the smartphone when a new question arrives. This is especially true for long contests where keeping the admin page open is not really feasible.
This patch sends a Telegram message to a chat when a new question arrives and it is approved (i.e. not too long). It just uses "requests" for interacting with the Telgram API, avoiding additional dependencies and it's disabled by default. The actual notification is sent in a separate greenlet avoiding to slow down the request handler if the Internet connection or the Telegram servers are slow.
To enable the bot you have to set the Telegram token and the chat id in cms.conf (
telegram_bot_token
andtelegram_bot_chat_id
).This has been tested by the Italian Olympiads team for many competitions, at least at 2 national rounds and at 10 OIS rounds (olympiads in team). Needless to say that the contest web servers should have access to Internet in order to send the messages.