Skip to content

Commit

Permalink
完善pushplus通知方式的代码逻辑 (#2454)
Browse files Browse the repository at this point in the history
* 1. pushplus通知方式返回结果优化
2. pushplus通知方式适配更多参数

* 细节bug修改

* 删除没用的空格

---------

Co-authored-by: 陈思远 <[email protected]>
Co-authored-by: 陈大人 <[email protected]>
  • Loading branch information
3 people authored Dec 13, 2024
1 parent ab27a4c commit c71abd8
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 25 deletions.
5 changes: 5 additions & 0 deletions back/data/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ export class IGotNotification extends NotificationBaseInfo {
export class PushPlusNotification extends NotificationBaseInfo {
public pushPlusToken = '';
public pushPlusUser = '';
public pushPlusTemplate = '';
public pushplusChannel = '';
public pushplusWebhook = '';
public pushplusCallbackUrl = '';
public pushplusTo = '';
}

export class WePlusBotNotification extends NotificationBaseInfo {
Expand Down
27 changes: 17 additions & 10 deletions back/services/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,19 +522,26 @@ export default class NotificationService {
}

private async pushPlus() {
const { pushPlusToken, pushPlusUser } = this.params;
const { pushPlusToken, pushPlusUser, pushplusWebhook, pushPlusTemplate, pushplusChannel, pushplusCallbackUrl, pushplusTo} = this.params;
const url = `https://www.pushplus.plus/send`;
try {
let body = {
...this.gotOption,
json: {
token: `${pushPlusToken}`,
title: `${this.title}`,
content: `${this.content.replace(/[\n\r]/g, '<br>')}`,
topic: `${pushPlusUser || ''}`,
template: `${pushPlusTemplate || 'html'}`,
channel: `${pushplusChannel || 'wechat'}`,
webhook: `${pushplusWebhook || ''}`,
callbackUrl: `${pushplusCallbackUrl || ''}`,
to: `${pushplusTo || ''}`
},
}

const res: any = await got
.post(url, {
...this.gotOption,
json: {
token: `${pushPlusToken}`,
title: `${this.title}`,
content: `${this.content.replace(/[\n\r]/g, '<br>')}`,
topic: `${pushPlusUser || ''}`,
},
})
.post(url, body)
.json();

if (res.code === 200) {
Expand Down
10 changes: 10 additions & 0 deletions sample/config.sample.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ export PUSH_PLUS_TOKEN=""
## 下方填写您的一对多推送的 "群组编码" ,(一对多推送下面->您的群组(如无则新建)->群组编码)
## 1. 需订阅者扫描二维码 2、如果您是创建群组所属人,也需点击“查看二维码”扫描绑定,否则不能接受群组消息推送
export PUSH_PLUS_USER=""
## 发送模板,支持html,txt,json,markdown,cloudMonitor,jenkins,route,pay
export PUSH_PLUS_TEMPLATE="html"
## 发送渠道,支持wechat,webhook,cp,mail,sms
export PUSH_PLUS_CHANNEL="wechat"
## webhook编码,可在pushplus公众号上扩展配置出更多渠道
export PUSH_PLUS_WEBHOOK=""
## 发送结果回调地址,会把推送最终结果通知到这个地址上
export PUSH_PLUS_CALLBACKURL=""
## 好友令牌,微信公众号渠道填写好友令牌,企业微信渠道填写企业微信用户id
export PUSH_PLUS_TO=""

## 9. 微加机器人
## 官方网站:http://www.weplusbot.com
Expand Down
27 changes: 19 additions & 8 deletions sample/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ const push_config = {
CHAT_URL: '', // synology chat url
CHAT_TOKEN: '', // synology chat token

// 官方文档:http://www.pushplus.plus/
PUSH_PLUS_TOKEN: '', // push+ 微信推送的用户令牌
PUSH_PLUS_USER: '', // push+ 微信推送的群组编码
// 官方文档:https://www.pushplus.plus/
PUSH_PLUS_TOKEN: '', // pushplus 推送的用户令牌
PUSH_PLUS_USER: '', // pushplus 推送的群组编码
PUSH_PLUS_TEMPLATE: 'html', // pushplus 发送模板,支持html,txt,json,markdown,cloudMonitor,jenkins,route,pay
PUSH_PLUS_CHANNEL: 'wechat', // pushplus 发送渠道,支持wechat,webhook,cp,mail,sms
PUSH_PLUS_WEBHOOK: '', // pushplus webhook编码,可在pushplus公众号上扩展配置出更多渠道
PUSH_PLUS_CALLBACKURL: '', // pushplus 发送结果回调地址,会把推送最终结果通知到这个地址上
PUSH_PLUS_TO: '', // pushplus 好友令牌,微信公众号渠道填写好友令牌,企业微信渠道填写企业微信用户id


// 微加机器人,官方网站:https://www.weplusbot.com/
WE_PLUS_BOT_TOKEN: '', // 微加机器人的用户令牌
Expand Down Expand Up @@ -765,14 +771,19 @@ function iGotNotify(text, desp, params = {}) {

function pushPlusNotify(text, desp) {
return new Promise((resolve) => {
const { PUSH_PLUS_TOKEN, PUSH_PLUS_USER } = push_config;
const { PUSH_PLUS_TOKEN, PUSH_PLUS_USER, PUSH_PLUS_TEMPLATE, PUSH_PLUS_CHANNEL, PUSH_PLUS_WEBHOOK, PUSH_PLUS_CALLBACKURL, PUSH_PLUS_TO } = push_config;
if (PUSH_PLUS_TOKEN) {
desp = desp.replace(/[\n\r]/g, '<br>'); // 默认为html, 不支持plaintext
const body = {
token: `${PUSH_PLUS_TOKEN}`,
title: `${text}`,
content: `${desp}`,
topic: `${PUSH_PLUS_USER}`,
template: `${PUSH_PLUS_TEMPLATE}`,
channel: `${PUSH_PLUS_CHANNEL}`,
webhook: `${PUSH_PLUS_WEBHOOK}`,
callbackUrl: `${PUSH_PLUS_CALLBACKURL}`,
to: `${PUSH_PLUS_TO}`
};
const options = {
url: `https://www.pushplus.plus/send`,
Expand All @@ -786,21 +797,21 @@ function pushPlusNotify(text, desp) {
try {
if (err) {
console.log(
`Push+ 发送${
`pushplus 发送${
PUSH_PLUS_USER ? '一对多' : '一对一'
}通知消息失败😞\n`,
err,
);
} else {
if (data.code === 200) {
console.log(
`Push+ 发送${
`pushplus 发送${
PUSH_PLUS_USER ? '一对多' : '一对一'
}通知消息完成🎉\n`,
}通知请求成功🎉,可根据流水号查询推送结果:${data.data}\n注意:请求成功并不代表推送成功,如未收到消息,请到pushplus官网使用流水号查询推送最终结果`,
);
} else {
console.log(
`Push+ 发送${
`pushplus 发送${
PUSH_PLUS_USER ? '一对多' : '一对一'
}通知消息异常 ${data.msg}\n`,
);
Expand Down
26 changes: 20 additions & 6 deletions sample/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ def print(text, *args, **kw):
'CHAT_URL': '', # synology chat url
'CHAT_TOKEN': '', # synology chat token

'PUSH_PLUS_TOKEN': '', # push+ 微信推送的用户令牌
'PUSH_PLUS_USER': '', # push+ 微信推送的群组编码
'PUSH_PLUS_TOKEN': '', # pushplus 推送的用户令牌
'PUSH_PLUS_USER': '', # pushplus 推送的群组编码
'PUSH_PLUS_TEMPLATE': 'html', # pushplus 发送模板,支持html,txt,json,markdown,cloudMonitor,jenkins,route,pay
'PUSH_PLUS_CHANNEL': 'wechat', # pushplus 发送渠道,支持wechat,webhook,cp,mail,sms
'PUSH_PLUS_WEBHOOK': '', # pushplus webhook编码,可在pushplus公众号上扩展配置出更多渠道
'PUSH_PLUS_CALLBACKURL': '', # pushplus 发送结果回调地址,会把推送最终结果通知到这个地址上
'PUSH_PLUS_TO': '', # pushplus 好友令牌,微信公众号渠道填写好友令牌,企业微信渠道填写企业微信用户id

'WE_PLUS_BOT_TOKEN': '', # 微加机器人的用户令牌
'WE_PLUS_BOT_RECEIVER': '', # 微加机器人的消息接收者
Expand Down Expand Up @@ -364,26 +369,35 @@ def chat(title: str, content: str) -> None:

def pushplus_bot(title: str, content: str) -> None:
"""
通过 push+ 推送消息。
通过 pushplus 推送消息。
"""
if not push_config.get("PUSH_PLUS_TOKEN"):
print("PUSHPLUS 服务的 PUSH_PLUS_TOKEN 未设置!!\n取消推送")
return
print("PUSHPLUS 服务启动")

url = "http://www.pushplus.plus/send"
url = "https://www.pushplus.plus/send"
data = {
"token": push_config.get("PUSH_PLUS_TOKEN"),
"title": title,
"content": content,
"topic": push_config.get("PUSH_PLUS_USER"),
"template": push_config.get("PUSH_PLUS_TEMPLATE"),
"channel": push_config.get("PUSH_PLUS_CHANNEL"),
"webhook": push_config.get("PUSH_PLUS_WEBHOOK"),
"callbackUrl": push_config.get("PUSH_PLUS_CALLBACKURL"),
"to": push_config.get("PUSH_PLUS_TO")
}
body = json.dumps(data).encode(encoding="utf-8")
headers = {"Content-Type": "application/json"}
response = requests.post(url=url, data=body, headers=headers).json()

if response["code"] == 200:
print("PUSHPLUS 推送成功!")
code = response["code"]
if code == 200:
print("PUSHPLUS 推送请求成功,可根据流水号查询推送结果:"+ response["data"])
print("注意:请求成功并不代表推送成功,如未收到消息,请到pushplus官网使用流水号查询推送最终结果")
elif code == 900 or code == 903 or code == 905 or code == 999 :
print(response["msg"])

else:
url_old = "http://pushplus.hxtrip.com/send"
Expand Down
5 changes: 5 additions & 0 deletions src/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@
"iGot的信息推送key,例如:https://push.hellyw.com/XXXXXXXX": "iGot information push key, e.g., https://push.hellyw.com/XXXXXXXX",
"微信扫码登录后一对一推送或一对多推送下面的token(您的Token),不提供PUSH_PLUS_USER则默认为一对一推送,参考 https://www.pushplus.plus/": "After WeChat scan login, one-to-one or one-to-many push using the provided token (your Token). If PUSH_PLUS_USER is not provided, it defaults to one-to-one push. See reference at https://www.pushplus.plus/",
"一对多推送的“群组编码”(一对多推送下面->您的群组(如无则创建)->群组编码,如果您是创建群组人。也需点击“查看二维码”扫描绑定,否则不能接受群组消息推送)": "The 'group code' for one-to-many push (one-to-many push -> your group (if none, create one) -> group code). If you are the creator of the group, you need to click 'View QR code' to scan and bind, otherwise, you won't receive group messages.",
"发送模板": "send template, can use type: 'html,txt,json,markdown,cloudMonitor,jenkins,route,pay'",
"发送渠道": "send channel, can use type: 'wechat,webhook,cp,mail,sms'",
"webhook编码": "webhook code",
"发送结果回调地址": "send result callback url",
"好友令牌": "friend token",
"用户令牌,扫描登录后 我的—>设置->令牌 中获取,参考 https://www.weplusbot.com/": "Token, which can be obtained after scanning and logging in, is available under 'My Account' -> 'Settings' -> 'Tokens'. Please refer to the instructions for detailed steps: https://www.weplusbot.com/",
"消息接收人": "message recipient",
"调用版本;专业版填写pro,个人版填写personal,为空默认使用专业版": "Version, you can specify 'pro' for the Professional version and 'personal' for the Personal version. If left blank, it will default to the Professional version.",
Expand Down
7 changes: 6 additions & 1 deletion src/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,13 @@
"好友": "好友",
"要发送的用户昵称或群名,如果目标是群,需要填群名,如果目标是好友,需要填好友昵称": "要发送的用户昵称或群名,如果目标是群,需要填群名,如果目标是好友,需要填好友昵称",
"iGot的信息推送key,例如:https://push.hellyw.com/XXXXXXXX": "iGot的信息推送key,例如:https://push.hellyw.com/XXXXXXXX",
"微信扫码登录后一对一推送或一对多推送下面的token(您的Token),不提供PUSH_PLUS_USER则默认为一对一推送,参考 https://www.pushplus.plus/": "微信扫码登录后一对一推送或一对多推送下面的token(您的Token),不提供PUSH_PLUS_USER则默认为一对一推送,参考 https://www.pushplus.plus/",
"微信扫码登录后一对一推送或一对多推送下面的token(您的Token),不提供PUSH_PLUS_USER则默认为一对一推送,参考 https://www.pushplus.plus/": "微信扫码登录后一对一推送或一对多推送下面的token(你的Token),不提供PUSH_PLUS_USER则默认为一对一推送,参考 https://www.pushplus.plus/",
"一对多推送的“群组编码”(一对多推送下面->您的群组(如无则创建)->群组编码,如果您是创建群组人。也需点击“查看二维码”扫描绑定,否则不能接受群组消息推送)": "一对多推送的“群组编码”(一对多推送下面->您的群组(如无则创建)->群组编码,如果您是创建群组人。也需点击“查看二维码”扫描绑定,否则不能接受群组消息推送)",
"发送模板": "发送模板,支持html,txt,json,markdown,cloudMonitor,jenkins,route,pay",
"发送渠道": "发送渠道,支持wechat,webhook,cp,mail,sms",
"webhook编码": "webhook编码,可在pushplus公众号上扩展配置出更多渠道",
"发送结果回调地址": "发送结果回调地址,会把推送最终结果通知到这个地址上",
"好友令牌": "好友令牌,微信公众号渠道填写好友令牌,企业微信渠道填写企业微信用户id",
"用户令牌,扫描登录后 我的—>设置->令牌 中获取,参考 https://www.weplusbot.com/": "用户令牌,扫描登录后 我的—>设置->令牌 中获取,参考 https://www.weplusbot.com/",
"消息接收人": "消息接收人",
"调用版本;专业版填写pro,个人版填写personal,为空默认使用专业版": "调用版本;专业版填写pro,个人版填写personal,为空默认使用专业版",
Expand Down
30 changes: 30 additions & 0 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,36 @@ export default {
'一对多推送的“群组编码”(一对多推送下面->您的群组(如无则创建)->群组编码,如果您是创建群组人。也需点击“查看二维码”扫描绑定,否则不能接受群组消息推送)',
),
},
{
label: 'pushplusTemplate',
tip: intl.get(
'发送模板',
),
},
{
label: 'pushplusChannel',
tip: intl.get(
'发送渠道',
),
},
{
label: 'pushplusWebhook',
tip: intl.get(
'webhook编码',
),
},
{
label: 'pushplusCallbackUrl',
tip: intl.get(
'发送结果回调地址',
),
},
{
label: 'pushplusTo',
tip: intl.get(
'好友令牌',
),
},
],
wePlusBot: [
{
Expand Down

0 comments on commit c71abd8

Please sign in to comment.