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 2baa42d8f3
commit c93c7a47b8
3 changed files with 19 additions and 1 deletions

View file

@ -10,7 +10,8 @@ logger = logging.getLogger(__name__)
class TelegramBot(Bot): class TelegramBot(Bot):
def crawl(self, user): def crawl(self, user):
tb = Telegram(user.get_telegram_credentials()) 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 = [] reports = []
for update in updates: for update in updates:
try: try:
@ -28,6 +29,7 @@ class TelegramBot(Bot):
else: else:
reports.append(Report(update.message.sender.username, self, reports.append(Report(update.message.sender.username, self,
update.message.text, None, update.message.date)) update.message.text, None, update.message.date))
user.save_seen_tg(update.message.id)
except AttributeError: except AttributeError:
print(updates[0], updates[1]) # Telegram API returns an Error print(updates[0], updates[1]) # Telegram API returns an Error
return reports return reports

6
db.py
View file

@ -75,6 +75,12 @@ class DB(object):
FOREIGN KEY(mastodon_accounts_id) FOREIGN KEY(mastodon_accounts_id)
REFERENCES 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 ( CREATE TABLE IF NOT EXISTS twitter_request_tokens (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
user_id INTEGER, user_id INTEGER,

10
user.py
View file

@ -174,6 +174,16 @@ schlitz
(tweet_id, self.uid)) (tweet_id, self.uid))
db.commit() 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): def get_mailinglist(self):
db.execute("SELECT email FROM mailinglist WHERE user_id = ?;", (self.uid, )) db.execute("SELECT email FROM mailinglist WHERE user_id = ?;", (self.uid, ))
return db.cur.fetchall() return db.cur.fetchall()