diff --git a/README.md b/README.md index 97481ac..e3eba40 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,12 @@ interface NoticeOptions { url?: string; verifyPay?: boolean; }; + dingtalk?: { + /** + * 消息类型,目前支持 text、markdown。不设置,默认为 text。 + */ + msgtype?: string; + }; } ``` diff --git a/src/index.ts b/src/index.ts index 3a1700c..38b287d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,6 +35,12 @@ export interface NoticeOptions { url?: string; verifyPay?: boolean; }; + dingtalk?: { + /** + * 消息类型,目前支持 text、markdown。不设置,默认为 text。 + */ + msgtype?: string; + }; } export interface CommonOptions { token: string; @@ -201,14 +207,22 @@ async function noticeDingTalk(options: CommonOptions) { } else { url += options.token; } - let content = getTxt(options.content); - if (options.title) { - content = `${options.title}\n${content}`; + + const msgtype = options.options?.dingtalk?.msgtype || 'text'; + const content = msgtype === 'text' + ? (options.title ? `${options.title}\n` : '') + getTxt(options.content) + : options.content; + + const msgBody = { + msgtype, + }; + + if (msgtype === 'text') { + msgBody[msgtype] = { content }; + } else if (msgtype === 'markdown') { + msgBody[msgtype] = { title: options.title || getTitle(options.content), text: content }; } - const response = await axios.post(url, { - msgtype: 'text', - text: { content }, - }); + const response = await axios.post(url, msgBody); return response.data; }