telegram troubleshooting and fine-tuning

This commit is contained in:
b3yond 2018-09-09 16:58:07 +02:00
parent fd8b29c55f
commit 439dbeb1fa
4 changed files with 22 additions and 31 deletions

View file

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

1
db.py
View file

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

View file

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

11
user.py
View file

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