diff --git a/src/config.ts b/src/config.ts index 33316d444904bc989ed471f3efec938b8eb77f37..462ad16b64eeffab81ce5c2805d6f141247d514e 100644 --- a/src/config.ts +++ b/src/config.ts @@ -188,7 +188,7 @@ export interface RecommendInfo { } export interface Sayable { - say(content: string, replyTo?: any|any[]): Promise + say(content: string, replyTo?: any|any[]): Promise } export interface Sleepable { diff --git a/src/contact.ts b/src/contact.ts index 54f68cffc0d7fa88736328cb37eeceef5aa2071d..85c065f5cc32100c169cca5ee874d2b234658970 100644 --- a/src/contact.ts +++ b/src/contact.ts @@ -703,7 +703,7 @@ export class Contact implements Sayable { public async say(text: string) public async say(mediaMessage: MediaMessage) - public async say(textOrMedia: string | MediaMessage): Promise { + public async say(textOrMedia: string | MediaMessage): Promise { const content = textOrMedia instanceof MediaMessage ? textOrMedia.filename() : textOrMedia log.verbose('Contact', 'say(%s)', content) @@ -726,8 +726,7 @@ export class Contact implements Sayable { m.to(this) log.silly('Contact', 'say() from: %s to: %s content: %s', user.name(), this.name(), content) - await wechaty.send(m) - return + return await wechaty.send(m) } } diff --git a/src/puppet-web/bridge.ts b/src/puppet-web/bridge.ts index 88bcad964a5ea5a2ca108c638db8c54c3e1e0bfa..53b448465c3cc73c28a2f785e1bb2f6148570c5c 100644 --- a/src/puppet-web/bridge.ts +++ b/src/puppet-web/bridge.ts @@ -248,7 +248,7 @@ export class Bridge { }) } - public send(toUserName: string, content: string): Promise { + public send(toUserName: string, content: string): Promise { if (!toUserName) { throw new Error('UserName not found') } @@ -387,7 +387,7 @@ export class Bridge { } } - public sendMedia(toUserName: string, mediaId: string, type: number): Promise { + public sendMedia(toUserName: string, mediaId: string, type: number): Promise { if (!toUserName) { throw new Error('UserName not found') } diff --git a/src/puppet-web/puppet-web.ts b/src/puppet-web/puppet-web.ts index 1d0e051a171d81ec322e4e0b7927c26b4b61204f..801bcee42aa1ce3c93976ce6b4625563f862a69e 100644 --- a/src/puppet-web/puppet-web.ts +++ b/src/puppet-web/puppet-web.ts @@ -425,7 +425,7 @@ export class PuppetWeb extends Puppet { return mediaId as string } - public async sendMedia(message: MediaMessage): Promise { + public async sendMedia(message: MediaMessage): Promise { const to = message.to() const room = message.room() @@ -448,16 +448,17 @@ export class PuppetWeb extends Puppet { mediaId, ) + let ret = false try { - await this.bridge.sendMedia(destinationId, mediaId, msgType) + ret = await this.bridge.sendMedia(destinationId, mediaId, msgType) } catch (e) { log.error('PuppetWeb', 'send() exception: %s', e.message) - throw e + return false } - return + return ret } - public async send(message: Message | MediaMessage): Promise { + public async send(message: Message | MediaMessage): Promise { const to = message.to() const room = message.room() @@ -472,8 +473,10 @@ export class PuppetWeb extends Puppet { destinationId = to.id } + let ret = false + if (message instanceof MediaMessage) { - await this.sendMedia(message) + ret = await this.sendMedia(message) } else { const content = message.content() @@ -483,20 +486,20 @@ export class PuppetWeb extends Puppet { ) try { - await this.bridge.send(destinationId, content) + ret = await this.bridge.send(destinationId, content) } catch (e) { log.error('PuppetWeb', 'send() exception: %s', e.message) throw e } } - return + return ret } /** * Bot say... * send to `filehelper` for notice / log */ - public async say(content: string): Promise { + public async say(content: string): Promise { if (!this.logined()) { throw new Error('can not say before login') } @@ -505,8 +508,7 @@ export class PuppetWeb extends Puppet { m.to('filehelper') m.content(content) - await this.send(m) - return + return await this.send(m) } /** diff --git a/src/puppet-web/wechaty-bro.js b/src/puppet-web/wechaty-bro.js index 5b73fe85f4c89dbefe810a34190a653ec2b044de..c9684bc2cd638788cb94b24f65af25eb96683b98 100644 --- a/src/puppet-web/wechaty-bro.js +++ b/src/puppet-web/wechaty-bro.js @@ -444,7 +444,7 @@ function getBaseRequest() { var accountFactory = WechatyBro.glue.accountFactory var BaseRequest = accountFactory.getBaseRequest() - + return JSON.stringify(BaseRequest) } @@ -463,17 +463,23 @@ var confFactory = WechatyBro.glue.confFactory if (!chatFactory || !confFactory) { - log('send() chatFactory or confFactory not exist.') + log('sendMedia() chatFactory or confFactory not exist.') return false } - var m = chatFactory.createMessage({ - ToUserName: ToUserName - , MediaId: MediaId - , MsgType: Type - }) - chatFactory.appendMessage(m) - return chatFactory.sendMessage(m) + try { + var m = chatFactory.createMessage({ + ToUserName: ToUserName + , MediaId: MediaId + , MsgType: Type + }) + chatFactory.appendMessage(m) + chatFactory.sendMessage(m) + } catch (e) { + log('sendMedia() exception: ' + e.message) + return false + } + return true } function send(ToUserName, Content) { @@ -484,13 +490,19 @@ log('send() chatFactory or confFactory not exist.') return false } - var m = chatFactory.createMessage({ - ToUserName: ToUserName - , Content: Content - , MsgType: confFactory.MSGTYPE_TEXT - }) - chatFactory.appendMessage(m) - return chatFactory.sendMessage(m) + try { + var m = chatFactory.createMessage({ + ToUserName: ToUserName + , Content: Content + , MsgType: confFactory.MSGTYPE_TEXT + }) + chatFactory.appendMessage(m) + chatFactory.sendMessage(m) + } catch (e) { + log('send() exception: ' + e.message) + return false + } + return true } function getContact(id) { var contactFactory = WechatyBro.glue.contactFactory diff --git a/src/puppet.ts b/src/puppet.ts index e841f5a219fcc1679a220dfea542627e01c54963..270be5730c1d121899d0de2e3fd44928ce466b35 100644 --- a/src/puppet.ts +++ b/src/puppet.ts @@ -35,8 +35,8 @@ export abstract class Puppet extends EventEmitter implements Sayable { public abstract self(): Contact - public abstract send(message: Message | MediaMessage): Promise - public abstract say(content: string): Promise + public abstract send(message: Message | MediaMessage): Promise + public abstract say(content: string): Promise public abstract reset(reason?: string): void public abstract logout(): Promise diff --git a/src/room.ts b/src/room.ts index 3b276eb74ad733a290abb77957603e2c4048d1e6..3d3e5035f9adffbc673ad9c8b9971965351823d4 100644 --- a/src/room.ts +++ b/src/room.ts @@ -163,12 +163,12 @@ export class Room extends EventEmitter implements Sayable { return this } - public say(mediaMessage: MediaMessage): Promise - public say(content: string): Promise - public say(content: string, replyTo: Contact): Promise - public say(content: string, replyTo: Contact[]): Promise + public say(mediaMessage: MediaMessage) + public say(content: string) + public say(content: string, replyTo: Contact) + public say(content: string, replyTo: Contact[]) - public say(textOrMedia: string | MediaMessage, replyTo?: Contact|Contact[]): Promise { + public say(textOrMedia: string | MediaMessage, replyTo?: Contact|Contact[]): Promise { const content = textOrMedia instanceof MediaMessage ? textOrMedia.filename() : textOrMedia log.verbose('Room', 'say(%s, %s)', content, diff --git a/src/wechaty.ts b/src/wechaty.ts index a69bbc9b45c5eafa23e4ee5e14ccb3b183558c1a..d93545bd9c673a8c07f58ba284a402b37dff7612 100644 --- a/src/wechaty.ts +++ b/src/wechaty.ts @@ -424,29 +424,27 @@ export class Wechaty extends EventEmitter implements Sayable { /** * @todo document me */ - public async send(message: Message | MediaMessage): Promise { + public async send(message: Message | MediaMessage): Promise { if (!this.puppet) { throw new Error('no puppet') } - await this.puppet.send(message) - .catch(e => { - log.error('Wechaty', 'send() exception: %s', e.message) - throw e - }) - return + return await this.puppet.send(message) + .catch(e => { + log.error('Wechaty', 'send() exception: %s', e.message) + throw e + }) } /** * @todo document me */ - public async say(content: string): Promise { + public async say(content: string): Promise { log.verbose('Wechaty', 'say(%s)', content) if (!this.puppet) { throw new Error('no puppet') } - this.puppet.say(content) - return + return await this.puppet.say(content) } /**