telegram troubleshooting and fine-tuning

master
b3yond 2018-09-09 16:58:07 +02:00
parent ecb9ac659d
commit 2baa42d8f3
4 changed files with 22 additions and 31 deletions

View File

@ -13,24 +13,24 @@ class TelegramBot(Bot):
updates = tb.get_updates().wait() updates = tb.get_updates().wait()
reports = [] reports = []
for update in updates: for update in updates:
if update.message.text.lower() == "/start": try:
user.add_telegram_subscribers(update.message.sender.id) if update.message.text.lower() == "/start":
tb.send_message(update.message.sender.id, "You are now \ user.add_telegram_subscribers(update.message.sender.id)
subscribed to report notifications.") tb.send_message(update.message.sender.id, "You are now subscribed to report notifications.")
#TODO: /start message should be set in frontend # TODO: /start message should be set in frontend
elif update.message.text.lower() == "/stop": elif update.message.text.lower() == "/stop":
user.remove_telegram_subscribers(update.message.sender.id) user.remove_telegram_subscribers(update.message.sender.id)
tb.send_message(update.message.sender.id, "You are now \ tb.send_message(update.message.sender.id, "You are now unsubscribed from report notifications.")
unsubscribed from report notifications.") # TODO: /stop message should be set in frontend
#TODO: /stop message should be set in frontend elif update.message.text.lower() == "/help":
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.")
tb.send_message(update.message.sender.id, "Send reports here to \ # TODO: /help message should be set in frontend
share them with other users. Use /start and /stop to \ else:
be included/excluded.") reports.append(Report(update.message.sender.username, self,
#TODO: /help message should be set in frontend update.message.text, None, update.message.date))
else: except AttributeError:
reports.append(Report(update.message.sender.username, self, print(updates[0], updates[1]) # Telegram API returns an Error
update.message.text, None, update.message.date)) return reports
return reports return reports
def post(self, user, report): def post(self, user, report):

1
db.py
View File

@ -126,7 +126,6 @@ class DB(object):
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
user_id INTEGER, user_id INTEGER,
subscriber_id INTEGER, subscriber_id INTEGER,
active INTEGER,
FOREIGN KEY(user_id) REFERENCES user(id), FOREIGN KEY(user_id) REFERENCES user(id),
UNIQUE(user_id, subscriber_id) ON CONFLICT IGNORE UNIQUE(user_id, subscriber_id) ON CONFLICT IGNORE
); );

View File

@ -160,11 +160,10 @@ def update_badwords(user):
@post('/settings/telegram') @post('/settings/telegram')
@view('template/settings.tpl')
def register_telegram(user): def register_telegram(user):
apikey = request.forms['apikey'] apikey = request.forms['apikey']
user.set_telegram_key(apikey) user.update_telegram_key(apikey)
return user.state() return city_page(user.get_city(), info="Thanks for registering Telegram!")
@get('/api/state') @get('/api/state')

11
user.py
View File

@ -94,20 +94,13 @@ schlitz
return True return True
def get_telegram_credentials(self): def get_telegram_credentials(self):
db.execute("""SELECT api_token db.execute("""SELECT apikey
FROM telegram_accounts FROM telegram_accounts
WHERE user_id = ? AND active = 1;""", WHERE user_id = ? AND active = 1;""",
(self.uid,)) (self.uid,))
row = db.cur.fetchone() row = db.cur.fetchone()
return row[0] 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): def get_telegram_subscribers(self):
db.execute("""SELECT subscriber_id db.execute("""SELECT subscriber_id
FROM telegram_subscribers FROM telegram_subscribers
@ -272,7 +265,7 @@ schlitz
(self.uid, )) (self.uid, ))
return db.cur.fetchall() 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.execute("UPDATE telegram_accounts SET apikey = ? WHERE user_id = ?;", (apikey, self.uid))
db.commit() db.commit()