-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtgbot.py
82 lines (74 loc) · 2.84 KB
/
tgbot.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
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import requests, json, os
import log
# 填充电报机器人的token
TG_BOT_URL = "https://api.telegram.org/bot%s/" % os.getenv("TG_BOT_TOKEN")
# 填充电报频道 chat_id
TG_CHAT_ID = os.getenv("TG_CHAT_ID")
def send_message(text):
payload = {
"method": "sendMessage",
"chat_id": TG_CHAT_ID,
"text": text,
"parse_mode": "Markdown",
}
log.logger.debug(log.SensitiveData(json.dumps(payload, ensure_ascii=False)))
try:
res = requests.post(TG_BOT_URL, json=payload)
res.raise_for_status()
except Exception as e:
log.logger.error(json.dumps(payload,ensure_ascii=False))
log.logger.error(res.text)
raise e
def send_photo(caption, photo):
payload = {
"method": "sendPhoto",
"chat_id": TG_CHAT_ID,
"photo": photo,
"caption": caption,
"parse_mode": "Markdown",
}
log.logger.debug(log.SensitiveData(json.dumps(payload, ensure_ascii=False)))
try:
res = requests.post(TG_BOT_URL, json=payload)
res.raise_for_status()
except Exception as e:
log.logger.error(json.dumps(payload,ensure_ascii=False))
log.logger.error(res.text)
raise e
def bot_authorization():
try:
res = requests.get(TG_BOT_URL + "getMe")
res.raise_for_status()
log.logger.debug(log.SensitiveData(res.text))
log.logger.info(f"Telegram bot authorization successful. Current bot: {res.json()['result']['username']}")
except requests.exceptions.ConnectionError as e:
log.logger.error(f"Telegram bot authorization failed. Check network connection: {e}")
raise e
except Exception as e:
log.logger.error(f"Telegram bot authorization failed. Error: {e}")
raise e
def get_chat():
payload = {
"method": "getChat",
"chat_id": TG_CHAT_ID,
}
try:
res = requests.post(TG_BOT_URL, json=payload)
res.raise_for_status()
log.logger.debug(log.SensitiveData(res.text))
chat_type = res.json()['result']['type']
if chat_type == 'private':
log.logger.info(f"Telegram getChat successful. Chat User: [{res.json()['result']['username']}], type: {chat_type}")
elif chat_type == 'channel':
log.logger.info(f"Telegram getChat successful. Chat title: [{res.json()['result']['title']}], type: {chat_type}")
else:
log.logger.warning(f"Telegram getChat successful. Chat type: {chat_type}, Chat Detail: {res.json()['result']}")
except requests.exceptions.ConnectionError as e:
log.logger.error(f"Telegram getChat failed. Check network connection: {e}")
raise e
except Exception as e:
log.logger.error(json.dumps(payload,ensure_ascii=False))
log.logger.error(f"Telegram getChat failed. Error: {e}")
raise e