Telegram allowlist

Tweak skip day queries
This commit is contained in:
Johnny Gear 2026-03-06 18:57:13 -06:00
parent ccd523d740
commit e90df3d61e
4 changed files with 11 additions and 16 deletions

View file

@ -116,17 +116,17 @@ def skip_day_put(user, date):
return SkipDay.create(user=user, date=date) return SkipDay.create(user=user, date=date)
def skip_day_delete(user, date): def skip_day_delete(user, date):
q = SkipDay.delete().where(SkipDay.user == user & SkipDay.date == date) q = SkipDay.delete().where((SkipDay.user == user) & (SkipDay.date == date))
return q.execute() return q.execute()
def skip_days_upcoming(user): def skip_days_upcoming(user):
return (SkipDay.select() return (SkipDay.select()
.where(SkipDay.user == user and SkipDay.date >= datetime.date.today()) .where((SkipDay.user == user) & (SkipDay.date >= datetime.date.today()))
.order_by(SkipDay.date) .order_by(SkipDay.date)
.limit(10)) .limit(10))
def skip_day_contains(user, date): def skip_day_contains(user, date):
q = SkipDay.select().where(SkipDay.user == user & SkipDay.date == date) q = SkipDay.select().where((SkipDay.user == user) & (SkipDay.date == date))
return len(q) > 0 return len(q) > 0
def order_status_put(orders_pool, user, mastodon_id, created_at, due_at, text, punishment_for=None): def order_status_put(orders_pool, user, mastodon_id, created_at, due_at, text, punishment_for=None):

View file

@ -18,7 +18,7 @@ MASTODON_OAUTH_CLIENT_NAME = os.environ.get('MASTODON_OAUTH_CLIENT_NAME', 'Gear
MASTODON_OAUTH_CLIENT_WEBSITE = os.environ.get('MASTODON_OAUTH_CLIENT_WEBSITE') MASTODON_OAUTH_CLIENT_WEBSITE = os.environ.get('MASTODON_OAUTH_CLIENT_WEBSITE')
TELEGRAM_API_TOKEN = os.environ.get('TELEGRAM_API_TOKEN') TELEGRAM_API_TOKEN = os.environ.get('TELEGRAM_API_TOKEN')
TELEGRAM_CHAT_ID = int(os.environ.get('TELEGRAM_CHAT_ID')) TELEGRAM_ALLOWLIST = os.environ.get('TELEGRAM_ALLOWLIST', '').split(',')
TELEGRAM_COMMAND_TIMEOUT = int(os.environ.get('TELEGRAM_COMMAND_TIMEOUT', 120)) TELEGRAM_COMMAND_TIMEOUT = int(os.environ.get('TELEGRAM_COMMAND_TIMEOUT', 120))
TELEGRAM_BOT_NAME = os.environ.get('TELEGRAM_BOT_NAME') TELEGRAM_BOT_NAME = os.environ.get('TELEGRAM_BOT_NAME')
TELEGRAM_BOT_DOMAIN = os.environ.get('TELEGRAM_BOT_DOMAIN') TELEGRAM_BOT_DOMAIN = os.environ.get('TELEGRAM_BOT_DOMAIN')

View file

@ -18,7 +18,7 @@ class StartCommand(TelegramCommand):
user_add(username, chat_id) user_add(username, chat_id)
yield "Welcome to gear orders bot. Add a dom with `/dom_add <username>` or ask a sub to add you." yield "Welcome to Gear Orders bot.\n\nEdit your orders at {FLASK_URL}\n\nAdd a dom with `/dom_add <username>` or ask a sub to add you."
class DomAddCommand(TelegramCommand): class DomAddCommand(TelegramCommand):
command_regex = re.compile(r"^\/dom_add( (?P<username>@?\w+))$") command_regex = re.compile(r"^\/dom_add( (?P<username>@?\w+))$")
@ -45,7 +45,7 @@ class DomAddCommand(TelegramCommand):
yield f"Successfully added {dom_username} to your list of doms" yield f"Successfully added {dom_username} to your list of doms"
t = Telegram(session) t = Telegram(session)
await t.message_send(dom.telegram_chat_id, f"@{sub_username} has added you as a dom. You may administer their orders at {FLASK_URL}") await t.message_send(dom.telegram_chat_id, f"@{sub_username} has added you as a dom. You may edit their orders at {FLASK_URL}")
class DomRemoveCommand(TelegramCommand): class DomRemoveCommand(TelegramCommand):
command_regex = re.compile(r"^\/dom_remove( (?P<username>@?\w+))$") command_regex = re.compile(r"^\/dom_remove( (?P<username>@?\w+))$")

View file

@ -1,7 +1,7 @@
import logging import logging
import asyncio import asyncio
from settings import ENV, TELEGRAM_API_TOKEN, TELEGRAM_CHAT_ID, TELEGRAM_COMMAND_TIMEOUT from settings import ENV, TELEGRAM_API_TOKEN, TELEGRAM_ALLOWLIST, TELEGRAM_COMMAND_TIMEOUT
from util import make_session from util import make_session
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -40,15 +40,9 @@ class Telegram:
return await response.json() return await response.json()
async def message_send(self, chat_id, text): async def message_send(self, chat_id, text):
chat_id_actual = chat_id
text_actual = text
if ENV == 'dev' and chat_id != TELEGRAM_CHAT_ID:
text_actual = f"⚠️ Message intended for chat id {chat_id}\n\n" + text
chat_id_actual = TELEGRAM_CHAT_ID
data = { data = {
'chat_id': chat_id_actual, 'chat_id': chat_id,
'text': text_actual 'text': text
} }
return await self.post('sendMessage', data=data) return await self.post('sendMessage', data=data)
@ -102,7 +96,8 @@ async def handle_commands(commands=[], loop=None):
if('message' in update): if('message' in update):
chat_id = update['message']['chat']['id'] chat_id = update['message']['chat']['id']
if(chat_id != TELEGRAM_CHAT_ID): if(update['message']['chat']['type'] != "private" or
update['message']['chat']['username'] not in TELEGRAM_ALLOWLIST):
continue continue
if('text' not in update['message']): if('text' not in update['message']):