forked from madwind/flexget_qbittorrent_mod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram_mod.py
101 lines (85 loc) · 3.51 KB
/
telegram_mod.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import copy
from PIL import Image
from flexget import plugin
from flexget.components.notify.notifiers.telegram import TelegramNotifier, ChatIdEntry
from flexget.event import event
from flexget.manager import Session
from flexget.plugin import PluginWarning
try:
import telegram
from telegram.error import ChatMigrated, TelegramError
from telegram.utils.request import NetworkError
except ImportError:
telegram = None
TelegramError = None
ChatMigrated = None
_IMAGE_ATTR = 'image'
_TEXT_LIMIT = 4096
_PLUGIN_NAME = 'telegram_mod'
def dict_merge(dict1: dict, dict2: dict) -> dict:
for i in dict2:
if isinstance(dict1.get(i), dict) and isinstance(dict2.get(i), dict):
dict_merge(dict1[i], dict2[i])
else:
dict1[i] = dict2[i]
return dict1
class TelegramNotifierMod(TelegramNotifier):
schema = dict_merge(copy.deepcopy(TelegramNotifier.schema), {
'properties': {
_IMAGE_ATTR: {'type': 'string'}
}
})
def notify(self, title, message: str, config) -> None:
if not message.strip():
return
session = Session()
chat_ids = self._real_init(session, config)
if not chat_ids:
return
msg_limits = self._get_msg_limits(message)
for msg_limit in msg_limits:
self._send_msgs(msg_limit, chat_ids, session)
if self._image:
self._send_photo(self._image, chat_ids, session)
def _parse_config(self, config):
super(TelegramNotifierMod, self)._parse_config(config)
self._image = config.get(_IMAGE_ATTR)
def _get_msg_limits(self, msg: str) -> list[str]:
msg_limits = ['']
if len(msg) < _TEXT_LIMIT:
return [msg]
msg_lines = msg.split('\n')
for line in msg_lines:
if len(msg_limits[-1] + line) > _TEXT_LIMIT and len(msg_limits[-1]) > 0:
msg_limits.append('')
msg_limits[-1] = msg_limits[-1] + line
return msg_limits
def _send_photo(self, image: str, chat_ids: list[ChatIdEntry], session: Session) -> None:
for chat_id in (x.id for x in chat_ids):
try:
photo = Image.open(image)
width = photo.width
height = photo.height
if width + height > 10000 or width / height > 20:
try:
self._bot.sendDocument(chat_id=chat_id, document=open(image, 'rb'))
except ChatMigrated as e:
try:
self._bot.sendDocument(chat_id=e.new_chat_id, document=open(image, 'rb'))
self._replace_chat_id(chat_id, e.new_chat_id, session)
except TelegramError as e:
raise PluginWarning(e.message)
else:
try:
self._bot.sendPhoto(chat_id=chat_id, photo=open(image, 'rb'))
except ChatMigrated as e:
try:
self._bot.sendPhoto(chat_id=e.new_chat_id, photo=open(image, 'rb'))
self._replace_chat_id(chat_id, e.new_chat_id, session)
except TelegramError as e:
raise PluginWarning(e.message)
except TelegramError as e:
raise PluginWarning(e.message)
@event('plugin.register')
def register_plugin() -> None:
plugin.register(TelegramNotifierMod, _PLUGIN_NAME, api_ver=2, interfaces=['notifiers'])