Skip to content

Commit

Permalink
dingtalk 支持 markdown 消息配置 (#22)
Browse files Browse the repository at this point in the history
* feat: 支持 wxpusher

* fix: wxpusher

* feat: dingtalk 支持 markdown 消息配置

* docs: readme
  • Loading branch information
funnyzak authored May 25, 2023
1 parent 6e43d41 commit 19a244c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ interface NoticeOptions {
url?: string;
verifyPay?: boolean;
};
dingtalk?: {
/**
* 消息类型,目前支持 text、markdown。不设置,默认为 text。
*/
msgtype?: string;
};
}
```

Expand Down
28 changes: 21 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export interface NoticeOptions {
url?: string;
verifyPay?: boolean;
};
dingtalk?: {
/**
* 消息类型,目前支持 text、markdown。不设置,默认为 text。
*/
msgtype?: string;
};
}
export interface CommonOptions {
token: string;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 19a244c

Please sign in to comment.