From 2baa42d8f34825d871b02e354dac4944231b8e28 Mon Sep 17 00:00:00 2001 From: b3yond Date: Sun, 9 Sep 2018 16:58:07 +0200 Subject: [PATCH] telegram troubleshooting and fine-tuning --- active_bots/telegrambot.py | 36 ++++++++++++++++++------------------ db.py | 1 - frontend.py | 5 ++--- user.py | 11 ++--------- 4 files changed, 22 insertions(+), 31 deletions(-) diff --git a/active_bots/telegrambot.py b/active_bots/telegrambot.py index bd833f0..ab62fa5 100644 --- a/active_bots/telegrambot.py +++ b/active_bots/telegrambot.py @@ -13,24 +13,24 @@ class TelegramBot(Bot): updates = tb.get_updates().wait() reports = [] for update in updates: - if update.message.text.lower() == "/start": - user.add_telegram_subscribers(update.message.sender.id) - tb.send_message(update.message.sender.id, "You are now \ - subscribed to report notifications.") - #TODO: /start message should be set in frontend - elif update.message.text.lower() == "/stop": - user.remove_telegram_subscribers(update.message.sender.id) - tb.send_message(update.message.sender.id, "You are now \ - unsubscribed from report notifications.") - #TODO: /stop message should be set in frontend - elif update.message.text.lower() == "/help": - tb.send_message(update.message.sender.id, "Send reports here to \ - share them with other users. Use /start and /stop to \ - be included/excluded.") - #TODO: /help message should be set in frontend - else: - reports.append(Report(update.message.sender.username, self, - update.message.text, None, update.message.date)) + try: + if update.message.text.lower() == "/start": + user.add_telegram_subscribers(update.message.sender.id) + tb.send_message(update.message.sender.id, "You are now subscribed to report notifications.") + # TODO: /start message should be set in frontend + elif update.message.text.lower() == "/stop": + user.remove_telegram_subscribers(update.message.sender.id) + tb.send_message(update.message.sender.id, "You are now unsubscribed from report notifications.") + # TODO: /stop message should be set in frontend + elif update.message.text.lower() == "/help": + tb.send_message(update.message.sender.id, "Send reports here to share them with other users. Use /start and /stop to be included/excluded.") + # TODO: /help message should be set in frontend + else: + reports.append(Report(update.message.sender.username, self, + update.message.text, None, update.message.date)) + except AttributeError: + print(updates[0], updates[1]) # Telegram API returns an Error + return reports return reports def post(self, user, report): diff --git a/db.py b/db.py index a55158e..473b24a 100644 --- a/db.py +++ b/db.py @@ -126,7 +126,6 @@ class DB(object): id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, user_id INTEGER, subscriber_id INTEGER, - active INTEGER, FOREIGN KEY(user_id) REFERENCES user(id), UNIQUE(user_id, subscriber_id) ON CONFLICT IGNORE ); diff --git a/frontend.py b/frontend.py index e6c3b91..adae5fc 100755 --- a/frontend.py +++ b/frontend.py @@ -160,11 +160,10 @@ def update_badwords(user): @post('/settings/telegram') -@view('template/settings.tpl') def register_telegram(user): apikey = request.forms['apikey'] - user.set_telegram_key(apikey) - return user.state() + user.update_telegram_key(apikey) + return city_page(user.get_city(), info="Thanks for registering Telegram!") @get('/api/state') diff --git a/user.py b/user.py index e6b0c8d..9356518 100644 --- a/user.py +++ b/user.py @@ -94,20 +94,13 @@ schlitz return True def get_telegram_credentials(self): - db.execute("""SELECT api_token + db.execute("""SELECT apikey FROM telegram_accounts WHERE user_id = ? AND active = 1;""", (self.uid,)) row = db.cur.fetchone() return row[0] - def save_telegram_credentials(self, api_token): - db.execute("""INSERT INTO telegram_accounts ( - user_id, api_token, active) VALUES(?, ?, 1);""", - (self.uid, api_token)) - db.commit() - - def get_telegram_subscribers(self): db.execute("""SELECT subscriber_id FROM telegram_subscribers @@ -272,7 +265,7 @@ schlitz (self.uid, )) return db.cur.fetchall() - def set_telegram_key(self, apikey): + def update_telegram_key(self, apikey): db.execute("UPDATE telegram_accounts SET apikey = ? WHERE user_id = ?;", (apikey, self.uid)) db.commit()