-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8a5568
commit 071fb8b
Showing
18 changed files
with
430 additions
and
91 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
import { ServerChan } from './push/server-chan' | ||
import { ServerChanTurbo } from './push/server-chan-turbo' | ||
|
||
export { | ||
ServerChan, | ||
ServerChanTurbo, | ||
} | ||
export * from './push/dingtalk' | ||
export * from './push/server-chan' | ||
export * from './push/server-chan-turbo' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* 要求所有 push 方法都至少实现了 send 接口 | ||
* | ||
* @author CaoMeiYouRen | ||
* @date 2021-02-27 | ||
* @export | ||
* @interface Send | ||
*/ | ||
interface Send { | ||
send(...args: any[]): Promise<any> | ||
} | ||
export { Send } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Send } from '../interfaces/send' | ||
import { ajax } from '@/utils/ajax' | ||
import { AxiosResponse } from 'axios' | ||
import debug from 'debug' | ||
import colors from 'colors' | ||
import CryptoJS from 'crypto-js' | ||
import { Message } from './dingtalk/index' | ||
|
||
const Debugger = debug('push:dingtalk') | ||
|
||
class RobotOption { | ||
accessToken?: string | ||
secret?: string | ||
} | ||
|
||
export class Dingtalk implements Send { | ||
private accessToken?: string | ||
private secret?: string | ||
private webhook: string = 'https://oapi.dingtalk.com/robot/send' | ||
constructor(option: RobotOption = {}) { | ||
Object.assign(this, option) | ||
if (!this.accessToken) { | ||
throw new Error('accessToken is required!') | ||
} | ||
if (!this.secret) { | ||
console.warn(colors.yellow('Secret is undefined')) | ||
} | ||
} | ||
|
||
private getSign(timeStamp: number) { | ||
let signStr = '' | ||
if (this.secret) { | ||
signStr = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(`${timeStamp}\n${this.secret}`, this.secret)) | ||
Debugger('Sign string is %s, result is %s', `${timeStamp}\n${this.secret}`, signStr) | ||
} | ||
return signStr | ||
} | ||
|
||
public async send(message: Message): Promise<AxiosResponse<any>> { | ||
const timestamp = Date.now() | ||
const sign = this.getSign(timestamp) | ||
const result = await ajax({ | ||
url: this.webhook, | ||
query: { | ||
timestamp, | ||
sign, | ||
access_token: this.accessToken, | ||
}, | ||
data: message.get(), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}) | ||
Debugger('Result is %s, %s。', result.data.errcode, result.data.errmsg) | ||
if (result.data.errcode === 310000) { | ||
console.error('Send Failed:', result.data) | ||
Debugger('Please check safe config : %O', result.data) | ||
} | ||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
import { MessageTemplateAbs } from './template' | ||
|
||
class ActionCard extends MessageTemplateAbs { | ||
title: string | ||
text: string | ||
hideAvatar: number | ||
btnOrientation?: number | ||
singleTitle: string | ||
singleURL: string | ||
btns: { title: string, actionURL: string }[] | ||
constructor() { | ||
super() | ||
this.msgtype = 'actionCard' | ||
|
||
this.title = '' | ||
this.text = '' | ||
// 0-正常发消息者头像,1-隐藏发消息者头像 | ||
this.hideAvatar = 0 | ||
// 0-按钮竖直排列,1-按钮横向排列 | ||
this.btnOrientation = 0 | ||
|
||
// 单个按钮的方案。(设置此项和singleURL后btns无效) | ||
this.singleTitle = '' | ||
this.singleURL = '' | ||
this.btns = [] | ||
} | ||
|
||
setBtns(btns: ConcatArray<{ title: string, actionURL: string }>) { | ||
this.btns = this.btns.concat(btns) | ||
return this | ||
} | ||
|
||
setSingleTitle(title: string) { | ||
this.singleTitle = title | ||
return this | ||
} | ||
|
||
setSingleURL(url: string) { | ||
this.singleURL = url | ||
return this | ||
} | ||
|
||
setTitle(title: string) { | ||
this.title = title | ||
return this | ||
} | ||
|
||
setText(content: string) { | ||
this.text = content | ||
return this | ||
} | ||
|
||
setBtnOrientation(flag: number) { | ||
this.btnOrientation = flag | ||
return this | ||
} | ||
|
||
setHideAvatar(flag: number) { | ||
this.hideAvatar = flag | ||
return this | ||
} | ||
|
||
get() { | ||
return this.render({ | ||
actionCard: { | ||
title: this.title, | ||
text: this.text, | ||
hideAvatar: this.hideAvatar, | ||
btnOrientation: this.btnOrientation, | ||
btns: this.btns, | ||
singleTitle: this.singleTitle, | ||
singleURL: this.singleURL, | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
export { ActionCard } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
import { MessageTemplateAbs } from './template' | ||
import { Link } from './Link' | ||
|
||
class FeedCard extends MessageTemplateAbs { | ||
links: Link[] | ||
constructor(links: Link[]) { | ||
super() | ||
this.msgtype = 'feedCard' | ||
|
||
this.links = links || [] | ||
} | ||
|
||
addLinks(links: Link[]) { | ||
this.links = this.links.concat(links) | ||
return this | ||
} | ||
|
||
get() { | ||
return this.render({ | ||
feedCard: { | ||
links: this.links, | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
export { FeedCard } |
Oops, something went wrong.