def __init__(self, token, chat_id): """ :param token: str - Telegram's bot token :param chat_id: str - Telegram's chat ID """ self.token = token self.chat_id = chat_id def make_message(self, torrent_data): return '''The following torrents were updated:\n%s''' \ % '\n'.join(map(lambda t: t['name'], torrent_data.values())) def test_configuration(self): url = '%s%s/getMe' % (self.url, self.token) r = requests.get(url) return r.json().get('ok', False) def send_message(self, msg): url = '%s%s/sendMessage' % (self.url, self.token) r = requests.post(url, data={'chat_id': self.chat_id, 'text': msg}) if r.json()['ok']: LOGGER.info('Telegram message was sent to user %s' % self.chat_id) else: LOGGER.error('Telegram notification not send: %s' % r['description']) NotifierClassesRegistry.add(TelegramNotifier)
except Exception as e: LOGGER.error(e) return if self.user and self.password: try: connection.login(self.user, self.password) except SMTPAuthenticationError as e: LOGGER.error(e) return return connection def send_message(self, msg): self.connection.sendmail(self.sender, [self.email], msg) def test_configuration(self): return bool(self.connection) def make_message(self, torrent_data): text = '''The following torrents were updated:\n%s\n\nBest regards,\ntorrt.''' \ % '\n'.join(map(lambda t: t['name'], torrent_data.values())) msg = MIMEText(text) msg['Subject'] = 'New torrents were added to download queue.' msg['From'] = self.sender msg['To'] = self.email LOGGER.info('Notification message was sent to user %s' % self.email) return msg.as_string() NotifierClassesRegistry.add(EmailNotifier)
self.chat_id = chat_id def make_message(self, torrent_data): return '''The following torrents were updated:\n%s''' \ % '\n'.join(map(lambda t: t['name'], torrent_data.values())) def test_configuration(self): url = '%s%s/getMe' % (self.url, self.token) r = requests.get(url) return r.json().get('ok', False) def send_message(self, msg): url = '%s%s/sendMessage' % (self.url, self.token) try: response = requests.post(url, data={'chat_id': self.chat_id, 'text': msg}) except RequestException as e: LOGGER.error('Failed to send Telegram message: %s', e) else: if response.ok: json_data = response.json() if json_data['ok']: LOGGER.debug('Telegram message was sent to user %s', self.chat_id) else: LOGGER.error('Telegram notification not send: %s', json_data['description']) else: LOGGER.error('Telegram notification not send. Response code: %s (%s)', response.status_code, response.reason) NotifierClassesRegistry.add(TelegramNotifier)