-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_server.py
111 lines (97 loc) · 3.33 KB
/
main_server.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import json
import os
import requests
import flask
import logging
from flask import Flask, jsonify, has_request_context, copy_current_request_context, request
from functools import wraps
from concurrent.futures import Future, ThreadPoolExecutor
from get_spider_file import get_file_path
from getmsg import getmsg
import asyncio
def run_async(func):
@wraps(func)
def _wrapper(*args, **kwargs):
call_result = Future()
def _run():
loop = asyncio.new_event_loop()
try:
result = loop.run_until_complete(func(*args, **kwargs))
except Exception as error:
call_result.set_exception(error)
else:
call_result.set_result(result)
finally:
loop.close()
loop_executor = ThreadPoolExecutor(max_workers=1)
if has_request_context():
_run = copy_current_request_context(_run)
loop_future = loop_executor.submit(_run)
loop_future.result()
return call_result.result()
return _wrapper
app = flask.Flask(__name__)
@app.route('/weixin', methods=['POST'])
@run_async
async def main():
msg, from_name, final_from_name, time_str, from_wxid, final_from_wxid, msg_type, robot_wxid = getmsg()
# 这一块后面可以写插件主要内容
# new_msg = '123'
new_msg = ''
message = {
"success": True,
"message": "successful!",
"event": "SendTextMsg",
"robot_wxid": robot_wxid,
"to_wxid": from_wxid,
"member_wxid": "",
"member_name": "",
"group_wxid": "",
"msg": new_msg
}
if from_wxid in ['23584516242@chatroom', '48377160542@chatroom', 'wxid_rs2fy4y9i5rz22']:
# sendTextMsg(from_wxid="23584516242@chatroom", new_msg=new_msg, robot_wxid=robot_wxid)
path = get_file_path(msg)
# sendFile(from_wxid=from_wxid, path=path, robot_wxid=robot_wxid)
if path:
sendFile(from_wxid='48377160542@chatroom', path=path, robot_wxid=robot_wxid)
else:
pass
return jsonify(message)
def sendTextMsg(from_wxid, new_msg, robot_wxid):
data = dict()
data["event"] = "SendTextMsg"
data["robot_wxid"] = robot_wxid
data["to_wxid"] = from_wxid
data["msg"] = str(new_msg)
res = requests.post(url="http://192.168.3.22:8090", data=json.dumps(data, ensure_ascii=False))
print(res.json())
def sendFile(from_wxid, path, robot_wxid):
# path = r"D:\workspace\wechatRob\output\2023-05-27\行路千万里.txt"
name = path.split(os.sep)[-1]
data = dict()
msg = dict()
data["event"] = "SendFileMsg"
data["robot_wxid"] = robot_wxid
data["to_wxid"] = from_wxid
msg['name'] = name
msg["path"] = path
data['msg'] = msg
res = requests.post(url="http://127.0.0.1:8090",json=data)
print(res.json())
def testsend():
from_wxid = "23584516242@chatroom"
new_msg = "开呢"
robot_wxid = "wxid_rs2fy4y9i5rz22"
sendTextMsg(from_wxid, new_msg, robot_wxid)
def runserver():
app.config['JSON_AS_ASCII'] = False
log = logging.getLogger('log')
log.disabled = True
app.run(host='0.0.0.0', port=8074, debug=True)
if __name__ == '__main__':
runserver()
# from_wxid = "23584516242@chatroom"
# new_msg = "开呢"
# robot_wxid = "wxid_rs2fy4y9i5rz22"
# sendTextMsg(from_wxid, new_msg, robot_wxid)