2018-05-25 00:38:27 +00:00
|
|
|
from bot import Bot
|
|
|
|
import logging
|
|
|
|
from report import Report
|
2018-05-29 05:07:15 +00:00
|
|
|
from twx.botapi import TelegramBot as Telegram
|
2018-05-25 00:38:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class TelegramBot(Bot):
|
|
|
|
def crawl(self, user):
|
2018-05-29 05:07:15 +00:00
|
|
|
tb = Telegram(user.get_telegram_credentials())
|
2018-09-09 15:22:00 +00:00
|
|
|
seen_tg = user.get_seen_tg()
|
2018-09-09 15:51:07 +00:00
|
|
|
try:
|
2018-09-09 15:52:23 +00:00
|
|
|
updates = tb.get_updates(offset=seen_tg + 1,
|
|
|
|
allowed_updates="message").wait()
|
2018-09-09 15:51:07 +00:00
|
|
|
except TypeError:
|
|
|
|
updates = tb.get_updates().wait()
|
2018-05-25 00:38:27 +00:00
|
|
|
reports = []
|
2018-09-14 17:59:45 +00:00
|
|
|
if updates == None:
|
|
|
|
return reports
|
2018-05-25 00:38:27 +00:00
|
|
|
for update in updates:
|
2018-10-07 17:09:02 +00:00
|
|
|
# return when telegram returns an error code
|
2018-10-26 15:27:00 +00:00
|
|
|
if update in [303, 404, 420, 500, 502]:
|
2018-10-07 17:09:02 +00:00
|
|
|
return reports
|
2018-11-06 07:56:24 +00:00
|
|
|
if isinstance(update, int):
|
2018-12-31 14:32:19 +00:00
|
|
|
try:
|
2018-12-31 14:33:50 +00:00
|
|
|
logger.error("City " + str(user.uid) +
|
2018-12-31 14:32:19 +00:00
|
|
|
": Unknown Telegram error code: " +
|
2018-12-31 14:33:50 +00:00
|
|
|
str(update) + " - " + str(updates[1]))
|
2018-12-31 14:32:19 +00:00
|
|
|
except TypeError:
|
|
|
|
logger.error("Unknown Telegram error code: " + str(update))
|
2018-09-09 14:58:07 +00:00
|
|
|
return reports
|
2018-09-09 18:32:10 +00:00
|
|
|
user.save_seen_tg(update.update_id)
|
2019-05-03 10:06:11 +00:00
|
|
|
if update.message.photo:
|
|
|
|
tb.send_message(
|
|
|
|
update.message.sender.id,
|
|
|
|
"Sending Photos is not supported for privacy reasons. Can "
|
|
|
|
"you describe it in a report instead?")
|
|
|
|
continue
|
2018-09-09 18:32:10 +00:00
|
|
|
if update.message.text.lower() == "/start":
|
|
|
|
user.add_telegram_subscribers(update.message.sender.id)
|
2018-09-14 10:45:49 +00:00
|
|
|
tb.send_message(
|
|
|
|
update.message.sender.id,
|
|
|
|
"You are now subscribed to report notifications.")
|
2018-09-09 18:32:10 +00:00
|
|
|
# TODO: /start message should be set in frontend
|
|
|
|
elif update.message.text.lower() == "/stop":
|
|
|
|
user.remove_telegram_subscribers(update.message.sender.id)
|
2018-09-14 10:45:49 +00:00
|
|
|
tb.send_message(
|
|
|
|
update.message.sender.id,
|
|
|
|
"You are now unsubscribed from report notifications.")
|
2018-09-09 18:32:10 +00:00
|
|
|
# TODO: /stop message should be set in frontend
|
|
|
|
elif update.message.text.lower() == "/help":
|
2018-09-14 10:45:49 +00:00
|
|
|
tb.send_message(
|
|
|
|
update.message.sender.id,
|
2019-01-07 18:02:07 +00:00
|
|
|
"Send reports here to share them with other users. "
|
|
|
|
"Use /start and /stop to get reports or not.")
|
2018-09-09 18:32:10 +00:00
|
|
|
# TODO: /help message should be set in frontend
|
|
|
|
else:
|
2018-10-26 16:20:01 +00:00
|
|
|
# set report.author to "" to avoid mailbot crash
|
2018-11-12 11:32:28 +00:00
|
|
|
sender_name = update.message.sender.username
|
|
|
|
if sender_name is None:
|
|
|
|
sender_name = ""
|
2018-10-26 16:20:01 +00:00
|
|
|
|
2018-11-12 11:32:28 +00:00
|
|
|
reports.append(Report(sender_name, self, update.message.text,
|
|
|
|
None, update.message.date))
|
2018-05-25 00:38:27 +00:00
|
|
|
return reports
|
|
|
|
|
|
|
|
def post(self, user, report):
|
2018-05-29 05:07:15 +00:00
|
|
|
tb = Telegram(user.get_telegram_credentials())
|
2018-05-25 00:38:27 +00:00
|
|
|
text = report.text
|
|
|
|
if len(text) > 4096:
|
2019-01-07 18:00:51 +00:00
|
|
|
text = text[:4096 - 2] + " \N{Horizontal ellipsis}"
|
2018-05-25 00:38:27 +00:00
|
|
|
try:
|
|
|
|
for subscriber_id in user.get_telegram_subscribers():
|
|
|
|
tb.send_message(subscriber_id, text).wait()
|
|
|
|
except Exception:
|
|
|
|
logger.error('Error telegramming: ' + user.get_city() + ': '
|
2018-09-09 15:51:07 +00:00
|
|
|
+ str(report.id), exc_info=True)
|