Skip to content

Commit

Permalink
修复cqhttp通知
Browse files Browse the repository at this point in the history
  • Loading branch information
zhonghuaifang committed Feb 7, 2023
1 parent d81ee50 commit cfb6f1b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 45 deletions.
34 changes: 23 additions & 11 deletions function/bika/sendNotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def serverNotify(self, text, desp):
'text': text,
'desp': desp
}
response = json.dumps(requests.post(url, data).json(), ensure_ascii=False)
response = json.dumps(requests.post(
url, data).json(), ensure_ascii=False)
datas = json.loads(response)
# print(datas)
if datas['code'] == 0:
Expand All @@ -154,7 +155,8 @@ def BarkNotify(self, text, desp):
url = sendNotify.BARK_PUSH + '/' + urllib.parse.quote(text) + '/' + urllib.parse.quote(
desp) + '?sound=' + sendNotify.BARK_SOUND
headers = {'Content-type': "application/x-www-form-urlencoded"}
response = json.dumps(requests.get(url, headers=headers).json(), ensure_ascii=False)
response = json.dumps(requests.get(
url, headers=headers).json(), ensure_ascii=False)
data = json.loads(response)
# print(data)
if data['code'] == 400:
Expand All @@ -175,7 +177,8 @@ def tgBotNotify(self, text, desp):
headers = {'Content-type': "application/x-www-form-urlencoded"}
body = 'chat_id=' + sendNotify.TG_USER_ID + '&text=' + urllib.parse.quote(
text) + '\n\n' + urllib.parse.quote(desp) + '&disable_web_page_preview=true'
response = json.dumps(requests.post(url, data=body, headers=headers).json(), ensure_ascii=False)
response = json.dumps(requests.post(
url, data=body, headers=headers).json(), ensure_ascii=False)

data = json.loads(response)
if data['ok']:
Expand Down Expand Up @@ -209,11 +212,14 @@ def dingNotify(self, text, desp):
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
hmac_code = hmac.new(
secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
url = 'https://oapi.dingtalk.com/robot/send?access_token=' + sendNotify.DD_BOT_TOKEN + '&timestamp=' + timestamp + '&sign=' + sign
url = 'https://oapi.dingtalk.com/robot/send?access_token=' + \
sendNotify.DD_BOT_TOKEN + '&timestamp=' + timestamp + '&sign=' + sign

response = requests.post(url=url, data=json.dumps(data), headers=headers).text
response = requests.post(
url=url, data=json.dumps(data), headers=headers).text
if json.loads(response)['errcode'] == 0:
print('\n钉钉发送通知消息成功\n')
else:
Expand Down Expand Up @@ -258,7 +264,8 @@ def pushNotify(self, text, desp):
}
body = json.dumps(data).encode(encoding='utf-8')
headers = {'Content-Type': 'application/json'}
response = json.dumps(requests.post(url, data=body, headers=headers).json(), ensure_ascii=False)
response = json.dumps(requests.post(
url, data=body, headers=headers).json(), ensure_ascii=False)
datas = json.loads(response)
if datas['code'] == 200:
print('\npush+发送通知消息成功\n')
Expand All @@ -275,14 +282,16 @@ def sendWechat(self, desp):
if sendNotify.QYWX_AM != '':
# 获得access_token
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
token_param = '?corpid=' + sendNotify.QYWX_AM.split(',')[0] + '&corpsecret=' + sendNotify.QYWX_AM.split(',')[1]
token_param = '?corpid=' + \
sendNotify.QYWX_AM.split(
',')[0] + '&corpsecret=' + sendNotify.QYWX_AM.split(',')[1]
token_data = requests.get(url + token_param)
token_data.encoding = 'utf-8'
token_data = token_data.json()
access_token = token_data['access_token']
#发送内容
# 发送内容
content = desp
#创建要发送的消息
# 创建要发送的消息
data = {
"touser": sendNotify.QYWX_AM.split(',')[2],
"msgtype": "text",
Expand All @@ -308,7 +317,10 @@ def go_cqhttp(self, title, content):
return
print("go-cqhttp 服务启动")

url = f'{sendNotify.GOBOT_URL}?access_token={sendNotify.GOBOT_TOKEN}&{sendNotify.GOBOT_QQ}&message=标题:{title}\n内容:{content}'
if '/send_group_msg' in sendNotify.GOBOT_URL:
url = f'{sendNotify.GOBOT_URL}?access_token={sendNotify.GOBOT_TOKEN}&group_id={sendNotify.GOBOT_QQ}&message=标题:{title}\n内容:{content}'
else:
url = f'{sendNotify.GOBOT_URL}?access_token={sendNotify.GOBOT_TOKEN}&user_id={sendNotify.GOBOT_QQ}&message=标题:{title}\n内容:{content}'

try:
response = requests.get(url, timeout=15)
Expand Down
34 changes: 23 additions & 11 deletions function/bilibili/sendNotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def serverNotify(self, text, desp):
'text': text,
'desp': desp
}
response = json.dumps(requests.post(url, data).json(), ensure_ascii=False)
response = json.dumps(requests.post(
url, data).json(), ensure_ascii=False)
datas = json.loads(response)
# print(datas)
if datas['code'] == 0:
Expand All @@ -154,7 +155,8 @@ def BarkNotify(self, text, desp):
url = sendNotify.BARK_PUSH + '/' + urllib.parse.quote(text) + '/' + urllib.parse.quote(
desp) + '?sound=' + sendNotify.BARK_SOUND
headers = {'Content-type': "application/x-www-form-urlencoded"}
response = json.dumps(requests.get(url, headers=headers).json(), ensure_ascii=False)
response = json.dumps(requests.get(
url, headers=headers).json(), ensure_ascii=False)
data = json.loads(response)
# print(data)
if data['code'] == 400:
Expand All @@ -175,7 +177,8 @@ def tgBotNotify(self, text, desp):
headers = {'Content-type': "application/x-www-form-urlencoded"}
body = 'chat_id=' + sendNotify.TG_USER_ID + '&text=' + urllib.parse.quote(
text) + '\n\n' + urllib.parse.quote(desp) + '&disable_web_page_preview=true'
response = json.dumps(requests.post(url, data=body, headers=headers).json(), ensure_ascii=False)
response = json.dumps(requests.post(
url, data=body, headers=headers).json(), ensure_ascii=False)

data = json.loads(response)
if data['ok']:
Expand Down Expand Up @@ -209,11 +212,14 @@ def dingNotify(self, text, desp):
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
hmac_code = hmac.new(
secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
url = 'https://oapi.dingtalk.com/robot/send?access_token=' + sendNotify.DD_BOT_TOKEN + '&timestamp=' + timestamp + '&sign=' + sign
url = 'https://oapi.dingtalk.com/robot/send?access_token=' + \
sendNotify.DD_BOT_TOKEN + '&timestamp=' + timestamp + '&sign=' + sign

response = requests.post(url=url, data=json.dumps(data), headers=headers).text
response = requests.post(
url=url, data=json.dumps(data), headers=headers).text
if json.loads(response)['errcode'] == 0:
print('\n钉钉发送通知消息成功\n')
else:
Expand Down Expand Up @@ -258,7 +264,8 @@ def pushNotify(self, text, desp):
}
body = json.dumps(data).encode(encoding='utf-8')
headers = {'Content-Type': 'application/json'}
response = json.dumps(requests.post(url, data=body, headers=headers).json(), ensure_ascii=False)
response = json.dumps(requests.post(
url, data=body, headers=headers).json(), ensure_ascii=False)
datas = json.loads(response)
if datas['code'] == 200:
print('\npush+发送通知消息成功\n')
Expand All @@ -275,14 +282,16 @@ def sendWechat(self, desp):
if sendNotify.QYWX_AM != '':
# 获得access_token
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
token_param = '?corpid=' + sendNotify.QYWX_AM.split(',')[0] + '&corpsecret=' + sendNotify.QYWX_AM.split(',')[1]
token_param = '?corpid=' + \
sendNotify.QYWX_AM.split(
',')[0] + '&corpsecret=' + sendNotify.QYWX_AM.split(',')[1]
token_data = requests.get(url + token_param)
token_data.encoding = 'utf-8'
token_data = token_data.json()
access_token = token_data['access_token']
#发送内容
# 发送内容
content = desp
#创建要发送的消息
# 创建要发送的消息
data = {
"touser": sendNotify.QYWX_AM.split(',')[2],
"msgtype": "text",
Expand All @@ -308,7 +317,10 @@ def go_cqhttp(self, title, content):
return
print("go-cqhttp 服务启动")

url = f'{sendNotify.GOBOT_URL}?access_token={sendNotify.GOBOT_TOKEN}&{sendNotify.GOBOT_QQ}&message=标题:{title}\n内容:{content}'
if '/send_group_msg' in sendNotify.GOBOT_URL:
url = f'{sendNotify.GOBOT_URL}?access_token={sendNotify.GOBOT_TOKEN}&group_id={sendNotify.GOBOT_QQ}&message=标题:{title}\n内容:{content}'
else:
url = f'{sendNotify.GOBOT_URL}?access_token={sendNotify.GOBOT_TOKEN}&user_id={sendNotify.GOBOT_QQ}&message=标题:{title}\n内容:{content}'

try:
response = requests.get(url, timeout=15)
Expand Down
2 changes: 1 addition & 1 deletion function/sendNotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function gobotNotify(text, desp, time = 2100) {
return new Promise((resolve) => {
if (GOBOT_URL) {
const options = {
url: `${GOBOT_URL}?access_token=${GOBOT_TOKEN}&${GOBOT_QQ}`,
url: `${GOBOT_URL}?access_token=${GOBOT_TOKEN}&${GOBOT_URL.indexOf("/send_group_msg") !== -1?'group_id=':'user_id='}${GOBOT_QQ}`,
json: {message:`${text}\n${desp}`},
headers: {
'Content-Type': 'application/json',
Expand Down
34 changes: 23 additions & 11 deletions function/sendNotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def serverNotify(self, text, desp):
'text': text,
'desp': desp
}
response = json.dumps(requests.post(url, data).json(), ensure_ascii=False)
response = json.dumps(requests.post(
url, data).json(), ensure_ascii=False)
datas = json.loads(response)
# print(datas)
if datas['code'] == 0:
Expand All @@ -154,7 +155,8 @@ def BarkNotify(self, text, desp):
url = sendNotify.BARK_PUSH + '/' + urllib.parse.quote(text) + '/' + urllib.parse.quote(
desp) + '?sound=' + sendNotify.BARK_SOUND
headers = {'Content-type': "application/x-www-form-urlencoded"}
response = json.dumps(requests.get(url, headers=headers).json(), ensure_ascii=False)
response = json.dumps(requests.get(
url, headers=headers).json(), ensure_ascii=False)
data = json.loads(response)
# print(data)
if data['code'] == 400:
Expand All @@ -175,7 +177,8 @@ def tgBotNotify(self, text, desp):
headers = {'Content-type': "application/x-www-form-urlencoded"}
body = 'chat_id=' + sendNotify.TG_USER_ID + '&text=' + urllib.parse.quote(
text) + '\n\n' + urllib.parse.quote(desp) + '&disable_web_page_preview=true'
response = json.dumps(requests.post(url, data=body, headers=headers).json(), ensure_ascii=False)
response = json.dumps(requests.post(
url, data=body, headers=headers).json(), ensure_ascii=False)

data = json.loads(response)
if data['ok']:
Expand Down Expand Up @@ -209,11 +212,14 @@ def dingNotify(self, text, desp):
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
hmac_code = hmac.new(
secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
url = 'https://oapi.dingtalk.com/robot/send?access_token=' + sendNotify.DD_BOT_TOKEN + '&timestamp=' + timestamp + '&sign=' + sign
url = 'https://oapi.dingtalk.com/robot/send?access_token=' + \
sendNotify.DD_BOT_TOKEN + '&timestamp=' + timestamp + '&sign=' + sign

response = requests.post(url=url, data=json.dumps(data), headers=headers).text
response = requests.post(
url=url, data=json.dumps(data), headers=headers).text
if json.loads(response)['errcode'] == 0:
print('\n钉钉发送通知消息成功\n')
else:
Expand Down Expand Up @@ -258,7 +264,8 @@ def pushNotify(self, text, desp):
}
body = json.dumps(data).encode(encoding='utf-8')
headers = {'Content-Type': 'application/json'}
response = json.dumps(requests.post(url, data=body, headers=headers).json(), ensure_ascii=False)
response = json.dumps(requests.post(
url, data=body, headers=headers).json(), ensure_ascii=False)
datas = json.loads(response)
if datas['code'] == 200:
print('\npush+发送通知消息成功\n')
Expand All @@ -275,14 +282,16 @@ def sendWechat(self, desp):
if sendNotify.QYWX_AM != '':
# 获得access_token
url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken'
token_param = '?corpid=' + sendNotify.QYWX_AM.split(',')[0] + '&corpsecret=' + sendNotify.QYWX_AM.split(',')[1]
token_param = '?corpid=' + \
sendNotify.QYWX_AM.split(
',')[0] + '&corpsecret=' + sendNotify.QYWX_AM.split(',')[1]
token_data = requests.get(url + token_param)
token_data.encoding = 'utf-8'
token_data = token_data.json()
access_token = token_data['access_token']
#发送内容
# 发送内容
content = desp
#创建要发送的消息
# 创建要发送的消息
data = {
"touser": sendNotify.QYWX_AM.split(',')[2],
"msgtype": "text",
Expand All @@ -308,7 +317,10 @@ def go_cqhttp(self, title, content):
return
print("go-cqhttp 服务启动")

url = f'{sendNotify.GOBOT_URL}?access_token={sendNotify.GOBOT_TOKEN}&{sendNotify.GOBOT_QQ}&message=标题:{title}\n内容:{content}'
if '/send_group_msg' in sendNotify.GOBOT_URL:
url = f'{sendNotify.GOBOT_URL}?access_token={sendNotify.GOBOT_TOKEN}&group_id={sendNotify.GOBOT_QQ}&message=标题:{title}\n内容:{content}'
else:
url = f'{sendNotify.GOBOT_URL}?access_token={sendNotify.GOBOT_TOKEN}&user_id={sendNotify.GOBOT_QQ}&message=标题:{title}\n内容:{content}'

try:
response = requests.get(url, timeout=15)
Expand Down
Loading

0 comments on commit cfb6f1b

Please sign in to comment.