Skip to content

Commit

Permalink
refactor: 重构 钉钉机器人 推送,迁移到 新版接口;优化 日志输出
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Nov 8, 2024
1 parent bd912f1 commit 82bfab4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
30 changes: 24 additions & 6 deletions src/push/dingtalk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,24 @@ import { SendResponse } from '@/interfaces/response'

const Debugger = debug('push:dingtalk')

export interface DingtalkConfig {
/**
* 钉钉机器人 access_token。官方文档:https://developers.dingtalk.com/document/app/custom-robot-access
*/
DINGTALK_ACCESS_TOKEN: string
/**
* 加签安全秘钥(HmacSHA256)
*/
DINGTALK_SECRET?: string
}

export interface DingtalkResponse {
errcode: number
errmsg: string
}

/**
* 钉钉机器人推送
* 在 [dingtalk-robot-sdk](https://github.com/ineo6/dingtalk-robot-sdk) 的基础上重构了一下,用法几乎完全一致。
* 参考文档 [钉钉开放平台 - 自定义机器人接入](https://developers.dingtalk.com/document/app/custom-robot-access)
*
Expand All @@ -31,10 +48,11 @@ export class Dingtalk implements Send {
private SECRET?: string
private webhook: string = 'https://oapi.dingtalk.com/robot/send'

constructor(ACCESS_TOKEN: string, SECRET?: string) {
this.ACCESS_TOKEN = ACCESS_TOKEN
this.SECRET = SECRET
Debugger('ACCESS_TOKEN: %s , SECRET: %s', ACCESS_TOKEN, SECRET)
constructor(config: DingtalkConfig) {
const { DINGTALK_ACCESS_TOKEN, DINGTALK_SECRET } = config
this.ACCESS_TOKEN = DINGTALK_ACCESS_TOKEN
this.SECRET = DINGTALK_SECRET
Debugger('DINGTALK_ACCESS_TOKEN: %s , DINGTALK_SECRET: %s', this.ACCESS_TOKEN, this.SECRET)
if (!this.ACCESS_TOKEN) {
throw new Error('ACCESS_TOKEN 是必须的!')
}
Expand All @@ -52,7 +70,7 @@ export class Dingtalk implements Send {
return signStr
}

private async push(message: MessageTemplateAbs): Promise<AxiosResponse> {
private async push(message: MessageTemplateAbs): Promise<AxiosResponse<DingtalkResponse>> {
const timestamp = Date.now()
const sign = this.getSign(timestamp)
const result = await ajax({
Expand Down Expand Up @@ -85,7 +103,7 @@ export class Dingtalk implements Send {
* @param [desp] 消息的内容,支持 Markdown
* @returns
*/
async send(title: string, desp?: string): Promise<SendResponse> {
async send(title: string, desp?: string): Promise<SendResponse<DingtalkResponse>> {
Debugger('title: "%s", desp: "%s"', title, desp)
if (!desp) {
return this.push(new Text(title))
Expand Down
9 changes: 2 additions & 7 deletions src/utils/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios, { AxiosResponse, Method, AxiosRequestHeaders } from 'axios'
import debug from 'debug'
import { HttpsProxyAgent } from 'https-proxy-agent'
import { SocksProxyAgent } from 'socks-proxy-agent'
import { isHttpURL, isSocksUrl } from './helper'
import { isHttpURL, isSocksUrl, logger } from './helper'

const Debugger = debug('push:ajax')

Expand Down Expand Up @@ -69,14 +69,9 @@ export async function ajax<T = any>(config: AjaxConfig): Promise<AxiosResponse<T
return response
} catch (error) {
if (error?.response) {
console.error(error.response)
logger.error(error.response)
return error.response
}
if (error.toJSON) {
console.error(error.toJSON())
} else {
console.error(error)
}
throw error
}
}
5 changes: 5 additions & 0 deletions src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export function error(text: any): void {
console.error(text)
}

export const logger = {
warn,
error,
}

/**
* 检测是否为 http/https 开头的 url
* @param url
Expand Down

0 comments on commit 82bfab4

Please sign in to comment.