Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持IFTTT WebHooks #14

Merged
merged 5 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Twikoo 评论系统对不同的消息推送平台做了大量的适配工作,
- [iGot](https://push.hellyw.com/)
- [Telegram](https://core.telegram.org/bots)
- [飞书](https://www.feishu.cn/hc/zh-CN/articles/360024984973)
- [IFTTT](https://ifttt.com/maker_webhooks)

## 计划支持的推送平台

Expand Down Expand Up @@ -65,7 +66,7 @@ console.log(result);

| 参数 | 必填 | 默认 | 说明 |
| ---- | ---- | ---- | ---- |
| 平台名称 | ✅ | 无 | 字符串,平台名称的缩写,支持:`qmsg`、`serverchan`、`pushplus`、`pushplushxtrip`、`dingtalk`、`wecom`、`bark`、`gocqhttp`、`atri`、`pushdeer`、`igot`、`telegram`、`feishu` |
| 平台名称 | ✅ | 无 | 字符串,平台名称的缩写,支持:`qmsg`、`serverchan`、`pushplus`、`pushplushxtrip`、`dingtalk`、`wecom`、`bark`、`gocqhttp`、`atri`、`pushdeer`、`igot`、`telegram`、`feishu`、`ifttt` |
| token | ✅ | 无 | 平台用户身份标识,通常情况下是一串数字和字母组合,详情和示例见下方详细说明 |
| title | | 内容第一行 | 可选,消息标题,如果推送平台不支持消息标题,则会拼接在正文首行 |
| content | ✅ | 无 | Markdown 格式的推送内容,如果推送平台不支持 Markdown,pushoo 会自动转换成支持的格式 |
Expand All @@ -81,12 +82,18 @@ interface NoticeOptions {
* url 用于点击通知后跳转的地址
*/
url?: string
},
/**
* IFTTT通知方式的参数配置
*/
ifttt?: {
value1?: string
value2?: string
value3?: string
}
}
```



## 详细说明

### 💬 [Qmsg](https://qmsg.zendee.cn/) <sub>缩写: `qmsg`</sub>
Expand Down Expand Up @@ -220,3 +227,17 @@ Telegram 是自由的聊天工具,支持机器人 API,免费,中国大陆
2. 复制机器人的 Webhook,填入 pushoo 的 token 中

示例 token:`https://open.feishu.cn/open-apis/bot/v2/hook/393df85f-7b2c-4ff6-bd4f-*******3ed54`(完整的 Webhook)或者 `393df85f-7b2c-4ff6-bd4f-*******3ed54`(只保留 access token)

### 💬 [IFTTT](https://ifttt.com/maker_webhooks) <sub>缩写: `ifttt`</sub>

IFTTT Webhooks推送,免费。

1. 首先打开 [http://ifttt.com/maker](http://ifttt.com/maker),确保你的WebHooks服务是可以用;
2. 点击 `Document` 获取你的 **Key**, 点击 `Create` 开始创建一个Applet;
3. If this Then that, this选择WebHooks,Trigger选择Receive a web request,Event Name填一个有意义的,如 `push`;
4. if this then that, that选择Notification,参数填Value1 、Value2、Value3;
5. 将 2 步获取的 **key** 和 第 3 步设置的 **Event Name** 拼接到一起,中间用 “`#`” 号分隔,填入 pushoo 的 token 中。

示例 token:`d-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx#push`

PS: title 和 content 会分别对应 Value1 和 Value2,如果要设置 Value3 请在 options 设置
95 changes: 70 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from 'axios';
import axios, { Method } from 'axios';
import { marked } from 'marked';
import markdownToTxt from 'markdown-to-txt';

Expand All @@ -10,8 +10,16 @@ export interface NoticeOptions {
/**
* url 用于点击通知后跳转的地址
*/
url?: string
}
url?: string;
};
/**
* IFTTT通知方式的参数配置
*/
ifttt?: {
value1?: string;
value2?: string;
value3?: string;
};
}
export interface CommonOptions {
token: string;
Expand All @@ -38,6 +46,7 @@ export type ChannelType =
| 'igot'
| 'telegram'
| 'feishu'
| 'ifttt';

function checkParameters(options: any, requires: string[] = []) {
requires.forEach((require) => {
Expand All @@ -56,15 +65,13 @@ function getTxt(content: string) {
}

function getTitle(content: string) {
return getTxt(content)
.split('\n')[0];
return getTxt(content).split('\n')[0];
}

function removeUrlAndIp(content: string) {
const urlRegex = /(https?:\/\/[^\s]+)/g;
const ipRegex = /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/g;
return content.replace(urlRegex, '')
.replace(ipRegex, '');
return content.replace(urlRegex, '').replace(ipRegex, '');
}

/**
Expand Down Expand Up @@ -113,8 +120,7 @@ async function noticeServerChan(options: CommonOptions) {
checkParameters(options, ['token', 'content']);
let url: string;
let param: URLSearchParams;
if (options.token.substring(0, 3)
.toLowerCase() === 'sct') {
if (options.token.substring(0, 3).toLowerCase() === 'sct') {
url = 'https://sctapi.ftqq.com';
param = new URLSearchParams({
title: options.title || getTitle(options.content),
Expand Down Expand Up @@ -172,8 +178,7 @@ async function noticePushPlusHxtrip(options: CommonOptions) {
async function noticeDingTalk(options: CommonOptions) {
checkParameters(options, ['token', 'content']);
let url = 'https://oapi.dingtalk.com/robot/send?access_token=';
if (options.token.substring(0, 4)
.toLowerCase() === 'http') {
if (options.token.substring(0, 4).toLowerCase() === 'http') {
url = options.token;
} else {
url += options.token;
Expand All @@ -196,15 +201,20 @@ async function noticeDingTalk(options: CommonOptions) {
async function noticeWeCom(options: CommonOptions) {
checkParameters(options, ['token', 'content']);
const [corpid, corpsecret, agentid, touser = '@all'] = options.token.split('#');
checkParameters({
corpid,
corpsecret,
agentid,
}, ['corpid', 'corpsecret', 'agentid']);
checkParameters(
{
corpid,
corpsecret,
agentid,
},
['corpid', 'corpsecret', 'agentid'],
);
// 获取 Access Token
let accessToken;
try {
const accessTokenRes = await axios.get(`https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${corpid}&corpsecret=${corpsecret}`);
const accessTokenRes = await axios.get(
`https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${corpid}&corpsecret=${corpsecret}`,
);
accessToken = accessTokenRes.data.access_token;
} catch (e) {
console.error('获取企业微信 access token 失败,请检查 token', e);
Expand Down Expand Up @@ -232,8 +242,7 @@ async function noticeWeCom(options: CommonOptions) {
async function noticeBark(options: CommonOptions) {
checkParameters(options, ['token', 'content']);
let url = 'https://api.day.app/';
if (options.token.substring(0, 4)
.toLowerCase() === 'http') {
if (options.token.substring(0, 4).toLowerCase() === 'http') {
url = options.token;
} else {
url += options.token;
Expand Down Expand Up @@ -292,10 +301,13 @@ async function noticeIgot(options: CommonOptions) {
async function noticeTelegram(options: CommonOptions) {
checkParameters(options, ['token', 'content']);
const [tgToken, chatId] = options.token.split('#');
checkParameters({
tgToken,
chatId,
}, ['tgToken', 'chatId']);
checkParameters(
{
tgToken,
chatId,
},
['tgToken', 'chatId'],
);
let text = options.content;
if (options.title) {
text = `${options.title}\n\n${text}`;
Expand All @@ -317,8 +329,7 @@ async function noticeFeishu(options: CommonOptions) {
const v2 = 'https://open.feishu.cn/open-apis/bot/v2/hook/';
let url;
let params;
if (options.token.substring(0, 4)
.toLowerCase() === 'http') {
if (options.token.substring(0, 4).toLowerCase() === 'http') {
url = options.token;
} else {
url = v2 + options.token;
Expand All @@ -342,6 +353,38 @@ async function noticeFeishu(options: CommonOptions) {
return response.data;
}

/**
* https://ifttt.com/maker_webhooks
* http://ift.tt/webhooks_faq
*/
async function noticeIfttt(options: CommonOptions) {
checkParameters(options, ['token', 'content']);

const [token, eventName] = options.token.split('#');
checkParameters(
{
token,
eventName,
},
['token', 'eventName'],
);

const url = `https://maker.ifttt.com/trigger/${eventName}/with/key/${token}`;

const response = await axios.post(
url,
{
value1: options.options?.ifttt?.value1 || getTxt(options.title),
value2: options.options?.ifttt?.value2 || getTxt(options.content),
value3: options.options?.ifttt?.value3,
},
{
headers: { 'Content-Type': 'application/json' },
},
);
return response.data;
}

async function notice(channel: ChannelType, options: CommonOptions) {
try {
let data: any;
Expand All @@ -360,6 +403,7 @@ async function notice(channel: ChannelType, options: CommonOptions) {
igot: noticeIgot,
telegram: noticeTelegram,
feishu: noticeFeishu,
ifttt: noticeIfttt,
}[channel.toLowerCase()];
if (noticeFn) {
data = await noticeFn(options);
Expand Down Expand Up @@ -391,4 +435,5 @@ export {
noticeIgot,
noticeTelegram,
noticeFeishu,
noticeIfttt,
};