forked from Fortigate/AutoAFK
-
Notifications
You must be signed in to change notification settings - Fork 3
/
telegram.py
58 lines (46 loc) · 2.05 KB
/
telegram.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
import requests
from textwrap import dedent
import configparser
config = configparser.ConfigParser()
config.read('settings.ini')
class Telegram(object):
def __init__(self, chat_id: int, token: str, disable_notification: bool = False):
self.chat_id = chat_id
self.telegram_api = f"https://api.telegram.org/bot{token}/sendMessage"
self.disable_notification = disable_notification
def send(self, message: str) -> None:
requests.post(
url=self.telegram_api,
data={
"chat_id": self.chat_id,
"text": dedent(message),
"disable_web_page_preview": True,
"disable_notification": self.disable_notification,
},
)
if config.has_section('TELEGRAM') and config.getboolean('TELEGRAM', 'enable'):
# Initialize the Telegram object
telegram = Telegram(chat_id=config.getint("TELEGRAM","chat_id"), token=config.get("TELEGRAM","token"))
# Custom print function that duplicates output to console and Telegram
def print_and_send_to_telegram(*args, **kwargs):
# Convert all arguments to strings and join them
message = ' '.join(map(str, args))
# Print to console
built_in_print(*args, **kwargs)
# Check if message is empty
if message.strip():
# List of prefixes to check
prefixes = ['ERR', 'WAR', 'GRE', 'BLU', 'PUR']
# Check if the message starts with any of the prefixes
if any(message.startswith(prefix) for prefix in prefixes):
# Start from the fourth character
processed_message = message[3:]
# Send processed message to Telegram
telegram.send(processed_message)
else:
# Send the original message to Telegram
telegram.send(message)
# Save the built-in print function to avoid infinite recursion
built_in_print = print
# Replace the built-in print function with our custom function
print = print_and_send_to_telegram