@zhengxs/dingtalk-sdk-js
v0.0.1-beta.1
Published
[![Typescript](https://img.shields.io/badge/lang-typescript-informational?style=flat-square)](https://www.typescriptlang.org)[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/pretti
Downloads
1
Readme
@zhengxs/dingtalk-sdk-js
当前状态:Alpha
非官方 JS-SDK,请勿在生产中使用。
文档
- 获取访问凭证
- 机器人开发
- 互动卡片
安装
# With NPM
$ npm i -S @zhengxs/dingtalk-sdk-js
# With Yarn
$ yarn add @zhengxs/dingtalk-sdk-js
# With PNPM
$ pnpm add @zhengxs/dingtalk-sdk-js
使用
企业机器人开发
import { DingtalkClient, StreamClient } from '@zhengxs/dingtalk-sdk-js';
const client = new DingtalkClient({
clientId: 'My Client ID',
clientSecret: 'My Client Secret',
});
// 订阅消息
const connection = new StreamClient(client);
// 监听机器人消息
connection.onImMessage(function (message) {
console.log('接收到的消息', message.data);
// 回复消息给当前发送者
message.reply('hello,world');
});
connection.connect();
链接
自定义机器人开发
如果传递 secret
参数,会自动对 URL 进行签名
import { CustomizeWebhook } from '@zhengxs/dingtalk-sdk-js';
const webhook = new CustomizeWebhook({
token: 'webhook token',
secret: 'webhook secret',
});
webhook.say('hello,world');
详见 自定义机器人接入
获取企业内部应用的身份凭证
如果凭证过期,内部会自动获取最新的可用凭证,无需外部处理。
import { SessionCredentials } from '@zhengxs/dingtalk-sdk-js';
const session = new SessionCredentials({
clientId: 'My Client ID',
clientSecret: 'My Client Secret',
});
async function main() {
const token = await session.getAccessToken();
console.log(token);
}
main();
发送互动卡片
import { DingtalkClient, StreamClient } from '@zhengxs/dingtalk-sdk-js';
const client = new DingtalkClient({
clientId: process.env.DINGTALK_CLIENT_ID,
clientSecret: process.env.DINGTALK_CLIENT_SECRET,
});
const connection = new StreamClient(client);
connection.onImMessage(async message => {
// 发送互动卡片
const data = await client.card.instances.send({
outTrackId: 'Your out trackId',
cardTemplateId: 'Your card templateId',
openConversationId: message.conversationId,
receiverUserIdList: [message.talker.staffId],
conversationType: message.conversationType === '2' ? 1 : 0, // 互动卡片和消息卡片的聊天类型值不一致
chatBotId: message.robot.id, // 根据做了判断,如果是私聊,会直接删除机器人ID
robotCode: message.robot.code, // 根据做了判断,如果是私聊,会直接删除机器人编码
cardData: {
cardParamMap: {},
},
// 其他参数...
});
console.log(data);
});
connection.connect();
参考
License
MIT