-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathYHlib.py
237 lines (234 loc) · 8.25 KB
/
YHlib.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from bottle import route,request,run
import requests
import json
import time
import functools
onMsgList=[]
onTxtMsgList=[]
onCmdDict={}
onFollowedList=[]
onUnfollowedList=[]
onJoinList=[]
onLeaveList=[]
btnEventDict={}
reply={}
tok=''
def setToken(token):
global tok
tok=token
def editMsg(msgId,recvId,recvType,contentType,content='content',fileName='fileName',url='url',buttons=False):
global headers,sjson,tok,reply
headers = {'Content-Type': 'application/json'}
sampleDict={
"msgId": msgId,
"recvId": recvId,
"recvType": recvType,
"contentType": contentType,
"content": {
"text": content
}
}
if contentType=='image':
sampleDict['content']={'imageUrl':url}
if contentType=='file':
sampleDict['content']={'fileName':fileName,'fileUrl':url}
if buttons!=False:
sampleDict['content']['buttons']=[buttons]
sjson=json.dumps(sampleDict)
#print(sjson)
response = requests.request("POST", "https://chat-go.jwzhd.com/open-apis/v1/bot/edit?token={}".format(tok), headers=headers, data=sjson)
reply=json.loads(response.text)
print(reply)
def sendMsg(recvId,recvType,contentType,content='content',fileName='fileName',url='url',buttons=False):
global headers,sjson,tok,reply
headers = {'Content-Type': 'application/json'}
sampleDict={
"recvId": recvId,
"recvType": recvType,
"contentType": contentType,
"content": {
"text": content
}
}
if contentType=='image':
sampleDict['content']={'imageUrl':url}
if contentType=='file':
sampleDict['content']={'fileName':fileName,'fileUrl':url}
if buttons!=False:
sampleDict['content']['buttons']=[buttons]
if type(recvId)==list:
#sampleDict.update({'recvIds':sampleDict.pop("recvId")})
#response=requests.request("POST", "https://chat-go.jwzhd.com/open-apis/v1/bot/batch_send?token={}".format(tok), headers=headers, data=sjson)
#reply=json.loads(response.text)
#print(reply)
##Alternative
for yid in recvId:
sampleDict['recvId']=yid
sjson=json.dumps(sampleDict)
response = requests.request("POST", "https://chat-go.jwzhd.com/open-apis/v1/bot/send?token={}".format(tok), headers=headers, data=sjson)
reply=json.loads(response.text)
print(reply)
time.sleep(0.1)
else:
sjson=json.dumps(sampleDict)
#print(sjson)
response = requests.request("POST", "https://chat-go.jwzhd.com/open-apis/v1/bot/send?token={}".format(tok), headers=headers, data=sjson)
reply=json.loads(response.text)
print(reply)
def geneBaseBox(json,cnt=True,btn=True):
msgbox={}
msgbox["type"]=json["header"]["eventType"]
if cnt:
msgbox["contentType"]=json['event']['message']['contentType']
if msgbox['contentType'] in ('text','markdown'):
msgbox['msg']=json["event"]["message"]["content"]["text"]
elif msgbox['contentType']=='image':
msgbox['url']=json['event']['message']['content']['imageUrl']
elif msgbox['contentType']=='file':
msgbox['fileName']=json['event']['message']['content']['fileName']
msgbox['url']=json['event']['message']['content']['fileUrl']
elif msgbox['contentType']=='form':
msgbox['form']=json['event']['message']['content']['formJson']
msgbox['sender']=json["event"]["sender"]["senderId"]
msgbox['senderInfo']={'nickname':json['event']['sender']['senderNickname'],'level':json['event']['sender']['senderUserLevel']}
if msgbox['type']=='group' and cnt:
msgbox["id"]=json["event"]["message"]["chatId"]
elif msgbox['type']=='group' :
msgbox['id']=json['event']['chatId']
msgbox['nickname']=json['event']['nickname']
msgbox['avatar']=json['event']['avatarUrl']
msgbox['sender']=json["event"]["userId"]
elif cnt:
msgbox["id"]=json["event"]["sender"]["senderId"]
else:
msgbox['id']=json['event']['userId']
msgbox['sender']=json['event']['userId']
msgbox['nickname']=json['event']['nickname']
msgbox['avatar']=json['event']['avatarUrl']
if msgbox['type']=='group':
msgbox['recvType']='group'
elif msgbox['type']=='bot':
msgbox['recvType']='user'
return msgbox
@route("/sub",method='POST')
def onRecvPost():
global sender
json=request.json
#print(json['header']['eventType'])
if json['header']['eventType']=="message.receive.normal":
#print(json)
msgbox=geneBaseBox(json)
for func in onMsgList:
func(ctx=msgbox)
if msgbox['contentType'] in ("text","markdown"):
for func in onTxtMsgList:
func(ctx=msgbox)
elif json['header']['eventType']=='message.receive.instruction':
cmd=json['event']['message']['commandName']
if cmd in onCmdDict:
msgbox=geneBaseBox(json)
msgbox['cmd']=cmd
onCmdDict[cmd](ctx=msgbox)
elif json['header']['eventType']=='bot.followed':
msgbox=geneBaseBox(json,False)
for func in onFollowedList:
func(ctx=msgbox)
elif json['header']['eventType']=="bot.unfollowed":
msgbox=geneBaseBox(json,False)
for func in onUnfollowedList:
func(ctx=msgbox)
elif json['header']['eventType']=="group.join":
msgbox=geneBaseBox(json,False)
for func in onJoinList:
func(ctx=msgbox)
elif json['header']['eventType']=='group.leave':
msgbox=geneBaseBox(json,False)
for func in onLeaveList:
func(ctx=msgbox)
elif json['header']['eventType']=='button.report.inline':
msgbox={'type':json['event']['recvType'],'id':json['event']['recvId'],'value':json['event']['value']}
if msgbox['value'] in btnEventDict:
btnEventDict[msgbox['value']](ctx=msgbox)
@route("/ping",method="GET")
def ping():
return "pong"
class onLeave:
def __init__(self,func):
global onLeaveList
self.func=func
onLeaveList.append(func)
def __call__(self, *args, **kwds):
rv=self.func(*args,**kwds)
return rv
class onJoin:
def __init__(self,func):
global onJoinList
self.func=func
onJoinList.append(func)
def __call__(self, *args, **kwds):
rv=self.func(*args,**kwds)
return rv
class onUnfollowed:
def __init__(self,func):
global onUnfollowedList
self.func=func
onUnfollowedList.append(func)
def __call__(self, *args, **kwds):
rv=self.func(*args,**kwds)
return rv
class onFollowed:
def __init__(self,func):
global onFollowedList
self.func=func
onFollowedList.append(func)
def __call__(self, *args, **kwds):
rv=self.func(*args,**kwds)
return rv
class onTextMessage:
def __init__(self,func):
global onMsgList
self.func=func
onTxtMsgList.append(func)
def __call__(self, *args, **kwds):
rv=self.func(*args,**kwds)
return rv
class onMessage:
def __init__(self,func):
global onMsgList
self.func=func
onMsgList.append(func)
def __call__(self, *args, **kwds):
rv=self.func(*args,**kwds)
return rv
def onCommand(cmd):
def deco(func):
global onCmdDict
if cmd not in onCmdDict:
onCmdDict[cmd]=func
#print(onCmdDict)
def warpper(*args,**kwds):
try:
rv=func(*args,**kwds)
return rv
except:
pass
return warpper
return deco
def onButtonPressed(cmd):
def deco(func):
global btnEventDict
if cmd not in btnEventDict:
btnEventDict[cmd]=func
#print(onCmdDict)
def warpper(*args,**kwds):
try:
rv=func(*args,**kwds)
return rv
except:
pass
return warpper
return deco
def runBot(token='',port=7888):
global tok
tok=token
run(host='0.0.0.0', port=port,loader=True)