-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
13 changed files
with
120 additions
and
102 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Message, SendMessage } from '../types'; | ||
|
||
export interface IBotAdapter { | ||
id: string; | ||
msg: Message; | ||
|
||
replyText(text: string, contentExtra?: Record<string, any>): Promise<void>; | ||
reply(content: SendMessage): Promise<void>; | ||
|
||
handle(): Promise<void>; | ||
} |
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,53 @@ | ||
import { CommandCenter, ICommandCenterOptions } from '@opensumi/bot-commander'; | ||
import * as types from '../types'; | ||
import { send } from '../utils'; | ||
import { IBotAdapter } from './base-adapter'; | ||
|
||
function prepare(s: string) { | ||
return s.toString().trim(); | ||
} | ||
|
||
export class DingBotAdapter<CommandCenterContext extends Record<string, any>> | ||
implements IBotAdapter | ||
{ | ||
public cc: CommandCenter<CommandCenterContext>; | ||
|
||
constructor( | ||
public id: string, | ||
public msg: types.Message, | ||
public commandOptions?: ICommandCenterOptions<CommandCenterContext>, | ||
) { | ||
this.cc = new CommandCenter(commandOptions); | ||
} | ||
|
||
public async getContext(): Promise<CommandCenterContext> { | ||
return {} as CommandCenterContext; | ||
} | ||
|
||
async handle(options?: { | ||
timeout?: number | null; | ||
}) { | ||
const msg = this.msg; | ||
console.log( | ||
`${this.id} receive dingtalk msg: `, | ||
JSON.stringify(msg, null, 2), | ||
); | ||
|
||
// 其实目前钉钉机器人也就支持这一种消息类型 | ||
if (msg.msgtype === 'text') { | ||
const text = prepare(msg.text.content); | ||
console.log(`DingBot ~ handle ~ text`, text); | ||
|
||
await this.cc.tryHandle(text, await this.getContext(), options); | ||
} | ||
} | ||
|
||
async replyText(text: string, contentExtra: Record<string, any> = {}) { | ||
await this.reply(types.compose(types.text(text), contentExtra)); | ||
} | ||
|
||
async reply(content: types.SendMessage) { | ||
console.log(`DingBot ~ reply:`, JSON.stringify(content)); | ||
await send(content, this.msg.sessionWebhook); | ||
} | ||
} |
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,14 @@ | ||
import { IBotAdapter } from './base-adapter'; | ||
|
||
export class Session { | ||
constructor(public impl: IBotAdapter) {} | ||
|
||
async run() { | ||
await this.impl.handle().catch(async (err: Error) => { | ||
await this.impl.replyText( | ||
`处理消息出错: ${(err as Error).message} ${(err as Error).stack}`, | ||
); | ||
throw err; | ||
}); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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
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,14 +1,7 @@ | ||
import type { DingKVManager, DingUserKVManager } from '@/kv/ding'; | ||
import { Message, SendMessage } from '@opensumi/dingtalk-bot/lib/types'; | ||
import { IBotAdapter as _IBotAdapter } from '@opensumi/dingtalk-bot/lib/bot/adapter'; | ||
|
||
export interface IBotAdapter { | ||
id: string; | ||
export interface IBotAdapter extends _IBotAdapter { | ||
kvManager: DingKVManager; | ||
userInfoKVManager: DingUserKVManager; | ||
msg: Message; | ||
|
||
replyText(text: string, contentExtra?: Record<string, any>): Promise<void>; | ||
reply(content: SendMessage): Promise<void>; | ||
|
||
handle(): Promise<void>; | ||
} |
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