-
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
197 additions
and
134 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { DingBotAdapter as BaseDingBotAdapter, Session } from '../src'; | ||
|
||
describe('bot-commander', () => { | ||
it('should work', () => { | ||
const example = { | ||
chatbotCorpId: 'corp1', | ||
chatbotUserId: 'user1', | ||
conversationId: 'conv1', | ||
conversationType: 'convType1', | ||
createAt: 1, | ||
isAdmin: true, | ||
msgId: 'msg1', | ||
msgtype: 'text' as const, | ||
robotCode: 'code1', | ||
senderCorpId: 'corp1', | ||
senderId: 'sender1', | ||
senderNick: 'nick1', | ||
senderStaffId: 'staff1', | ||
sessionWebhook: 'webhook1', | ||
sessionWebhookExpiredTime: 1, | ||
text: { | ||
content: '/hello', | ||
}, | ||
}; | ||
|
||
const session = new Session(example); | ||
const adapter = new BaseDingBotAdapter('bot1'); | ||
|
||
adapter.cc.on('hello', async (ctx) => { | ||
const { session } = ctx; | ||
console.log(`===== ~ adapter.cc.on ~ ctx:`, ctx); | ||
await session.replyText('world'); | ||
}); | ||
|
||
adapter.handle(session); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,57 @@ | ||
import { CommandCenter, ICommandCenterOptions } from '@opensumi/bot-commander'; | ||
import * as types from '../types'; | ||
import { send } from '../utils'; | ||
import { IBotAdapter } from './base-adapter'; | ||
import { Session } from './session'; | ||
|
||
function prepare(s: string) { | ||
return s.toString().trim(); | ||
} | ||
|
||
export class DingBotAdapter<CommandCenterContext extends Record<string, any>> | ||
implements IBotAdapter | ||
{ | ||
export class DingBotAdapter< | ||
CommandCenterContext extends { | ||
session: Session; | ||
}, | ||
> { | ||
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; | ||
public async constructContext( | ||
session: Session, | ||
): Promise<CommandCenterContext> { | ||
return { | ||
session, | ||
} as CommandCenterContext; | ||
} | ||
|
||
async handle(options?: { | ||
timeout?: number | null; | ||
}) { | ||
const msg = this.msg; | ||
async handle( | ||
session: Session, | ||
options?: { | ||
timeout?: number | null; | ||
}, | ||
) { | ||
const msg = session.msg; | ||
console.log( | ||
`${this.id} receive dingtalk msg: `, | ||
JSON.stringify(msg, null, 2), | ||
); | ||
|
||
// 其实目前钉钉机器人也就支持这一种消息类型 | ||
if (msg.msgtype === 'text') { | ||
const text = prepare(msg.text.content); | ||
const text = session.text; | ||
console.log(`DingBot ~ handle ~ text`, text); | ||
|
||
await this.cc.tryHandle(text, await this.getContext(), options); | ||
try { | ||
await this.cc.tryHandle( | ||
text, | ||
await this.constructContext(session), | ||
options, | ||
); | ||
} catch (err) { | ||
await session.replyText( | ||
`处理消息出错: ${(err as Error).message} ${(err as Error).stack}`, | ||
); | ||
throw err; | ||
} | ||
} | ||
} | ||
|
||
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 |
---|---|---|
@@ -1,14 +1,23 @@ | ||
import { IBotAdapter } from './base-adapter'; | ||
import { Message, SendMessage, compose, text as textFn } from '../types'; | ||
import { send } from '../utils'; | ||
|
||
function prepare(s: string) { | ||
return s.toString().trim(); | ||
} | ||
|
||
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; | ||
}); | ||
constructor(public msg: Message) {} | ||
|
||
async replyText(text: string, contentExtra: Record<string, any> = {}) { | ||
await this.reply(compose(textFn(text), contentExtra)); | ||
} | ||
|
||
async reply(content: SendMessage) { | ||
console.log(`DingBot ~ reply:`, JSON.stringify(content)); | ||
await send(content, this.msg.sessionWebhook); | ||
} | ||
|
||
get text() { | ||
return prepare(this.msg.text.content); | ||
} | ||
} |
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
Oops, something went wrong.