From 8acbfb4569df0edc38832cda20ff310c72c0a8c2 Mon Sep 17 00:00:00 2001 From: git-sid <25916907+git-sid@users.noreply.github.com> Date: Sun, 9 Sep 2018 17:22:00 +0200 Subject: [PATCH] added offset to telegram message polling to prevent duplicated responses --- active_bots/telegrambot.py | 4 +++- db.py | 6 ++++++ user.py | 10 ++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/active_bots/telegrambot.py b/active_bots/telegrambot.py index ab62fa5..637b332 100644 --- a/active_bots/telegrambot.py +++ b/active_bots/telegrambot.py @@ -10,7 +10,8 @@ logger = logging.getLogger(__name__) class TelegramBot(Bot): def crawl(self, user): tb = Telegram(user.get_telegram_credentials()) - updates = tb.get_updates().wait() + seen_tg = user.get_seen_tg() + updates = tb.get_updates(offset=seen_tg+1).wait() reports = [] for update in updates: try: @@ -28,6 +29,7 @@ class TelegramBot(Bot): else: reports.append(Report(update.message.sender.username, self, update.message.text, None, update.message.date)) + user.save_seen_tg(update.message.id) except AttributeError: print(updates[0], updates[1]) # Telegram API returns an Error return reports diff --git a/db.py b/db.py index 473b24a..c875a2e 100644 --- a/db.py +++ b/db.py @@ -75,6 +75,12 @@ class DB(object): FOREIGN KEY(mastodon_accounts_id) REFERENCES mastodon_accounts(id) ); + CREATE TABLE IF NOT EXISTS seen_telegrams ( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, + user_id INTEGER, + tg_id INTEGER, + FOREIGN KEY(user_id) REFERENCES user(id), + ); CREATE TABLE IF NOT EXISTS twitter_request_tokens ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, user_id INTEGER, diff --git a/user.py b/user.py index 9356518..a7c79df 100644 --- a/user.py +++ b/user.py @@ -174,6 +174,16 @@ schlitz (tweet_id, self.uid)) db.commit() + def get_seen_tg(self): + db.execute("SELECT tg_id FROM seen_telegrams WHERE user_id = ?;", + (self.uid,)) + return db.cur.fetchone() + + def save_seen_tg(self, tg_id): + db.execute("UPDATE seen_telegrams SET tg_id = ? WHERE user_id = ?;", + (tg_id, self.uid)) + db.commit() + def get_mailinglist(self): db.execute("SELECT email FROM mailinglist WHERE user_id = ?;", (self.uid, )) return db.cur.fetchall()