From 80c5dad3d320a99e8966f46fbf5348b0e2a5c347 Mon Sep 17 00:00:00 2001 From: Huan LI Date: Mon, 15 May 2017 17:44:54 +0800 Subject: [PATCH] fix(say): add return boolean to identify success/failure --- src/config.ts | 2 +- src/contact.ts | 5 ++-- src/puppet-web/bridge.ts | 4 ++-- src/puppet-web/puppet-web.ts | 24 ++++++++++--------- src/puppet-web/wechaty-bro.js | 44 ++++++++++++++++++++++------------- src/puppet.ts | 4 ++-- src/room.ts | 10 ++++---- src/wechaty.ts | 18 +++++++------- 8 files changed, 61 insertions(+), 50 deletions(-) diff --git a/src/config.ts b/src/config.ts index 33316d444..462ad16b6 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 54f68cffc..85c065f5c 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 88bcad964..53b448465 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 1d0e051a1..801bcee42 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 5b73fe85f..c9684bc2c 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 e841f5a21..270be5730 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 3b276eb74..3d3e5035f 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 a69bbc9b4..d93545bd9 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) } /** -- GitLab