added /start, /stop, /help command check & small fixes

multi-deployment
sid 2018-05-29 07:07:15 +02:00
parent 4b37c0df3d
commit a48ba9ebf8
1 changed files with 16 additions and 5 deletions

View File

@ -1,7 +1,7 @@
from bot import Bot from bot import Bot
import logging import logging
from report import Report from report import Report
from twx.botapi import TelegramBot from twx.botapi import TelegramBot as Telegram
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -9,16 +9,27 @@ logger = logging.getLogger(__name__)
class TelegramBot(Bot): class TelegramBot(Bot):
def crawl(self, user): def crawl(self, user):
tb = TelegramBot(user.get_telegram_credentials()) tb = Telegram(user.get_telegram_credentials())
updates = tb.get_updates().wait() updates = tb.get_updates().wait()
reports = [] reports = []
for update in updates: for update in updates:
reports.append(Report(update.message.from.username,self, if update.message.text.lower() == "/start":
update.message.text,None,update.message.date)) user.add_telegram_subscribers(update.message.from.id)
tb.send_message(update.message.from.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.from.id)
tb.send_message(update.message.from.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.from.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:
for row in user.get_telegram_subscribers():
if update.message.from.id == row[0]:
reports.append(Report(update.message.from.username, self, update.message.text, None, update.message.date))
break
return reports return reports
def post(self, user, report): def post(self, user, report):
tb = TelegramBot(user.get_telegram_credentials()) tb = Telegram(user.get_telegram_credentials())
text = report.text text = report.text
if len(text) > 4096: if len(text) > 4096:
text = text[:4096 - 4] + u' ...' text = text[:4096 - 4] + u' ...'