From 81ee44b60f7032ed9ec843474a4942a7be9cb7e2 Mon Sep 17 00:00:00 2001 From: Gianfranco Date: Fri, 21 Aug 2020 22:49:26 +0200 Subject: [PATCH 1/4] Added "silent_message" option for Telegram Agent Added a new checkbox in the notification telegram config in order to send new messages silently. In this way the telegram users will receive a notification with no sound. --- plexpy/notifiers.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index 0cb42e94..74a7e8dc 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- # This file is part of Tautulli. # @@ -3365,6 +3365,7 @@ class TELEGRAM(Notifier): _DEFAULT_CONFIG = {'bot_token': '', 'chat_id': '', 'disable_web_preview': 0, + 'silent_message': 0, 'html_support': 1, 'incl_subject': 1, 'incl_poster': 0 @@ -3410,6 +3411,9 @@ class TELEGRAM(Notifier): data['text'] = (text[:4093] + (text[4093:] and '...')).encode('utf-8') + if self.config['silent_message']: + data['disable_notification'] = True + if self.config['disable_web_preview']: data['disable_web_page_preview'] = True @@ -3460,6 +3464,12 @@ class TELEGRAM(Notifier): 'name': 'telegram_disable_web_preview', 'description': 'Disables automatic link previews for links in the message', 'input_type': 'checkbox' + }, + {'label': 'Enable Silent Messages', + 'value': self.config['silent_message'], + 'name': 'telegram_silent_message', + 'description': 'Sends the message silently. Users will receive a notification with no sound.', + 'input_type': 'checkbox' } ] From 21799116c5a895a7caeacd8afdfe437c4c2ef4f7 Mon Sep 17 00:00:00 2001 From: Gianfranco Date: Mon, 14 Sep 2020 12:46:45 +0200 Subject: [PATCH 2/4] Reworked the Telegram Agent code to include the "silent_message" option. Both cases are now managed and the alerts are being respected. Signed-off-by: Gianfranco --- plexpy/notifiers.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index 74a7e8dc..89bbd18a 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- # This file is part of Tautulli. # @@ -3398,25 +3398,29 @@ class TELEGRAM(Notifier): poster_filename = 'poster_{}.png'.format(pretty_metadata.parameters['rating_key']) files = {'photo': (poster_filename, poster_content, 'image/png')} - if len(text) > 1024: + max_caption = len(text) + + if max_caption > 1024: data['disable_notification'] = True + self.make_request('https://api.telegram.org/bot{}/sendPhoto'.format(self.config['bot_token']), + data=data, files=files) + data.pop('disable_notification') #This prevents from alerting with 2 sounds Telegram when the Silent Message is OFF: one alert for the photo and the second one for the text else: data['caption'] = text.encode('utf-8') - - r = self.make_request('https://api.telegram.org/bot{}/sendPhoto'.format(self.config['bot_token']), + if self.config['silent_message']: + data['disable_notification'] = True + self.make_request('https://api.telegram.org/bot{}/sendPhoto'.format(self.config['bot_token']), data=data, files=files) - - if not data.pop('disable_notification', None): - return r + return data['text'] = (text[:4093] + (text[4093:] and '...')).encode('utf-8') - if self.config['silent_message']: - data['disable_notification'] = True - if self.config['disable_web_preview']: data['disable_web_page_preview'] = True + if self.config['silent_message']: + data['disable_notification'] = True + headers = {'Content-type': 'application/x-www-form-urlencoded'} return self.make_request('https://api.telegram.org/bot{}/sendMessage'.format(self.config['bot_token']), From f07bcca96a0f7518e5e0dfa7fa570a3a3d1c8064 Mon Sep 17 00:00:00 2001 From: Gianfranco Date: Mon, 14 Sep 2020 20:26:27 +0200 Subject: [PATCH 3/4] Wording changes Signed-off-by: Gianfranco --- plexpy/notifiers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index 89bbd18a..b939a28d 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -3469,10 +3469,10 @@ class TELEGRAM(Notifier): 'description': 'Disables automatic link previews for links in the message', 'input_type': 'checkbox' }, - {'label': 'Enable Silent Messages', + {'label': 'Enable Silent Notifications', 'value': self.config['silent_message'], 'name': 'telegram_silent_message', - 'description': 'Sends the message silently. Users will receive a notification with no sound.', + 'description': 'Send notifications silently without any alert sounds.', 'input_type': 'checkbox' } ] From 721cf5c93001cc01fdfe9081a8b6ee0b85dc6387 Mon Sep 17 00:00:00 2001 From: Gianfranco Date: Mon, 14 Sep 2020 20:46:22 +0200 Subject: [PATCH 4/4] Renamed 'silent_message' to 'silent_notification.' Signed-off-by: Gianfranco --- plexpy/notifiers.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index b939a28d..e4748faf 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -3365,7 +3365,7 @@ class TELEGRAM(Notifier): _DEFAULT_CONFIG = {'bot_token': '', 'chat_id': '', 'disable_web_preview': 0, - 'silent_message': 0, + 'silent_notification': 0, 'html_support': 1, 'incl_subject': 1, 'incl_poster': 0 @@ -3407,7 +3407,7 @@ class TELEGRAM(Notifier): data.pop('disable_notification') #This prevents from alerting with 2 sounds Telegram when the Silent Message is OFF: one alert for the photo and the second one for the text else: data['caption'] = text.encode('utf-8') - if self.config['silent_message']: + if self.config['silent_notification']: data['disable_notification'] = True self.make_request('https://api.telegram.org/bot{}/sendPhoto'.format(self.config['bot_token']), data=data, files=files) @@ -3418,7 +3418,7 @@ class TELEGRAM(Notifier): if self.config['disable_web_preview']: data['disable_web_page_preview'] = True - if self.config['silent_message']: + if self.config['silent_notification']: data['disable_notification'] = True headers = {'Content-type': 'application/x-www-form-urlencoded'} @@ -3470,8 +3470,8 @@ class TELEGRAM(Notifier): 'input_type': 'checkbox' }, {'label': 'Enable Silent Notifications', - 'value': self.config['silent_message'], - 'name': 'telegram_silent_message', + 'value': self.config['silent_notification'], + 'name': 'telegram_silent_notification', 'description': 'Send notifications silently without any alert sounds.', 'input_type': 'checkbox' }