-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add basic telegram notification integration #96
Open
Nachtalb
wants to merge
1
commit into
RickDB:master
Choose a base branch
from
Nachtalb:na/telegram-integration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -221,6 +221,15 @@ Depending on your OS make sure to place the show name between single or double q | |||||||
|
||||||||
https://github.com/RickDB/PlexAniSync/wiki/Tautulli-sync-script | ||||||||
|
||||||||
### Telegram Integration | ||||||||
|
||||||||
We can inform you via Telegram about start / errors / finish of the sync process. This is especially useful for automatic daily syncs. In case an error occurs or a new anime got added that needs a custom mapping you are always informed. | ||||||||
|
||||||||
To enable this you first have to create a new Telegram Bot by talking to the neat helper [BotFather](https://t.me/BotFather). From him you get a "bot_token" which you have to save in the `settings.ini`. After that you also need a chat | ||||||||
where to send the messages to. For that add the bot to any chatroom and use a bot like [IDBot](https://t.me/myidbot) to get the chats id. Save this to `chat_id` in the `settings.ini`. | ||||||||
|
||||||||
Last but not least you have to enable the integration by settings `eneabled` in the `[TELEGRAM]` section of `settings.ini` to `True`. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
## Docker | ||||||||
|
||||||||
Docker version is located here: [PlexAniSync](https://github.com/RickDB/PlexAniSync/pkgs/container/plexanisync) | ||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# coding=utf-8 | ||
import logging | ||
import time | ||
|
||
from io import StringIO | ||
from requests import get, HTTPError | ||
|
||
logger = logging.getLogger("PlexAniSync") | ||
# Gets overriden by settings.ini | ||
settings = { | ||
'enabled': False, | ||
'errors_only': False, | ||
'bot_token': '', | ||
'chat_id': '', | ||
} | ||
|
||
# Keep track of errors for Telegram integration | ||
error_tracker = StringIO() | ||
handler = logging.StreamHandler(error_tracker) | ||
handler.setLevel(logging.WARNING) | ||
logger.addHandler(handler) | ||
|
||
|
||
def setup(ini_settings): | ||
global settings | ||
if ini_settings.get('enabled', '').lower().strip() == 'true': | ||
enabled = True | ||
bot_token, chat_id = ini_settings.get('bot_token', '').strip(), ini_settings.get('chat_id').strip() | ||
|
||
if not bot_token: | ||
logger.error('[TELEGRAM] Bot token not set, disabling telegram integration') | ||
enabled = False | ||
if not chat_id: | ||
logger.error('[TELEGRAM] Chat id not set, disabling telegram integration') | ||
enabled = False | ||
|
||
settings['enabled'] = enabled | ||
settings['bot_token'] = bot_token | ||
settings['chat_id'] = chat_id | ||
settings['errors_only'] = ini_settings.get('errors_only', '').lower().strip() == 'true' | ||
|
||
|
||
def send_message(text): | ||
if not settings['enabled']: | ||
return | ||
|
||
log_text = text if len(text) < 30 else (text[:27] + '...') | ||
try: | ||
response = get( | ||
f'https://api.telegram.org/bot{settings["bot_token"]}/sendMessage', | ||
data={ | ||
'chat_id': settings['chat_id'], | ||
'text': text, | ||
'parse_mode': 'markdown' | ||
}) | ||
response.raise_for_status() | ||
logger.info(f'[TELEGRAM] message sent to Telegram | chat_id: {settings["chat_id"]} | text: "{log_text}"') | ||
except HTTPError as e: | ||
logger.error(f'[TELEGRAM] Could not send message | chat_id: {settings["chat_id"]} | text: "{log_text}" | error: {e}') | ||
|
||
|
||
def starting_sync(version): | ||
if not settings['enabled'] or settings['errors_only']: | ||
return | ||
|
||
send_message(f'PlexAniSync - version: `{version}`\nSync Started - `{time.asctime()}`') | ||
|
||
|
||
def report_to_telegram(): | ||
if not settings['enabled']: | ||
return | ||
|
||
if error_tracker.getvalue(): | ||
error = '' | ||
for line in error_tracker.getvalue().split('\n'): | ||
if len(error) + len(line) + 3 > 4096: | ||
send_message(error) | ||
error = '' | ||
error += f'`{line}`\n' | ||
if error: | ||
send_message(error) | ||
|
||
if not settings['errors_only']: | ||
send_message(f'Sync Complete - `{time.asctime()}`') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would also be useful for the Tautulli mode.