-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
53 lines (37 loc) · 1.08 KB
/
main.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
import time
import json
import threading
import websocket
from common.message_check import filter_heart_beat, check
api_url = 'ws://127.0.0.1:3001'
bot_id = '12345678910'
auth_token = ''
def on_message(ws, message):
message = json.loads(message)
is_heart_beat = filter_heart_beat(message)
if not is_heart_beat:
check(message)
else:
pass
def on_error(ws, error):
print(f'WebSocket 错误: {error}')
def on_close(ws, close_status):
print(f'WebSocket 连接已关闭: {close_status}')
def on_open(ws):
def run():
while True:
time.sleep(30) # 保持连接
thread = threading.Thread(target=run)
thread.start()
if __name__ == '__main__':
header = {
'Authorization': f'Bearer {auth_token}',
'bot_id': bot_id
}
ws = websocket.WebSocketApp(api_url,
header=header,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.on_open = on_open
ws.run_forever()