Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dingtalk 支持 markdown 消息配置 #22

Merged
merged 6 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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