From 8abb4a3de144bd2edb0b37e1e9266b89dcb08117 Mon Sep 17 00:00:00 2001 From: HCLonely Date: Mon, 13 May 2024 14:41:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EPushBullet,=20SimplePush,=20A?= =?UTF-8?q?nPush=E6=8E=A8=E9=80=81=E5=B9=B3=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/api-test.yml | 2 - README.md | 31 ++++ config/template.yaml.js | 122 +++++++++++++++ dist/AnPush.js | 95 +++++++++++ dist/PushBullet.js | 90 +++++++++++ dist/SimplePush.js | 91 +++++++++++ dist/index.js | 278 +++++++++++++++++++++++++++++++++ dist/types/AnPush.d.ts | 20 +++ dist/types/PushBullet.d.ts | 17 ++ dist/types/SimplePush.d.ts | 17 ++ dist/types/index.d.ts | 5 +- package.json | 2 +- src/AnPush.ts | 109 +++++++++++++ src/PushBullet.ts | 103 ++++++++++++ src/SimplePush.ts | 104 ++++++++++++ src/index.ts | 14 +- test/all-push-api.js | 25 +++ 17 files changed, 1120 insertions(+), 5 deletions(-) create mode 100644 dist/AnPush.js create mode 100644 dist/PushBullet.js create mode 100644 dist/SimplePush.js create mode 100644 dist/types/AnPush.d.ts create mode 100644 dist/types/PushBullet.d.ts create mode 100644 dist/types/SimplePush.d.ts create mode 100644 src/AnPush.ts create mode 100644 src/PushBullet.ts create mode 100644 src/SimplePush.ts diff --git a/.github/workflows/api-test.yml b/.github/workflows/api-test.yml index f6bb103..00229ad 100644 --- a/.github/workflows/api-test.yml +++ b/.github/workflows/api-test.yml @@ -2,8 +2,6 @@ name: Api Test on: schedule: - cron: '36 11 * * 6' -env: - version: 3.1.2 jobs: test: runs-on: ubuntu-latest diff --git a/README.md b/README.md index 4294bac..f18d9c2 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,9 @@ - [Ntfy](https://docs.ntfy.sh/publish/) -- Ntfy - [一封传话](https://www.phprm.com/push/h5/) -- YiFengChuanHua - [WPush](https://wpush.cn/) -- WPush +- [PushBullet](https://www.pushbullet.com/) -- PushBullet +- [SimplePush](https://simplepush.io/) -- SimplePush +- [AnPush](https://anpush.com/) -- AnPush ## 安装 @@ -328,6 +331,31 @@ const { PushApi } = require('all-pusher-api'); // 多平台同时推送 token: '******' } } + }, + { + name: 'PushBullet', + config: { + key: { + token: '******' + } + } + }, + { + name: 'SimplePush', + config: { + key: { + token: '******' + } + } + }, + { + name: 'AnPush', + config: { + key: { + token: '******', + channel: '******' + } + } } ]) .send({ message: '测试文本' })).map((e) => (e.result.status >= 200 && e.result.status < 300) ? `${e.name} 测试成功` : e)); @@ -607,16 +635,19 @@ const results: Array<{ - Showdoc: 'text' - Pushover: 'text' - 爱语飞飞: 'text' +- SimplePush: 'text' - QQ(go-cqhttp): 'text', 'other' - ~~Qmsg: 'text', 'other'~~ - Discord: 'text', 'other' - 飞书: 'text', 'other' +- PushBullet: 'text', 'other' - ~~NowPush: 'text', 'other'~~ - Chanify: 'text', 'other' - Bark: 'text', 'other' - Server酱Turbo: 'text', 'markdown' - 息知: 'text', 'markdown' - WPush: 'text', 'markdown' +- AnPush: 'text', 'markdown' - PushDeer: 'text', 'markdown', 'other' - QQ频道: 'text', 'markdown', 'other' - 企业微信: 'text', 'markdown', 'other' diff --git a/config/template.yaml.js b/config/template.yaml.js index e5af7c1..7dd7d00 100644 --- a/config/template.yaml.js +++ b/config/template.yaml.js @@ -1279,3 +1279,125 @@ const { WPush } = require("../dist/WPush") password: name: 代理密码 type: text +- name: PushBullet + type: json + filename: copy + quote: PushBullet配置参数生成器 + author: HCLonely + body: + key: + name: 认证信息 + type: object + body: + token: + name: Token + desp: Token + type: text + required: true + proxy: + name: 代理设置(可选) + type: object + body: + enable: + name: 启用 + type: boolean + defaultValue: false + protocol: + name: 代理协议 + type: text + defaultValue: http + host: + name: 代理主机地址 + type: text + port: + name: 代理端口 + type: text + username: + name: 代理用户名 + type: text + password: + name: 代理密码 + type: text +- name: SimplePush + type: json + filename: copy + quote: SimplePush配置参数生成器 + author: HCLonely + body: + key: + name: 认证信息 + type: object + body: + token: + name: Token + desp: Token + type: text + required: true + proxy: + name: 代理设置(可选) + type: object + body: + enable: + name: 启用 + type: boolean + defaultValue: false + protocol: + name: 代理协议 + type: text + defaultValue: http + host: + name: 代理主机地址 + type: text + port: + name: 代理端口 + type: text + username: + name: 代理用户名 + type: text + password: + name: 代理密码 + type: text +- name: AnPush + type: json + filename: copy + quote: AnPush配置参数生成器 + author: HCLonely + body: + key: + name: 认证信息 + type: object + body: + token: + name: Token + desp: Token + type: text + required: true + channel: + name: channel + desp: 推送通道id + type: text + required: true + proxy: + name: 代理设置(可选) + type: object + body: + enable: + name: 启用 + type: boolean + defaultValue: false + protocol: + name: 代理协议 + type: text + defaultValue: http + host: + name: 代理主机地址 + type: text + port: + name: 代理端口 + type: text + username: + name: 代理用户名 + type: text + password: + name: 代理密码 + type: text diff --git a/dist/AnPush.js b/dist/AnPush.js new file mode 100644 index 0000000..494912f --- /dev/null +++ b/dist/AnPush.js @@ -0,0 +1,95 @@ +'use strict'; + +var _defineProperty = require("@babel/runtime/helpers/defineProperty"); +var axios = require('axios'); +var tool = require('./tool'); +class AnPush { + constructor({ + token, + key, + channel, + proxy + }) { + _defineProperty(this, "_KEY", void 0); + _defineProperty(this, "baseURL", 'https://api.anpush.com/push/'); + _defineProperty(this, "httpsAgent", void 0); + _defineProperty(this, "channel", void 0); + const $key = { + token, + channel, + ...key + }; + if (!$key.token) { + throw new Error('Missing Parameter: token'); + } + if ($key.channel) { + this.channel = $key.channel; + } + this._KEY = $key.token; + if (proxy && proxy.enable) { + this.httpsAgent = tool.proxy2httpsAgent(proxy); + } + } + async send(sendOptions) { + if (!sendOptions.message && !sendOptions.customOptions) { + return { + status: 0, + statusText: 'Missing Parameter: message', + extraMessage: null + }; + } + let anPushOptions; + if (sendOptions.customOptions) { + anPushOptions = sendOptions.customOptions; + } else { + anPushOptions = { + channel: this.channel, + content: sendOptions.message, + title: sendOptions.title || sendOptions.message.split('\n')[0].trim().slice(0, 10) + }; + } + if (sendOptions.extraOptions) { + anPushOptions = { + ...anPushOptions, + ...sendOptions.extraOptions + }; + } + const axiosOptions = { + url: `${this.baseURL}${this._KEY}`, + method: 'POST', + headers: { + 'Content-type': 'application/x-www-form-urlencoded' + }, + data: anPushOptions + }; + if (this.httpsAgent) { + axiosOptions.httpsAgent = this.httpsAgent; + } + return axios(axiosOptions).then(response => { + if (response.data) { + if (response.data.code === 200) { + return { + status: 200, + statusText: 'Success', + extraMessage: response + }; + } + return { + status: 100, + statusText: 'Error', + extraMessage: response + }; + } + return { + status: 101, + statusText: 'No Response Data', + extraMessage: response + }; + }).catch(error => ({ + status: 102, + statusText: 'Request Error', + extraMessage: error + })); + } +} +exports.AnPush = AnPush; diff --git a/dist/PushBullet.js b/dist/PushBullet.js new file mode 100644 index 0000000..0eb1f22 --- /dev/null +++ b/dist/PushBullet.js @@ -0,0 +1,90 @@ +'use strict'; + +var _defineProperty = require("@babel/runtime/helpers/defineProperty"); +var axios = require('axios'); +var tool = require('./tool'); +class PushBullet { + constructor({ + token, + key, + proxy + }) { + _defineProperty(this, "_KEY", void 0); + _defineProperty(this, "baseURL", 'https://api.pushbullet.com/v2/pushes'); + _defineProperty(this, "httpsAgent", void 0); + const $key = { + token, + ...key + }; + if (!$key.token) { + throw new Error('Missing Parameter: token'); + } + this._KEY = $key.token; + if (proxy && proxy.enable) { + this.httpsAgent = tool.proxy2httpsAgent(proxy); + } + } + async send(sendOptions) { + if (!sendOptions.message && !sendOptions.customOptions) { + return { + status: 0, + statusText: 'Missing Parameter: message', + extraMessage: null + }; + } + let pushBulletOptions; + if (sendOptions.customOptions) { + pushBulletOptions = sendOptions.customOptions; + } else { + pushBulletOptions = { + type: 'note', + body: sendOptions.message, + title: sendOptions.title || sendOptions.message.split('\n')[0].trim().slice(0, 10) + }; + } + if (sendOptions.extraOptions) { + pushBulletOptions = { + ...pushBulletOptions, + ...sendOptions.extraOptions + }; + } + const axiosOptions = { + url: this.baseURL, + method: 'POST', + headers: { + 'Access-Token': this._KEY, + 'Content-type': 'application/json' + }, + data: pushBulletOptions + }; + if (this.httpsAgent) { + axiosOptions.httpsAgent = this.httpsAgent; + } + return axios(axiosOptions).then(response => { + if (response.data) { + if (response.status === 200) { + return { + status: 200, + statusText: 'Success', + extraMessage: response + }; + } + return { + status: 100, + statusText: 'Error', + extraMessage: response + }; + } + return { + status: 101, + statusText: 'No Response Data', + extraMessage: response + }; + }).catch(error => ({ + status: 102, + statusText: 'Request Error', + extraMessage: error + })); + } +} +exports.PushBullet = PushBullet; diff --git a/dist/SimplePush.js b/dist/SimplePush.js new file mode 100644 index 0000000..3031eed --- /dev/null +++ b/dist/SimplePush.js @@ -0,0 +1,91 @@ +'use strict'; + +var _defineProperty = require("@babel/runtime/helpers/defineProperty"); +var axios = require('axios'); +var tool = require('./tool'); +class SimplePush { + constructor({ + token, + key, + proxy + }) { + _defineProperty(this, "_KEY", void 0); + _defineProperty(this, "baseURL", 'https://api.simplepush.io/send'); + _defineProperty(this, "httpsAgent", void 0); + const $key = { + token, + ...key + }; + if (!$key.token) { + throw new Error('Missing Parameter: token'); + } + this._KEY = $key.token; + if (proxy && proxy.enable) { + this.httpsAgent = tool.proxy2httpsAgent(proxy); + } + } + async send(sendOptions) { + if (!sendOptions.message && !sendOptions.customOptions) { + return { + status: 0, + statusText: 'Missing Parameter: message', + extraMessage: null + }; + } + let simplePushOptions; + if (sendOptions.customOptions) { + simplePushOptions = sendOptions.customOptions; + } else { + simplePushOptions = { + key: this._KEY, + msg: sendOptions.message + }; + if (sendOptions.title) { + simplePushOptions.title = sendOptions.title; + } + } + if (sendOptions.extraOptions) { + simplePushOptions = { + ...simplePushOptions, + ...sendOptions.extraOptions + }; + } + const axiosOptions = { + url: this.baseURL, + method: 'POST', + headers: { + 'Content-type': 'application/x-www-form-urlencoded' + }, + data: simplePushOptions + }; + if (this.httpsAgent) { + axiosOptions.httpsAgent = this.httpsAgent; + } + return axios(axiosOptions).then(response => { + if (response.data) { + if (response.data.status === 'OK') { + return { + status: 200, + statusText: 'Success', + extraMessage: response + }; + } + return { + status: 100, + statusText: 'Error', + extraMessage: response + }; + } + return { + status: 101, + statusText: 'No Response Data', + extraMessage: response + }; + }).catch(error => ({ + status: 102, + statusText: 'Request Error', + extraMessage: error + })); + } +} +exports.SimplePush = SimplePush; diff --git a/dist/index.js b/dist/index.js index 4f05dcb..9dfba40 100644 --- a/dist/index.js +++ b/dist/index.js @@ -31,6 +31,266 @@ var Iyuu = require('./Iyuu'); var Ntfy = require('./Ntfy'); var YiFengChuanHua = require('./YiFengChuanHua'); var WPush = require('./WPush'); +var axios = require('axios'); +var tool = require('./tool'); +class PushBullet { + constructor({ + token, + key, + proxy + }) { + _defineProperty(this, "_KEY", void 0); + _defineProperty(this, "baseURL", 'https://api.pushbullet.com/v2/pushes'); + _defineProperty(this, "httpsAgent", void 0); + const $key = { + token, + ...key + }; + if (!$key.token) { + throw new Error('Missing Parameter: token'); + } + this._KEY = $key.token; + if (proxy && proxy.enable) { + this.httpsAgent = tool.proxy2httpsAgent(proxy); + } + } + async send(sendOptions) { + if (!sendOptions.message && !sendOptions.customOptions) { + return { + status: 0, + statusText: 'Missing Parameter: message', + extraMessage: null + }; + } + let pushBulletOptions; + if (sendOptions.customOptions) { + pushBulletOptions = sendOptions.customOptions; + } else { + pushBulletOptions = { + type: 'note', + body: sendOptions.message, + title: sendOptions.title || sendOptions.message.split('\n')[0].trim().slice(0, 10) + }; + } + if (sendOptions.extraOptions) { + pushBulletOptions = { + ...pushBulletOptions, + ...sendOptions.extraOptions + }; + } + const axiosOptions = { + url: this.baseURL, + method: 'POST', + headers: { + 'Access-Token': this._KEY, + 'Content-type': 'application/json' + }, + data: pushBulletOptions + }; + if (this.httpsAgent) { + axiosOptions.httpsAgent = this.httpsAgent; + } + return axios(axiosOptions).then(response => { + if (response.data) { + if (response.status === 200) { + return { + status: 200, + statusText: 'Success', + extraMessage: response + }; + } + return { + status: 100, + statusText: 'Error', + extraMessage: response + }; + } + return { + status: 101, + statusText: 'No Response Data', + extraMessage: response + }; + }).catch(error => ({ + status: 102, + statusText: 'Request Error', + extraMessage: error + })); + } +} +class SimplePush { + constructor({ + token, + key, + proxy + }) { + _defineProperty(this, "_KEY", void 0); + _defineProperty(this, "baseURL", 'https://api.simplepush.io/send'); + _defineProperty(this, "httpsAgent", void 0); + const $key = { + token, + ...key + }; + if (!$key.token) { + throw new Error('Missing Parameter: token'); + } + this._KEY = $key.token; + if (proxy && proxy.enable) { + this.httpsAgent = tool.proxy2httpsAgent(proxy); + } + } + async send(sendOptions) { + if (!sendOptions.message && !sendOptions.customOptions) { + return { + status: 0, + statusText: 'Missing Parameter: message', + extraMessage: null + }; + } + let simplePushOptions; + if (sendOptions.customOptions) { + simplePushOptions = sendOptions.customOptions; + } else { + simplePushOptions = { + key: this._KEY, + msg: sendOptions.message + }; + if (sendOptions.title) { + simplePushOptions.title = sendOptions.title; + } + } + if (sendOptions.extraOptions) { + simplePushOptions = { + ...simplePushOptions, + ...sendOptions.extraOptions + }; + } + const axiosOptions = { + url: this.baseURL, + method: 'POST', + headers: { + 'Content-type': 'application/x-www-form-urlencoded' + }, + data: simplePushOptions + }; + if (this.httpsAgent) { + axiosOptions.httpsAgent = this.httpsAgent; + } + return axios(axiosOptions).then(response => { + if (response.data) { + if (response.data.status === 'OK') { + return { + status: 200, + statusText: 'Success', + extraMessage: response + }; + } + return { + status: 100, + statusText: 'Error', + extraMessage: response + }; + } + return { + status: 101, + statusText: 'No Response Data', + extraMessage: response + }; + }).catch(error => ({ + status: 102, + statusText: 'Request Error', + extraMessage: error + })); + } +} +class AnPush { + constructor({ + token, + key, + channel, + proxy + }) { + _defineProperty(this, "_KEY", void 0); + _defineProperty(this, "baseURL", 'https://api.anpush.com/push/'); + _defineProperty(this, "httpsAgent", void 0); + _defineProperty(this, "channel", void 0); + const $key = { + token, + channel, + ...key + }; + if (!$key.token) { + throw new Error('Missing Parameter: token'); + } + if ($key.channel) { + this.channel = $key.channel; + } + this._KEY = $key.token; + if (proxy && proxy.enable) { + this.httpsAgent = tool.proxy2httpsAgent(proxy); + } + } + async send(sendOptions) { + if (!sendOptions.message && !sendOptions.customOptions) { + return { + status: 0, + statusText: 'Missing Parameter: message', + extraMessage: null + }; + } + let anPushOptions; + if (sendOptions.customOptions) { + anPushOptions = sendOptions.customOptions; + } else { + anPushOptions = { + channel: this.channel, + content: sendOptions.message, + title: sendOptions.title || sendOptions.message.split('\n')[0].trim().slice(0, 10) + }; + } + if (sendOptions.extraOptions) { + anPushOptions = { + ...anPushOptions, + ...sendOptions.extraOptions + }; + } + const axiosOptions = { + url: `${this.baseURL}${this._KEY}`, + method: 'POST', + headers: { + 'Content-type': 'application/x-www-form-urlencoded' + }, + data: anPushOptions + }; + if (this.httpsAgent) { + axiosOptions.httpsAgent = this.httpsAgent; + } + return axios(axiosOptions).then(response => { + if (response.data) { + if (response.data.code === 200) { + return { + status: 200, + statusText: 'Success', + extraMessage: response + }; + } + return { + status: 100, + statusText: 'Error', + extraMessage: response + }; + } + return { + status: 101, + statusText: 'No Response Data', + extraMessage: response + }; + }).catch(error => ({ + status: 102, + statusText: 'Request Error', + extraMessage: error + })); + } +} class PushApi { constructor(PushApiConfig) { _defineProperty(this, "pushers", []); @@ -217,6 +477,24 @@ class PushApi { pusher: new WPush.WPush(config.config) }); break; + case 'pushbullet': + this.pushers.push({ + name: config.name, + pusher: new PushBullet(config.config) + }); + break; + case 'simplepush': + this.pushers.push({ + name: config.name, + pusher: new SimplePush(config.config) + }); + break; + case 'anpush': + this.pushers.push({ + name: config.name, + pusher: new AnPush(config.config) + }); + break; } }); } diff --git a/dist/types/AnPush.d.ts b/dist/types/AnPush.d.ts new file mode 100644 index 0000000..4b41eea --- /dev/null +++ b/dist/types/AnPush.d.ts @@ -0,0 +1,20 @@ +import { AxiosRequestConfig } from 'axios'; +import { proxy, result, sendOptions } from './tool'; +interface AnPushConfig { + key?: { + token: string; + channel?: string; + }; + token?: string; + channel?: string; + proxy?: proxy; +} +declare class AnPush { + protected _KEY: string; + readonly baseURL = "https://api.anpush.com/push/"; + httpsAgent?: AxiosRequestConfig['httpsAgent']; + channel: string; + constructor({ token, key, channel, proxy }: AnPushConfig); + send(sendOptions: sendOptions): Promise; +} +export { AnPush }; diff --git a/dist/types/PushBullet.d.ts b/dist/types/PushBullet.d.ts new file mode 100644 index 0000000..2f0c767 --- /dev/null +++ b/dist/types/PushBullet.d.ts @@ -0,0 +1,17 @@ +import { AxiosRequestConfig } from 'axios'; +import { proxy, result, sendOptions } from './tool'; +interface PushBulletConfig { + key?: { + token: string; + }; + token?: string; + proxy?: proxy; +} +declare class PushBullet { + protected _KEY: string; + readonly baseURL = "https://api.pushbullet.com/v2/pushes"; + httpsAgent?: AxiosRequestConfig['httpsAgent']; + constructor({ token, key, proxy }: PushBulletConfig); + send(sendOptions: sendOptions): Promise; +} +export { PushBullet }; diff --git a/dist/types/SimplePush.d.ts b/dist/types/SimplePush.d.ts new file mode 100644 index 0000000..b1d3ad4 --- /dev/null +++ b/dist/types/SimplePush.d.ts @@ -0,0 +1,17 @@ +import { AxiosRequestConfig } from 'axios'; +import { proxy, result, sendOptions } from './tool'; +interface SimplePushConfig { + key?: { + token: string; + }; + token?: string; + proxy?: proxy; +} +declare class SimplePush { + protected _KEY: string; + readonly baseURL = "https://api.simplepush.io/send"; + httpsAgent?: AxiosRequestConfig['httpsAgent']; + constructor({ token, key, proxy }: SimplePushConfig); + send(sendOptions: sendOptions): Promise; +} +export { SimplePush }; diff --git a/dist/types/index.d.ts b/dist/types/index.d.ts index 65ad335..b735614 100644 --- a/dist/types/index.d.ts +++ b/dist/types/index.d.ts @@ -28,11 +28,14 @@ import { Iyuu } from './Iyuu'; import { Ntfy } from './Ntfy'; import { YiFengChuanHua } from './YiFengChuanHua'; import { WPush } from './WPush'; +import { PushBullet } from './PushBullet'; +import { SimplePush } from './SimplePush'; +import { AnPush } from './AnPush'; import { result, sendOptions } from './tool'; declare class PushApi { pushers: Array<{ name: string; - pusher: ServerChanTurbo | PushDeer | TelegramBot | DingTalk | WxPusher | Mail | FeiShu | WorkWeixin | QqChannel | PushPlus | Showdoc | Xizhi | Discord | GoCqhttp | Qmsg | WorkWeixinBot | Chanify | Bark | GoogleChat | Push | Slack | Pushback | Zulip | RocketChat | Gitter | Pushover | Iyuu | Ntfy | YiFengChuanHua | WPush; + pusher: ServerChanTurbo | PushDeer | TelegramBot | DingTalk | WxPusher | Mail | FeiShu | WorkWeixin | QqChannel | PushPlus | Showdoc | Xizhi | Discord | GoCqhttp | Qmsg | WorkWeixinBot | Chanify | Bark | GoogleChat | Push | Slack | Pushback | Zulip | RocketChat | Gitter | Pushover | Iyuu | Ntfy | YiFengChuanHua | WPush | PushBullet | SimplePush | AnPush; }>; constructor(PushApiConfig: Array<{ name: string; diff --git a/package.json b/package.json index c57f8d9..ab77b50 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "all-pusher-api", - "version": "1.3.1", + "version": "1.4.0", "description": "统一化推送服务API.", "main": "dist/index.js", "scripts": { diff --git a/src/AnPush.ts b/src/AnPush.ts new file mode 100644 index 0000000..3ac3603 --- /dev/null +++ b/src/AnPush.ts @@ -0,0 +1,109 @@ +import axios, { AxiosRequestConfig } from 'axios'; +import { proxy2httpsAgent, proxy, result, sendOptions } from './tool'; + +interface AnPushConfig { + key?: { + token: string + channel?: string + } + token?: string + channel?: string + proxy?: proxy +} +interface AnPushOptions { + channel: string + content: string + title?: string + [name: string]: any +} + +class AnPush { + protected _KEY: string; + readonly baseURL = 'https://api.anpush.com/push/'; + httpsAgent?: AxiosRequestConfig['httpsAgent']; + channel!: string; + + constructor({ token, key, channel, proxy }: AnPushConfig) { + const $key = { + token, + channel, + ...key + }; + if (!$key.token) { + throw new Error('Missing Parameter: token'); + } + if ($key.channel) { + this.channel = $key.channel; + } + this._KEY = $key.token; + if (proxy && proxy.enable) { + this.httpsAgent = proxy2httpsAgent(proxy); + } + } + + async send(sendOptions: sendOptions): Promise { + if (!sendOptions.message && !sendOptions.customOptions) { + return { + status: 0, + statusText: 'Missing Parameter: message', + extraMessage: null + }; + } + let anPushOptions: AnPushOptions; + if (sendOptions.customOptions) { + anPushOptions = sendOptions.customOptions; + } else { + anPushOptions = { + channel: this.channel, + content: sendOptions.message, + title: sendOptions.title || sendOptions.message.split('\n')[0].trim().slice(0, 10) + }; + } + + if (sendOptions.extraOptions) { + anPushOptions = { + ...anPushOptions, + ...sendOptions.extraOptions + }; + } + + const axiosOptions: AxiosRequestConfig = { + url: `${this.baseURL}${this._KEY}`, + method: 'POST', + headers: { + 'Content-type': 'application/x-www-form-urlencoded' + }, + data: anPushOptions + }; + if (this.httpsAgent) { + axiosOptions.httpsAgent = this.httpsAgent; + } + return axios(axiosOptions).then((response) => { + if (response.data) { + if (response.data.code === 200) { + return { + status: 200, + statusText: 'Success', + extraMessage: response + }; + } + return { + status: 100, + statusText: 'Error', + extraMessage: response + }; + } + return { + status: 101, + statusText: 'No Response Data', + extraMessage: response + }; + }).catch((error) => ({ + status: 102, + statusText: 'Request Error', + extraMessage: error + })); + } +} + +export { AnPush }; diff --git a/src/PushBullet.ts b/src/PushBullet.ts new file mode 100644 index 0000000..b86d65d --- /dev/null +++ b/src/PushBullet.ts @@ -0,0 +1,103 @@ +import axios, { AxiosRequestConfig } from 'axios'; +import { proxy2httpsAgent, proxy, result, sendOptions } from './tool'; + +interface PushBulletConfig { + key?: { + token: string + } + token?: string, + proxy?: proxy +} +interface PushBulletOptions { + type: 'note' | 'file' | 'link' + body: string + title?: string + [name: string]: any +} + +class PushBullet { + protected _KEY: string; + readonly baseURL = 'https://api.pushbullet.com/v2/pushes'; + httpsAgent?: AxiosRequestConfig['httpsAgent']; + + constructor({ token, key, proxy }: PushBulletConfig) { + const $key = { + token, + ...key + }; + if (!$key.token) { + throw new Error('Missing Parameter: token'); + } + this._KEY = $key.token; + if (proxy && proxy.enable) { + this.httpsAgent = proxy2httpsAgent(proxy); + } + } + + async send(sendOptions: sendOptions): Promise { + if (!sendOptions.message && !sendOptions.customOptions) { + return { + status: 0, + statusText: 'Missing Parameter: message', + extraMessage: null + }; + } + let pushBulletOptions: PushBulletOptions; + if (sendOptions.customOptions) { + pushBulletOptions = sendOptions.customOptions; + } else { + pushBulletOptions = { + type: 'note', + body: sendOptions.message, + title: sendOptions.title || sendOptions.message.split('\n')[0].trim().slice(0, 10) + }; + } + + if (sendOptions.extraOptions) { + pushBulletOptions = { + ...pushBulletOptions, + ...sendOptions.extraOptions + }; + } + + const axiosOptions: AxiosRequestConfig = { + url: this.baseURL, + method: 'POST', + headers: { + 'Access-Token': this._KEY, + 'Content-type': 'application/json' + }, + data: pushBulletOptions + }; + if (this.httpsAgent) { + axiosOptions.httpsAgent = this.httpsAgent; + } + return axios(axiosOptions).then((response) => { + if (response.data) { + if (response.status === 200) { + return { + status: 200, + statusText: 'Success', + extraMessage: response + }; + } + return { + status: 100, + statusText: 'Error', + extraMessage: response + }; + } + return { + status: 101, + statusText: 'No Response Data', + extraMessage: response + }; + }).catch((error) => ({ + status: 102, + statusText: 'Request Error', + extraMessage: error + })); + } +} + +export { PushBullet }; diff --git a/src/SimplePush.ts b/src/SimplePush.ts new file mode 100644 index 0000000..34f15d4 --- /dev/null +++ b/src/SimplePush.ts @@ -0,0 +1,104 @@ +import axios, { AxiosRequestConfig } from 'axios'; +import { proxy2httpsAgent, proxy, result, sendOptions } from './tool'; + +interface SimplePushConfig { + key?: { + token: string + } + token?: string, + proxy?: proxy +} +interface SimplePushOptions { + key: string + msg: string + title?: string + [name: string]: any +} + +class SimplePush { + protected _KEY: string; + readonly baseURL = 'https://api.simplepush.io/send'; + httpsAgent?: AxiosRequestConfig['httpsAgent']; + + constructor({ token, key, proxy }: SimplePushConfig) { + const $key = { + token, + ...key + }; + if (!$key.token) { + throw new Error('Missing Parameter: token'); + } + this._KEY = $key.token; + if (proxy && proxy.enable) { + this.httpsAgent = proxy2httpsAgent(proxy); + } + } + + async send(sendOptions: sendOptions): Promise { + if (!sendOptions.message && !sendOptions.customOptions) { + return { + status: 0, + statusText: 'Missing Parameter: message', + extraMessage: null + }; + } + let simplePushOptions: SimplePushOptions; + if (sendOptions.customOptions) { + simplePushOptions = sendOptions.customOptions; + } else { + simplePushOptions = { + key: this._KEY, + msg: sendOptions.message + }; + if (sendOptions.title) { + simplePushOptions.title = sendOptions.title; + } + } + + if (sendOptions.extraOptions) { + simplePushOptions = { + ...simplePushOptions, + ...sendOptions.extraOptions + }; + } + + const axiosOptions: AxiosRequestConfig = { + url: this.baseURL, + method: 'POST', + headers: { + 'Content-type': 'application/x-www-form-urlencoded' + }, + data: simplePushOptions + }; + if (this.httpsAgent) { + axiosOptions.httpsAgent = this.httpsAgent; + } + return axios(axiosOptions).then((response) => { + if (response.data) { + if (response.data.status === 'OK') { + return { + status: 200, + statusText: 'Success', + extraMessage: response + }; + } + return { + status: 100, + statusText: 'Error', + extraMessage: response + }; + } + return { + status: 101, + statusText: 'No Response Data', + extraMessage: response + }; + }).catch((error) => ({ + status: 102, + statusText: 'Request Error', + extraMessage: error + })); + } +} + +export { SimplePush }; diff --git a/src/index.ts b/src/index.ts index f2a530b..b3cae6d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,6 +28,9 @@ import { Iyuu } from './Iyuu'; import { Ntfy } from './Ntfy'; import { YiFengChuanHua } from './YiFengChuanHua'; import { WPush } from './WPush'; +import { PushBullet } from './PushBullet'; +import { SimplePush } from './SimplePush'; +import { AnPush } from './AnPush'; import { result, sendOptions } from './tool'; class PushApi { @@ -36,7 +39,7 @@ class PushApi { pusher: ServerChanTurbo | PushDeer | TelegramBot | DingTalk | WxPusher | Mail | FeiShu | WorkWeixin | QqChannel | PushPlus | Showdoc | Xizhi | Discord | GoCqhttp | Qmsg | WorkWeixinBot | Chanify | Bark | GoogleChat | Push | Slack | Pushback | Zulip | RocketChat | Gitter | Pushover | Iyuu | Ntfy | YiFengChuanHua | - WPush + WPush | PushBullet | SimplePush | AnPush }> = []; constructor(PushApiConfig: Array<{ name: string, config: any }>) { @@ -133,6 +136,15 @@ class PushApi { case 'wpush': this.pushers.push({ name: config.name, pusher: new WPush(config.config) }); break; + case 'pushbullet': + this.pushers.push({ name: config.name, pusher: new PushBullet(config.config) }); + break; + case 'simplepush': + this.pushers.push({ name: config.name, pusher: new SimplePush(config.config) }); + break; + case 'anpush': + this.pushers.push({ name: config.name, pusher: new AnPush(config.config) }); + break; default: break; } diff --git a/test/all-push-api.js b/test/all-push-api.js index 8f3a4c7..e66be23 100644 --- a/test/all-push-api.js +++ b/test/all-push-api.js @@ -262,6 +262,31 @@ const config = JSON.parse(process.env.CONFIG); token: config.WPush.token } } + }, + { + name: 'PushBullet', + config: { + key: { + token: config.PushBullet.token + } + } + }, + { + name: 'SimplePush', + config: { + key: { + token: config.SimplePush.token + } + } + }, + { + name: 'AnPush', + config: { + key: { + token: config.AnPush.token, + channel: config.AnPush.channel + } + } } ]) .send({ message: '测试文本' })).map((e) => ((e.result.status >= 200 && e.result.status < 300) ? `${e.name} 测试成功` : `${e.name} 测试失败`));