Skip to content

Commit

Permalink
Add webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
vilicvane committed Oct 16, 2023
1 parent 076772f commit 611bdaa
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ gtag('config', 'G-HD5ZM67WNQ');
{text: '定时执行', link: '/script/scheduling.md'},
{text: '添加依赖', link: '/script/dependencies.md'},
{text: '调试脚本', link: '/script/debugging.md'},
{text: 'Webhook', link: '/script/webhook.md'},
{text: '状态 State', link: '/script/state.md'},
{text: '消息 Message', link: '/script/message.md'},
{text: '高级用法', link: '/script/advanced-usages.md'},
Expand Down
1 change: 1 addition & 0 deletions docs/script/examples/digshare-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

> 以下脚本基本为 TypeScript 编写。
- [Webhook 消息](https://github.com/digshare-scipts/webhook-message)
- [雪球用户自选股票更新](https://github.com/digshare-scripts/xueqiu-stock-picks)
- [V2EX 爆贴推送](https://github.com/digshare-scripts/v2ex-trending)
- [GitHub 中文项目趋势](https://github.com/digshare-scripts/github-trending-chinese-projects)
Expand Down
73 changes: 73 additions & 0 deletions docs/script/webhook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Webhook

盯梢支持通过 webhook 调用脚本,支持调用参数(query string 和 body)和设定返回内容。

## 获取 webhook

```sh
npx dss webhook
npx dss webhook --debug # 获取调试环境对应的 webhook
```

## 读取参数

盯梢 webhook 将 query string 和 body 参数合并为 `params` 对象,其中 query string 优先级更高,body 则支持 `application/json``application/x-www-form-urlencoded` 两种格式。

在脚本中,可以通过第二个参数的 `params` 属性获得:

```js
import {script} from '@digshare/script';

export default script((_state, {params: {message}}) => {
return {
message: `这是来自 webhook 的消息:${message}`,
};
});
```

在调试时,可以通过 `--params` 选项传递参数:

```sh
# 使用 URL 编码
npx dss local-run --params message=hello
# 使用 JSON 格式
npx dss local-run --params {\"message\":\"hello\"}

# 其他支持 --params 的命令/参数组合
npx dss run --params message=hello
npx dss deploy --run --params message=hello
npx dss deploy --dry-run --params message=hello
```

## 返回内容

如果调用 webhook 的服务要求特定的返回内容,可以通过 `response` 属性传递:

```js
import {script} from '@digshare/script';

export default script(() => {
return {
response: '这是返回内容',
};
});
```

也可以设置特定的响应头,比如想返回 JSON 格式的内容,建议同时设定 `content-type` 响应头:

```js
import {script} from '@digshare/script';

export default script(() => {
return {
response: {
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
status: 'ok',
}),
},
};
});
```

0 comments on commit 611bdaa

Please sign in to comment.