-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathapp.py
61 lines (43 loc) · 1.58 KB
/
app.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
from bot import *
import threading
from flask import Flask
status =""
app = Flask(__name__)
@app.route('/bot', methods=['GET'])
def bot_info():
try:
print(bot.get_me())
return str(bot.get_me())
except:
return "无法获取bot信息,请检查api token"
def run_bot():
while True:
try:
bot.polling(none_stop=True)
except Exception as e:
time.sleep(10)
@app.route('/', methods=['GET'])
def index():
global status
if status=="":
t1 = threading.Thread(target=run_bot) # 通过target指定子线程要执行的任务。可以通过args=元组 来指定test1的参数。
t1.start() # 只有在调用start方法后才会创建子线程并执行
print(t1.is_alive())
status=t1
# threading.enumerate() 打印正在执行的线程,包括主线程和子线程
#print(threading.enumerate())
return "正在唤醒Bot", 200
else:
print(status.is_alive())
if status.is_alive()==True:
return "Bot 已经在运行", 200
elif status.is_alive()==False:
t1 = threading.Thread(target=run_bot) # 通过target指定子线程要执行的任务。可以通过args=元组 来指定test1的参数。
t1.start() # 只有在调用start方法后才会创建子线程并执行
print(t1.is_alive())
status=t1
return "重新唤醒Bot", 200
#worker: python3 bot.py
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=True)