-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.py
60 lines (48 loc) · 1.57 KB
/
wrapper.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
import os
import sys
import time
import json
import signal
import requests
import subprocess
process = None
def signal_handler(signum, frame):
global process
process.send_signal(signal.SIGINT)
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
def send_telegram_message(message):
url = f'https://api.telegram.org/bot{bot_token}/sendMessage'
data = {
'chat_id': owner_id,
'text': message
}
response = requests.post(url, data=data)
return response.json()
with open('bot_config.json', 'r') as f:
bot_config = json.load(f)
owner_id = bot_config['owner_id']
bot_token = bot_config['bot_token']
python_file = os.path.join(os.getcwd(), 'main.py')
send_telegram_message('Tütün Sabri başlatılıyor...')
while True:
time.sleep(5)
try:
# Run the python file
process = subprocess.Popen(['python', python_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# wait for the process to complete
stdout, stderr = process.communicate()
# get the exit code
exit_code = process.returncode
if(exit_code != 0):
stderr_str = stderr.decode('utf-8')
send_telegram_message(f'Program hata ile karşılaştı: {stderr_str}')
print(f'Exit code: {exit_code}: {stderr_str}')
# decode stdout
stdout_str = stdout.decode('utf-8')
output = stdout_str.strip().split('\n')
print(f'stdout: {output}')
except Exception as e:
send_telegram_message(f'Eyvah! : {e}')
print(f'Eyvah: {e}')
continue