Activepieces制作飞书机器人插件
2024 年 5 月 25 日 星期六(已编辑)
这篇文章上次修改于 2024 年 5 月 25 日 星期六,可能部分内容已经不适用,如有疑问可询问作者。
您最友好的开源一体化自动化工具✨ 工作流自动化工具100+集成/企业自动化工具/Zapier替代品
技术栈
绝大多数插件为国外软件,所以需要自定义插件。
Hiflow 为国内产品,两者操作没有太多区别.
写一个飞书机器人,参考 Hiflow 流程
选取组件
选取 action
这里有两个 action, 一个是发送文本,一个是发送富文本
然后下一步是配置登录(配置账号)
最后一步是拿到参数然后执行下一步
需要上传插件包到 npm 上,然后 输入包名与版本号
添加成功后可以看到
可以参考官方的如何实现
初始化包
我这里选择为 unbuild 打包
主要包为
开发依赖包
dev 依赖包
目录结构
入口文件
src/index.ts
发送纯文本
npm 发包
pnpm init
pnpm i unbuild
"dependencies": {
"@activepieces/pieces-common": "^0.2.12",
"@activepieces/pieces-framework": "^0.7.30",
"@activepieces/shared": "^0.10.105",
"crypto": "^1.0.1"
},
"devDependencies": {
"@antfu/eslint-config": "^2.18.1",
"@types/node": "^20.12.12",
"eslint": "^9.3.0",
"typescript": "5.3.3",
"unbuild": "^2.0.0"
}
./src
├── index.ts
└── lib
├── actions
│ ├── send-richtext.ts
│ └── send-text.ts
├── common
│ └── gen-sign.ts
└── constants
└── index.ts
import {
PieceAuth,
Property,
createPiece,
} from '@activepieces/pieces-framework'
import { PieceCategory } from '@activepieces/shared'
import { sendTextAction } from './lib/actions/send-text'
import { sendRichTextAction } from './lib/actions/send-richtext'
export const feishuBotAuth = PieceAuth.CustomAuth({
required: true,
description: `
飞书机器人
选择飞书群-群设置-群机器人,选择自定义添加,【安全设置】勾选"签名校验",请不要勾选"自定义关键词"和"IP白名单",否则会导致添加账户失败,然后点击复制密钥后粘贴到这里
`,
props: {
secret: PieceAuth.SecretText({
displayName: '加签密钥',
description: '',
required: true,
}),
webhook_url: Property.ShortText({
displayName: 'Webhook地址',
description: '机器人添加成功后可获取Webhook地址',
required: true,
}),
},
})
export const feishu_bot = createPiece({
displayName: '飞书机器人',
description: '飞书机器人',
minimumSupportedRelease: '0.5.0',
actions: [
sendTextAction,
sendRichTextAction,
],
logoUrl: 'https://cdn.taoya.art/use/202405241547362.png',
authors: ['taoya7'],
auth: feishuBotAuth,
triggers: [],
categories: [PieceCategory.COMMUNICATION],
})
import { Property, createAction } from '@activepieces/pieces-framework'
import {
HttpMethod,
httpClient,
} from '@activepieces/pieces-common'
import { feishuBotAuth } from '../../index'
import { genSign } from '@/lib/common/gen-sign'
/**
* 发送文本消息
*/
export const sendTextAction = createAction({
name: 'feishu_robot_send_text',
// 显示名称
displayName: '发送文本消息',
// 描述
description: '发送纯文本消息到飞书群',
auth: feishuBotAuth,
requireAuth: true,
// 配置参数
props: {
text: Property.ShortText({
displayName: '文本内容',
description: '流程启动后,填入的文本内容,将被飞书群机器人发出,请检查核对',
required: true,
}),
},
async run({ auth, propsValue }) {
return await httpClient.sendRequest({
method: HttpMethod.POST,
url: auth.webhook_url,
body: {
...genSign(auth.secret),
msg_type: 'text',
content: {
text: propsValue.text,
},
},
headers: {
'Content-Type': 'application/json',
},
})
},
})
npm run build
npm publish --access public