forked from Jxck-S/plane-notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
defTelegram.py
90 lines (88 loc) · 3.65 KB
/
defTelegram.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
def sendTeleg(photo, message, config):
try:
from telegram import __version_info__
except ImportError:
__version_info__ = (0, 0, 0, 0, 0)
if __version_info__ < (20, 0, 0, "alpha", 5):
sent = sendTelegOld(photo, message, config)
return sent
else:
import asyncio
sent = asyncio.run(t_send_photo(photo,message,config))
return sent
def sendTelegOld(photo, message, config):
import telegram
sent = False
retry_c = 0
while sent == False:
try:
bot = telegram.Bot(token=config.get('TELEGRAM', 'BOT_TOKEN'), request=telegram.utils.request.Request(connect_timeout=20, read_timeout=20))
sent = bot.send_photo(chat_id=config.get('TELEGRAM', 'ROOM_ID'), photo=photo, caption=message, parse_mode=telegram.ParseMode.MARKDOWN, timeout=20)
except Exception as err:
print('err.args:')
print(err.args)
print(f"Unexpected {err=}, {type(err)=}")
print("\nString err:\n"+str(err))
if retry_c > 4:
print('Telegram attempts exceeded. Message not sent.')
break
elif str(err) == 'Unauthorized':
print('Invalid Telegram bot token, message not sent.')
break
elif str(err) == 'Timed out':
retry_c += 1
print('Telegram timeout count: '+str(retry_c))
pass
elif str(err) == 'Chat not found':
print('Invalid Telegram Chat ID, message not sent.')
break
elif str(err)[:35] == '[Errno 2] No such file or directory':
print('Telegram module couldn\'t find an image to sent.')
break
elif str(err) == 'Media_caption_too_long':
print('Telegram image caption length exceeds 1024 characters. Message not sent.')
break
else:
print('[X] Unknown Telegram error. Message not sent.')
break
else:
print("Telegram message successfully sent.")
return sent
async def t_send_photo(photo,message,config):
import telegram
sent = False
retry_c = 0
while sent == False:
try:
bot = telegram.Bot(token=config.get('TELEGRAM', 'BOT_TOKEN'))
sent = await bot.send_photo(chat_id=config.get('TELEGRAM', 'ROOM_ID'), photo=photo, caption=message)
except Exception as err:
print('err.args:')
print(err.args)
print(f"Unexpected {err=}, {type(err)=}")
print("\nString err:\n"+str(err))
if retry_c > 4:
print('Telegram attempts exceeded. Message not sent.')
break
elif str(err) == 'Unauthorized':
print('Invalid Telegram bot token, message not sent.')
break
elif str(err) == 'Timed out':
retry_c += 1
print('Telegram timeout count: '+str(retry_c))
pass
elif str(err) == 'Chat not found':
print('Invalid Telegram Chat ID, message not sent.')
break
elif str(err)[:35] == '[Errno 2] No such file or directory':
print('Telegram module couldn\'t find an image to send.')
break
elif str(err) == 'Media_caption_too_long':
print('Telegram image caption length exceeds 1024 characters. Message not sent.')
break
else:
print('[X] Unknown Telegram error. Message not sent.')
break
else:
print("Telegram message successfully sent.")
return sent