2018-05-25 00:38:27 +00:00
|
|
|
from bot import Bot
|
|
|
|
import logging
|
|
|
|
from report import Report
|
|
|
|
from twx.botapi import TelegramBot
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class TelegramBot(Bot):
|
|
|
|
def crawl(self, user):
|
|
|
|
tb = TelegramBot(user.get_telegram_credentials())
|
|
|
|
updates = tb.get_updates().wait()
|
|
|
|
reports = []
|
|
|
|
for update in updates:
|
2018-06-30 19:32:22 +00:00
|
|
|
if update.message.text =="/stop":
|
|
|
|
remove_telegram_subscribers(self, update.message.from.id)
|
|
|
|
else:
|
|
|
|
add_telegram_subscribers(self, update.message.from.id)
|
|
|
|
reports.append(Report(update.message.from.username,self,
|
|
|
|
update.message.text,None,update.message.date))
|
2018-05-25 00:38:27 +00:00
|
|
|
return reports
|
|
|
|
|
|
|
|
def post(self, user, report):
|
|
|
|
tb = TelegramBot(user.get_telegram_credentials())
|
|
|
|
text = report.text
|
|
|
|
if len(text) > 4096:
|
|
|
|
text = text[:4096 - 4] + u' ...'
|
|
|
|
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() + ': '
|
|
|
|
+ report.id, exc_info=True)
|