import { Application } from 'egg' ; import { AppPluginBase } from '../../basic/AppPluginBase' ; import { IncomingWebhook , IncomingWebhookSendArguments } from '@slack/webhook/dist/IncomingWebhook' ; import { SlackConfig , DingTalkConfig , MailConfig } from '../../component/im/config' ; import DingTalkRobot from 'dingtalk-robot-sender' ; import { DingTalkMessageType } from '../../basic/IMDataTypes' ; import * as Nodemailer from 'nodemailer' ; export class IMManager extends AppPluginBase < null > { constructor ( config , app ) { super ( config , app ) ; } public async onReady ( ) { } public async onStart ( ) { } public async onClose ( ) { } public async sendToSlack ( message , config ) { if ( ! config || ! config . webhook ) return ; const webhook = new IncomingWebhook ( config . webhook ) ; await webhook . send ( message ) . catch ( e => { this . logger . error ( e ) ; } ) ; } public async sendToDingTalk ( message , config ) { if ( ! config || ! config . webhook ) return ; const robot = new DingTalkRobot ( { webhook : config . webhook } ) ; await robot . send ( message ) . catch ( e => { this . logger . error ( `DingTalk send error: ${ e }` ) ; } ) ; } public async sendToMail ( message , config ) { try { const client = Nodemailer . createTransport ( config . connectOptions ) ; const ok = await client . verify ( ) ; if ( ! ok ) { this . logger . warn ( 'send mail verify failed' ) ; return ; } const tryTimes = 5 ; for ( let i = 0 ; i < tryTimes ; i ++ ) { try { await client . sendMail ( message ) ; return ; } catch ( e ) { if ( e . code !== 'ETIMEDOUT' ) { this . logger . error ( 'Error when send mail ' , e ) ; return ; } } } } catch ( error ) { this . logger . error ( error ) ; } } }