forked from S-i-l-v-e-t/YHChatPythonSDK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathYHlib.py
183 lines (180 loc) · 6.28 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
from bottle import route,request,run
import requests
import json
import time
onMsgList=[]
onTxtMsgList=[]
onCmdDict={}
onFollowedList=[]
onUnfollowedList=[]
onJoinList=[]
onLeaveList=[]
reply={}
tok=''
def setToken(token):
global tok
tok=token
def sendMsg(recvId,recvType,contentType,content='content',fileName='fileName',url='url',buttons=False):
global headers,sjson,tok
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:
#Official
#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):
msgbox={}
msgbox["type"]=json["event"]["chat"]["chatType"]
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"]
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']
return msgbox
@route("/sub",method='POST')
def onRecvPost():
global sender
json=request.json
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']['instruction']
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)
@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 onCommand:
def __init__(self,func,cmd=''):
global onCmdDict
self.func=func
onCmdDict[cmd]=func
def __call__(self,*args,**kwds):
rv=self.func(*args,**kwds)
return rv
class onTextMessage:
def __init__(self,func):
global onTxtMsgList
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 runBot(token='',port=7888):
global tok
tok=token
run(host='0.0.0.0', port=port,loader=True)