send-notify-robot
Version:
47 lines (46 loc) • 2.72 kB
TypeScript
import { RobotDingType, DingMessageConfig, MsgType } from "../types/dingtalkTypes";
import { AxiosRequestConfig } from "axios";
export default class RobotDing implements RobotDingType {
readonly webhook: string;
readonly secret: string;
readonly config: AxiosRequestConfig;
/**
* 构造函数用于初始化 Webhook 发送器。
*
* @param webhook Webhook 的URL地址,用于发送请求。
* @param secret 用于验证请求的密钥。
* @param config 可选的 Axios 请求配置,用于自定义请求行为。
*/
constructor(webhook: string, secret: string, config?: AxiosRequestConfig);
/**
* 发送钉钉消息。
* @template T 消息类型,可以是 'text' | 'link' | 'markdown' | 'actionCard' | 'feedCard'
* @param {T} msgType - 消息类型
* @param {DingMessageConfig<T>} content - 根据消息类型配置的内容
* @returns {Promise<any>} 返回一个 Promise 对象,表示消息发送的结果
*
* @example
* // 发送文本消息
* sendDing('text', { content: 'Hello, World!', isAtAll: true, atUserIds: ['user1', 'user2'], atMobiles: ['13800000000'] })
*
* @example
* // 发送链接消息
* sendDing('link', { title: 'Hello, World!', content: 'This is a link message.', messageUrl: 'https://www.example.com', picUrl: 'https://www.example.com/pic.jpg' })
*
* @example
* // 发送 markdown 消息
* sendDing('markdown', { title: 'Hello, World!', content: 'This is a markdown message.', isAtAll: true, atUserIds: ['user1', 'user2'], atMobiles: ['13800000000'] })
*
* @example
* // 发送 actionCard 消息 - 整体跳转
* sendDing('actionCard', { title: 'Hello, World!', content: 'This is a actionCard message.', btnOrientation: 0, singleTitle: 'Single Button', singleURL: 'https://www.example.com'})
* // 发送 actionCard 消息 - 独立跳转
* sendDing('actionCard', { title: 'Hello, World!', content: 'This is a actionCard message.', btnOrientation: 0, btns: [{ title: 'Button 1', actionURL: 'https://www.example.com/button1' }, { title: 'Button 2', actionURL: 'https://www.example.com/button2' }])
*
* @example
* // 发送 feedCard 消息
* sendDing('feedCard', { links: [{ title: 'Hello, World!', messageURL: 'https://www.example.com', picURL: 'https://www.example.com/pic.jpg' }, { title: 'Hello, World!', messageURL: 'https://www.example.com', picURL: 'https://www.example.com/pic.jpg' }] })
*/
sendDing<T extends MsgType>(msgType: T, content: DingMessageConfig<T>): Promise<any>;
protected sendService<T extends MsgType>(msgType: T, postData: DingMessageConfig<T>): Promise<any>;
}