-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram_logger.py
55 lines (39 loc) · 1.37 KB
/
telegram_logger.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
from datetime import datetime
import subprocess
def send_telegram_message(log_message):
try:
command = [
'curl',
'-X', 'POST',
'https://api.telegram.org/(YOUR_BOT_KEY)/sendMessage',
'-d', f'chat_id=-(YOUR_CHAT_ID)&text={log_message}'
]
subprocess.run(command, check=True)
except subprocess.CalledProcessError as e:
with open('error_log.txt', 'a') as f:
f.write(str(e) + '\n')
def custom_print(*args, **kwargs):
original_print(*args, **kwargs)
message = " ".join(map(str, args))
send_telegram_message(message)
original_print = print
print = custom_print
def get_formatted_date_time():
now = datetime.now()
return now.strftime("%H:%M:%S on %d.%m.%Y")
def startup(browser, code):
print(f"APP HAS STARTED RUNNING AT {get_formatted_date_time()}")
def shutdown():
print(f"APP IS QUITTING AT {get_formatted_date_time()}")
# For testing purposes, calling the startup function with dummy arguments
startup(None, None)
try:
while True:
# The script will keep running indefinitely, listening to the print statements or any other triggers you set up
pass
except KeyboardInterrupt:
# Handle other exceptions if needed
pass
finally:
# This block will execute when the script is interrupted, calling the shutdown function
shutdown()