diff --git a/plexpy/config.py b/plexpy/config.py index 0b0adf15..4b28c369 100644 --- a/plexpy/config.py +++ b/plexpy/config.py @@ -380,6 +380,8 @@ _CONFIG_DEFINITIONS = { 'TELEGRAM_BOT_TOKEN': (str, 'Telegram', ''), 'TELEGRAM_ENABLED': (int, 'Telegram', 0), 'TELEGRAM_CHAT_ID': (str, 'Telegram', ''), + 'TELEGRAM_HTML_SUPPORT': (int, 'Telegram', 1), + 'TELEGRAM_INCL_POSTER': (int, 'Telegram', 0), 'TELEGRAM_INCL_SUBJECT': (int, 'Telegram', 1), 'TELEGRAM_ON_PLAY': (int, 'Telegram', 0), 'TELEGRAM_ON_STOP': (int, 'Telegram', 0), diff --git a/plexpy/notification_handler.py b/plexpy/notification_handler.py index 8ba8e017..49c48b12 100644 --- a/plexpy/notification_handler.py +++ b/plexpy/notification_handler.py @@ -624,14 +624,17 @@ def build_notify_text(session=None, timeline=None, notify_action=None, agent_id= if not poster_url and plexpy.CONFIG.NOTIFY_UPLOAD_POSTERS: try: thread_name = str(threading.current_thread().ident) + poster_file = os.path.join(plexpy.CONFIG.CACHE_DIR, 'cache-poster-' + thread_name) + # Retrieve the poster from Plex and cache to file urllib.urlretrieve(plexpy.CONFIG.PMS_URL + thumb + '?X-Plex-Token=' + plexpy.CONFIG.PMS_TOKEN, - os.path.join(plexpy.CONFIG.CACHE_DIR, 'cache-poster-'+thread_name+'.jpg')) + poster_file) + # Upload thumb to Imgur and get link - poster_url = helpers.uploadToImgur(os.path.join(plexpy.CONFIG.CACHE_DIR, - 'cache-poster-'+thread_name+'.jpg'), poster_title) + poster_url = helpers.uploadToImgur(poster_file, poster_title) + # Delete the cached poster - os.remove(os.path.join(plexpy.CONFIG.CACHE_DIR, 'cache-poster-'+thread_name+'.jpg')) + os.remove(poster_file) except Exception as e: logger.error(u"PlexPy Notifier :: Unable to retrieve poster for rating_key %s: %s." % (str(rating_key), e)) @@ -1106,6 +1109,10 @@ def strip_tag(data, agent_id=None): if agent_id == 7: p = re.compile(r'<(?!/?(b>|i>|u>)|(a\shref=\"[^\"\'\s]+\"|/a>|font\scolor=\"[^\"\'\s]+\"|/font>)).*?>', re.IGNORECASE | re.DOTALL) + # Allow tags b, i, code, pre, a[href] for Telegram + elif agent_id == 13: + p = re.compile(r'<(?!/?(b>|i>|code>|pre>)|(a\shref=\"[^\"\'\s]+\"|/a>)).*?>', + re.IGNORECASE | re.DOTALL) else: p = re.compile(r'<.*?>', re.IGNORECASE | re.DOTALL) diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index c8edb0c1..16effe97 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -21,6 +21,7 @@ from email.mime.text import MIMEText import email.utils from httplib import HTTPSConnection import os +import requests import shlex import smtplib import subprocess @@ -481,7 +482,7 @@ def send_notification(agent_id, subject, body, notify_action, **kwargs): iftttClient.notify(subject=subject, message=body, action=notify_action) elif agent_id == 13: telegramClient = TELEGRAM() - telegramClient.notify(message=body, event=subject) + telegramClient.notify(message=body, event=subject, **kwargs) elif agent_id == 14: slackClient = SLACK() slackClient.notify(message=body, event=subject) @@ -1175,7 +1176,7 @@ class PUSHOVER(object): {'label': 'Enable HTML Support', 'value': self.html_support, 'name': 'pushover_html_support', - 'description': 'Style your messages using these HTML Tags: b, i, u, a[href], font[color]', + 'description': 'Style your messages using these HTML tags: b, i, u, a[href], font[color]', 'input_type': 'checkbox' } ] @@ -1668,29 +1669,52 @@ class TELEGRAM(object): self.enabled = plexpy.CONFIG.TELEGRAM_ENABLED self.bot_token = plexpy.CONFIG.TELEGRAM_BOT_TOKEN self.chat_id = plexpy.CONFIG.TELEGRAM_CHAT_ID + self.html_support = plexpy.CONFIG.TELEGRAM_HTML_SUPPORT + self.incl_poster = plexpy.CONFIG.TELEGRAM_INCL_POSTER self.incl_subject = plexpy.CONFIG.TELEGRAM_INCL_SUBJECT def conf(self, options): return cherrypy.config['config'].get('Telegram', options) - def notify(self, message, event): + def notify(self, message, event, **kwargs): if not message or not event: return - http_handler = HTTPSConnection("api.telegram.org") + data = {'chat_id': self.chat_id} if self.incl_subject: - text = event.encode('utf-8') + ': ' + message.encode("utf-8") + text = event.encode('utf-8') + ': ' + message.encode('utf-8') else: - text = message.encode("utf-8") + text = message.encode('utf-8') - data = {'chat_id': self.chat_id, - 'text': text} + if self.incl_poster and 'metadata' in kwargs: + metadata = kwargs['metadata'] + poster_url = metadata.get('poster_url','') - http_handler.request("POST", - "/bot%s/%s" % (self.bot_token, "sendMessage"), - headers={'Content-type': "application/x-www-form-urlencoded"}, - body=urlencode(data)) + if poster_url: + files = {'photo': (poster_url, urllib.urlopen(poster_url).read())} + response = requests.post('https://api.telegram.org/bot%s/%s' % (self.bot_token, 'sendPhoto'), + data=data, + files=files) + request_status = response.status_code + request_content = json.loads(response.text) + + if request_status == 200: + logger.info(u"PlexPy Notifiers :: Telegram poster sent.") + elif request_status >= 400 and request_status < 500: + logger.warn(u"PlexPy Notifiers :: Telegram poster failed: %s" % request_content.get('description')) + else: + logger.warn(u"PlexPy Notifiers :: Telegram poster failed.") + + data['text'] = text + if self.html_support: + data['parse_mode'] = 'HTML' + + http_handler = HTTPSConnection("api.telegram.org") + http_handler.request('POST', + '/bot%s/%s' % (self.bot_token, 'sendMessage'), + headers={'Content-type': 'application/x-www-form-urlencoded'}, + body=urlencode(data)) response = http_handler.getresponse() request_status = response.status @@ -1733,11 +1757,23 @@ class TELEGRAM(object): ' on Telegram to get an ID.', 'input_type': 'text' }, + {'label': 'Include Poster Image', + 'value': self.incl_poster, + 'name': 'telegram_incl_poster', + 'description': 'Include a poster with the notifications.', + 'input_type': 'checkbox' + }, {'label': 'Include Subject Line', 'value': self.incl_subject, 'name': 'telegram_incl_subject', 'description': 'Include the subject line with the notifications.', 'input_type': 'checkbox' + }, + {'label': 'Enable HTML Support', + 'value': self.html_support, + 'name': 'telegram_html_support', + 'description': 'Style your messages using these HTML tags: b, i, a[href], code, pre', + 'input_type': 'checkbox' } ]