added offset to telegram message polling to prevent duplicated responses

This commit is contained in:
git-sid 2018-09-09 17:22:00 +02:00
parent 439dbeb1fa
commit 8acbfb4569
3 changed files with 19 additions and 1 deletions

View file

@ -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

6
db.py
View file

@ -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,

10
user.py
View file

@ -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()