Skip to content

Commit

Permalink
feat: allow admin to respond directly in telegram
Browse files Browse the repository at this point in the history
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
gabrielwong159 committed Sep 21, 2024
1 parent b875848 commit 97d1e48
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ services:
image: evs-telebot:latest
environment:
TELEGRAM_TOKEN: "${TELEGRAM_TOKEN}"
TELEGRAM_ADMIN_ID: "${TELEGRAM_ADMIN_ID}"
DB_API_HOST: "db_api"
DB_API_PORT: 8080
TELEMSG_API_HOST: "telemsg_api"
Expand Down
3 changes: 2 additions & 1 deletion hosts/telebot/src/bot/bot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from telegram.ext import Updater
from .handlers import commands as cmd
from .handlers import commands as cmd, message
from .handlers.conv import (add_subscription as add,
view_subscription as view,
feedback as feedback)
Expand All @@ -16,6 +16,7 @@ def add_handlers(self):
self.dispatcher.add_handler(add.conv_handler)
self.dispatcher.add_handler(view.conv_handler)
self.dispatcher.add_handler(feedback.conv_handler)
self.dispatcher.add_handler(message.message_handler)

def start(self):
self.updater.start_polling()
Expand Down
2 changes: 2 additions & 0 deletions hosts/telebot/src/bot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@

TELEMSG_API_HOST = os.environ.get('TELEMSG_API_HOST', 'localhost')
TELEMSG_API_PORT = os.environ.get('TELEMSG_API_PORT', 5001)

TELEGRAM_ADMIN_ID = int(os.environ['TELEGRAM_ADMIN_ID'])
34 changes: 34 additions & 0 deletions hosts/telebot/src/bot/handlers/message.py
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)

0 comments on commit 97d1e48

Please sign in to comment.