typing + smaller bugfixes

This commit is contained in:
relikd
2022-04-09 03:45:48 +02:00
parent a25b62d934
commit d0c5072d27
15 changed files with 415 additions and 199 deletions

View File

@@ -1,20 +1,20 @@
#!/usr/bin/env python3
from botlib.tgclient import TGClient
from botlib.tgclient import TGClient, Message
bot = TGClient(__API_KEY__, polling=True, allowedUsers=['my-username'])
@bot.message_handler(commands=['hi'])
def bot_reply(message):
def bot_reply(message: Message) -> None:
if bot.allowed(message): # only reply to a single user (my-username)
bot.reply_to(message, 'Good evening my dear.')
@bot.message_handler(commands=['set'])
def update_config(message):
def update_config(message: Message) -> None:
if bot.allowed(message):
try:
config = data_store.get(message.chat.id)
config = DATA_STORE.get(message.chat.id)
except KeyError:
bot.reply_to(message, 'Not found.')
return
@@ -28,32 +28,32 @@ def update_config(message):
@bot.message_handler(commands=['start'])
def new_chat_info(message):
def new_chat_info(message: Message) -> None:
bot.log_chat_info(message.chat)
if bot.allowed(message):
if data_store.get(message.chat.id):
if DATA_STORE.get(message.chat.id):
bot.reply_to(message, 'Already exists')
else:
CreateNew(message)
class CreateNew:
def __init__(self, message):
def __init__(self, message: Message) -> None:
self.ask_name(message)
def ask_name(self, message):
def ask_name(self, message: Message) -> None:
msg = bot.send_force_reply(message.chat.id, 'Enter Name:')
bot.register_next_step_handler(msg, self.ask_interval)
def ask_interval(self, message):
def ask_interval(self, message: Message) -> None:
self.name = message.text
msg = bot.send_buttons(message.chat.id, 'Update interval (minutes):',
options=[3, 5, 10, 15, 30, 60])
bot.register_next_step_handler(msg, self.finish)
def finish(self, message):
def finish(self, message: Message) -> None:
try:
interval = int(message.text)
interval = int(message.text or 'error')
except ValueError:
bot.send_abort_keyboard(message, 'Not a number. Aborting.')
return

View File

@@ -17,15 +17,15 @@ bot.set_on_kill(cron.stop)
def main():
def clean_db(_):
def clean_db(_) -> None:
Log.info('[clean up]')
OnceDB('cache.sqlite').cleanup(limit=150)
def notify_jobA(_):
def notify_jobA(_) -> None:
jobA.download(topic='development', cohort='dev:py')
send2telegram(__A_CHAT_ID__)
def notify_jobB(_):
def notify_jobB(_) -> None:
jobB.download()
send2telegram(__ANOTHER_CHAT_ID__)
@@ -37,14 +37,15 @@ def main():
# cron.fire()
def send2telegram(chat_id):
def send2telegram(chat_id: int) -> None:
db = OnceDB('cache.sqlite')
# db.mark_all_done()
def _send(cohort, uid, obj):
def _send(cohort: str, uid: str, obj: str) -> bool:
Log.info('[push] {} {}'.format(cohort, uid))
return bot.send(chat_id, obj, parse_mode='HTML',
disable_web_page_preview=True)
msg = bot.send(chat_id, obj, parse_mode='HTML',
disable_web_page_preview=True)
return msg is not None
if not db.foreach(_send):
# send() sleeps 45 sec (on error), safe to call immediatelly