forked from ziFieYu/WxbotManage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
158 lines (134 loc) · 6.04 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
# coding: utf-8
#sys.path.append('Lib/') #加载lib库,注意wxbot.py如果更新的话,就需要重启程序
import web
import json
import requests
import os
from traceback import format_exc
#
#f_hander = open(os.path.join(os.getcwd(),'static','main_log.txt'),'w')
#sys.stdout = f_hander
############################################################################################################################################################################
#webapi代码区
#数据库地址,WEB相关配置
web_db = web.database(dbn="sqlite", db=os.path.join(os.getcwd(),'data','weixin.db'))
render = web.template.render('webui_templates')
class index:
def GET(self):
if web.ctx.session.logged_in == True :
return render.index()
else:
raise web.seeother('/login.html')
class login:
def GET(self):
return render.login()
def POST(self):
try:
username=web.input()['username']
password=web.input()['password']
if username == 'admin' and password =='admin':
web.ctx.session.logged_in = True
raise web.seeother('/index.html')
return render.login()
except:
return render.login()
class webui:
def GET(self,action):
if web.ctx.session.logged_in != True :
raise web.seeother('/login.html')
if action == 'wx_login':
data = requests.get('http://127.0.0.1:1111/api/wx_login/').json()
return render.wx_login(data)
if action == 'bot_list':
data = requests.get('http://127.0.0.1:1111/api/get_wxbot_status/').json()
return render.bot_list(data)
if action == 'get_bot_conf':
bot_id = web.input()['bot_id']
data = requests.get('http://127.0.0.1:1111/api/get_bot_conf/?bot_id=%s'%(bot_id)).json()
bot_conf = json.dumps(data['data']['bot_conf'],ensure_ascii=False,indent=4)
return render.get_bot_conf(bot_id,bot_conf)
if action == 'get_contact_list':
bot_id = web.input()['bot_id']
data = requests.get('http://127.0.0.1:1111/api/get_contact_list/?bot_id=%s'%(bot_id)).json()
return render.get_contact_list(bot_id,data)
if action == 'get_group_list':
bot_id = web.input()['bot_id']
data = requests.get('http://127.0.0.1:1111/api/get_group_list/?bot_id=%s'%(bot_id)).json()
group_members = requests.get('http://127.0.0.1:1111/api/get_group_members/?bot_id=%s'%(bot_id)).json()['data']['group_members']
group_list = data['data']['group_list']
new_group_list = []
for temp in group_list:
try:
temp['MemberNums'] = len(group_members[temp['UserName']])
except:
temp['MemberNums'] = 0
new_group_list.append(temp)
data['data']['group_list'] = new_group_list
return render.get_group_list(bot_id,data)
if action == 'get_group_member_list':
bot_id = web.input()['bot_id']
gid = web.input()['gid']
group_members = requests.get('http://127.0.0.1:1111/api/get_group_members/?bot_id=%s'%(bot_id)).json()['data']['group_members']
return render.get_group_member_list(bot_id,gid,group_members[gid])
def POST(self,action):
if web.ctx.session.logged_in != True :
raise web.seeother('/login.html')
if action == 'delete_wxbot':
bot_id = web.input()['bot_id']
data = requests.get('http://127.0.0.1:1111/api/delete_wxbot/?bot_id=%s'%(bot_id)).json()
return '{"statusCode":"200","message":"操作成功","navTabId":"bot_list"}'
if action == 'update_bot_conf':
bot_id = web.input()['bot_id']
bot_conf = web.input()['bot_conf']
post_data = {'bot_id':bot_id,'bot_conf':bot_conf}
data = requests.post('http://127.0.0.1:1111/api/update_bot_conf/?bot_id=%s'%(bot_id),data=post_data).json()
return '{"statusCode":"200","message":"操作成功"}'
class api:
def GET(self,action):
try:
plugin = __import__("webapi."+action, fromlist=[action])
reload(plugin)
result = plugin.run(web.input(),action,bot_list)
except Exception,e:
result = {'code':500,'error_info':str(format_exc()),'data':''}
web.header('Content-Type','application/json')
return json.dumps(result,indent=4)
def POST(self,action):
try:
plugin = __import__("webapi."+action, fromlist=[action])
reload(plugin)
result = plugin.run(web.input(),action,bot_list)
except Exception,e:
result = {'code':500,'error_info':str(format_exc()),'data':''}
web.header('Content-Type','application/json')
return json.dumps(result,indent=4)
class not_found:
def GET(self,url):
raise web.seeother('/login.html')
def web_thread():
web.config.debug = False
urls = (
'/index.html','index',
'/login.html','login',
'/webui/(.+)/','webui',
'/api/(.+)/','api',
'(.+)','not_found'
)
app = web.application(urls, globals())
#定义seesion模型
store = web.session.DBStore(web_db, 'sessions')
session = web.session.Session(app, store, initializer = {
"logged_in": False
})
#hook seesion到所有的子应用
def session_hook():
web.ctx.session = session
app.add_processor(web.loadhook(session_hook))
app.run()
############################################################################################################################################################################
if __name__ == '__main__':
#机器人列表,机器人多开的时候使用,代开发
bot_list = []
#启动web线程
web_thread()