-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow admin to respond directly in telegram
Allow admins to respond to feedback messages by replying to the message in Telegram, instead of the current process of having to manually POST to the telemsg API. This should make it extremely easy to respond to future feedback. Determine responses by parsing the message that the admin is replying to, but this is a pretty bad implementation that couples the new handler with how the current `/feedback` handler sends messages to admin. To fix this, we'd have to offload the messaging to a separate service, but it isn't worth doing now. Messages are sent by POST-ing to the telemsg API. This responsibility should also be offloaded to a separate service, along with the POST-ing done in the `/feedback` handler, but again it isn't worth doing.
- Loading branch information
1 parent
b875848
commit 97d1e48
Showing
4 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import os | ||
import re | ||
|
||
import requests | ||
from telegram.ext import Filters, MessageHandler | ||
|
||
from ..config import TELEGRAM_ADMIN_ID, TELEMSG_API_HOST, TELEMSG_API_PORT | ||
from .logging import logger | ||
|
||
|
||
def admin_message_handler(update, context): | ||
if update.message.chat_id != TELEGRAM_ADMIN_ID: | ||
return | ||
if update.message.reply_to_message is None: | ||
return | ||
|
||
text = update.message.reply_to_message.text | ||
m = re.match(r'Message from (\d+):', text) | ||
if m is None: | ||
return | ||
target_user = int(m.group(1)) | ||
text = f'Message from admin:\n{update.message.text}' | ||
message_user(target_user, text) | ||
|
||
|
||
def message_user(chat_id: int, text: str) -> None: | ||
url = f'http://{TELEMSG_API_HOST}:{TELEMSG_API_PORT}/message' | ||
requests.post(url, json={ | ||
'chat_id': chat_id, | ||
'text': text, | ||
}) | ||
|
||
|
||
message_handler = MessageHandler(Filters.text & ~Filters.command, admin_message_handler) |