Skip to content

Commit

Permalink
refactor: 重构 钉钉机器人推送 的类型声明
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Nov 9, 2024
1 parent 664ca21 commit 7463ca4
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 144 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const dingtalk = new Dingtalk({
DINGTALK_ACCESS_TOKEN,
DINGTALK_SECRET,
})
dingtalk.send('你好', '你好,我很可爱 - 钉钉机器人')
dingtalk.send('你好', '你好,我很可爱 - 钉钉机器人', { msgtype: 'markdown' })

// 企业微信群机器人。官方文档:https://developer.work.weixin.qq.com/document/path/91770
// 企业微信群机器人的使用需要两人以上加入企业,如果个人使用微信推送建议使用 企业微信应用+微信插件 推送。虽然需要配置的内容更多了,但是无需下载企业微信,网页端即可完成操作。
Expand Down
4 changes: 2 additions & 2 deletions examples/01-example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AxiosResponse } from 'axios'
import colors from '@colors/colors'
import { ServerChanTurbo, ServerChanV3, CustomEmail, Dingtalk, WechatRobot, WechatApp, PushPlus, IGot, Qmsg, XiZhi, PushDeer, Discord, OneBot, Telegram, PushPlusTemplateType, CustomEmailType, OneBotMsgType, WechatRobotMsgType, WechatAppMsgType, PushPlusChannelType } from '../src'
import { ServerChanTurbo, ServerChanV3, CustomEmail, Dingtalk, WechatRobot, WechatApp, PushPlus, IGot, Qmsg, XiZhi, PushDeer, Discord, OneBot, Telegram, PushPlusTemplateType, CustomEmailType, WechatRobotMsgType, WechatAppMsgType, PushPlusChannelType } from '../src'
import { warn } from '../src/utils/helper'
import { SendResponse } from '../src/interfaces/response'

Expand Down Expand Up @@ -64,7 +64,7 @@ export async function batchPushAllInOne(title: string, desp?: string): Promise<P
DINGTALK_ACCESS_TOKEN: env.DINGTALK_ACCESS_TOKEN,
DINGTALK_SECRET: env.DINGTALK_SECRET,
})
pushs.push(dingtalk.send(title, desp))
pushs.push(dingtalk.send(title, desp, { msgtype: 'markdown' }))
info('钉钉机器人 已加入推送队列')
} else {
info('未配置 钉钉机器人,已跳过')
Expand Down
73 changes: 61 additions & 12 deletions src/push/dingtalk.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { AxiosResponse } from 'axios'
import debug from 'debug'
import { MessageTemplateAbs } from './dingtalk/template'
import { Text } from './dingtalk/Text'
import { Markdown } from './dingtalk/Markdown'
import { Markdown } from './dingtalk/markdown'

Check failure on line 3 in src/push/dingtalk.ts

View workflow job for this annotation

GitHub Actions / Test

Cannot find module './dingtalk/markdown' or its corresponding type declarations.
import { Text } from './dingtalk/text'

Check failure on line 4 in src/push/dingtalk.ts

View workflow job for this annotation

GitHub Actions / Test

Cannot find module './dingtalk/text' or its corresponding type declarations.
import { Link } from './dingtalk/link'
import { FeedCard } from './dingtalk/feed-card'
import { ActionCard } from './dingtalk/action-card'
import { Send } from '@/interfaces/send'
import { warn } from '@/utils/helper'
import { ajax } from '@/utils/ajax'
Expand All @@ -24,9 +26,9 @@ export interface DingtalkConfig {
DINGTALK_SECRET?: string
}

export interface DingtalkOption {
export type DingtalkOption = {
[key: string]: any
}
} & Partial<(Text | Markdown | Link | FeedCard | ActionCard)>

export interface DingtalkResponse {
errcode: number
Expand Down Expand Up @@ -79,7 +81,7 @@ export class Dingtalk implements Send {
return signStr
}

private async push(message: MessageTemplateAbs): Promise<AxiosResponse<DingtalkResponse>> {
private async push(data: DingtalkOption): Promise<AxiosResponse<DingtalkResponse>> {
const timestamp = Date.now()
const sign = this.getSign(timestamp)
const result = await ajax({
Expand All @@ -93,7 +95,7 @@ export class Dingtalk implements Send {
sign,
access_token: this.ACCESS_TOKEN,
},
data: message.get(),
data,
})
Debugger('Result is %s, %s。', result.data.errcode, result.data.errmsg)
if (result.data.errcode === 310000) {
Expand All @@ -114,11 +116,58 @@ export class Dingtalk implements Send {
*/
async send(title: string, desp?: string, option?: DingtalkOption): Promise<SendResponse<DingtalkResponse>> {
Debugger('title: "%s", desp: "%s", option: %O', title, desp, option)
if (!desp) {
return this.push(new Text(title))
switch (option.msgtype) {
case 'text':
return this.push({
msgtype: 'text',
text: {
content: `${title}${desp ? `\n${desp}` : ''}`,
},
...option,
})
case 'markdown':
return this.push({
msgtype: 'markdown',
markdown: {
title,
text: `# ${title}${desp ? `\n\n${desp}` : ''}`,
},
...option,
})
case 'link':
return this.push({
msgtype: 'link',
link: {
title,
text: desp || '',
picUrl: option?.link?.picUrl || '',
messageUrl: option.link?.messageUrl || '',
},
...option,
})
case 'actionCard':
return this.push({
msgtype: 'actionCard',
actionCard: {
title,
text: desp || '',
btnOrientation: option?.actionCard?.btnOrientation || '0',
btns: (option?.actionCard as any)?.btns,
singleTitle: (option?.actionCard as any)?.singleTitle,
singleURL: (option?.actionCard as any)?.singleURL,
},
...option,
})
case 'feedCard':
return this.push({
msgtype: 'feedCard',
feedCard: {
links: option?.feedCard?.links || [],
},
...option,
})
default:
throw new Error('msgtype is required!')
}
const markDown = new Markdown()
markDown.setTitle(title).add(`# ${title}`).add(`${desp}`)
return this.push(markDown)
}
}
53 changes: 17 additions & 36 deletions src/push/dingtalk/Markdown.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,20 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { MessageTemplateAbs } from './template'

class Markdown extends MessageTemplateAbs {
items: string[]
title: string
constructor() {
super()
this.msgtype = 'markdown'
this.canAt = true
this.items = []
/**
* 钉钉 markdown 消息
*
* @author CaoMeiYouRen
* @date 2024-11-09
* @export
* @interface Markdown
*/
export interface Markdown {
msgtype: 'markdown'
markdown: {
title: string
text: string
}

setTitle(title: string) {
this.title = title
return this
}

add(text: string | string[]) {
if (Array.isArray(text)) {
this.items.concat(text)
} else {
this.items.push(text)
}

return this
}

get() {
return this.render({
markdown: {
title: this.title,
text: this.items.join('\n'),
},
})
at?: {
atMobiles?: string[]
atUserIds?: string[]
isAtAll?: boolean
}
}

export { Markdown }
40 changes: 16 additions & 24 deletions src/push/dingtalk/Text.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { MessageTemplateAbs } from './template'

class Text extends MessageTemplateAbs {
content: string
constructor(content: string) {
super()
this.msgtype = 'text'
this.canAt = true
this.setContent(content)
/**
* 文本消息
*
* @author CaoMeiYouRen
* @date 2024-11-09
* @export
* @interface Text
*/
export interface Text {
msgtype: 'text'
text: {
content: string
}

setContent(content: string) {
this.content = content
return this
}

get() {
return this.render({
text: {
content: this.content,
},
})
at?: {
atMobiles?: string[]
atUserIds?: string[]
isAtAll?: boolean
}
}

export { Text }
38 changes: 38 additions & 0 deletions src/push/dingtalk/action-card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 整体跳转
export type OverallJump = {
// 单个按钮的标题。设置此项和 singleURL 后,btns无效。
singleTitle: string
// 点击消息跳转的URL
singleURL: string
}

// 独立跳转
export type IndependentJump = {
btns: {
// 按钮的标题
title: string
// 点击按钮触发的URL
actionURL: string
}[]
}

/**
* 动作卡片消息
*
* @author CaoMeiYouRen
* @date 2024-11-09
* @export
* @interface ActionCard
*/
export interface ActionCard {
msgtype: 'actionCard'
actionCard: {
// 首屏会话透出的展示内容
title: string
// markdown 格式的消息内容
text: string
// 0:按钮竖直排列;1:按钮横向排列
btnOrientation?: '0' | '1'
} & (OverallJump | IndependentJump)
}

19 changes: 19 additions & 0 deletions src/push/dingtalk/feed-card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface FeedCardLink {
title: string
messageURL: string
picURL: string
}
/**
* 订阅卡片消息
*
* @author CaoMeiYouRen
* @date 2024-11-09
* @export
* @interface FeedCard
*/
export interface FeedCard {
msgtype: 'feedCard'
feedCard: {
links: FeedCardLink[]
}
}
17 changes: 17 additions & 0 deletions src/push/dingtalk/link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* 链接消息
*
* @author CaoMeiYouRen
* @date 2024-11-09
* @export
* @interface Link
*/
export interface Link {
msgtype: 'link'
link: {
text: string
title: string
picUrl?: string
messageUrl: string
}
}
68 changes: 0 additions & 68 deletions src/push/dingtalk/template.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/push/wechat-app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import debug from 'debug'
import { Send } from '../interfaces/send'
import { error, warn } from '@/utils/helper'
import { warn } from '@/utils/helper'
import { ajax } from '@/utils/ajax'
import { SendResponse } from '@/interfaces/response'

Expand Down

0 comments on commit 7463ca4

Please sign in to comment.