提交 80c5dad3 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

fix(say): add return boolean to identify success/failure

上级 dbe0c174
......@@ -188,7 +188,7 @@ export interface RecommendInfo {
}
export interface Sayable {
say(content: string, replyTo?: any|any[]): Promise<void>
say(content: string, replyTo?: any|any[]): Promise<boolean>
}
export interface Sleepable {
......
......@@ -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<void> {
public async say(textOrMedia: string | MediaMessage): Promise<boolean> {
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)
}
}
......
......@@ -248,7 +248,7 @@ export class Bridge {
})
}
public send(toUserName: string, content: string): Promise<void> {
public send(toUserName: string, content: string): Promise<boolean> {
if (!toUserName) {
throw new Error('UserName not found')
}
......@@ -387,7 +387,7 @@ export class Bridge {
}
}
public sendMedia(toUserName: string, mediaId: string, type: number): Promise<void> {
public sendMedia(toUserName: string, mediaId: string, type: number): Promise<boolean> {
if (!toUserName) {
throw new Error('UserName not found')
}
......
......@@ -425,7 +425,7 @@ export class PuppetWeb extends Puppet {
return mediaId as string
}
public async sendMedia(message: MediaMessage): Promise<void> {
public async sendMedia(message: MediaMessage): Promise<boolean> {
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<void> {
public async send(message: Message | MediaMessage): Promise<boolean> {
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<void> {
public async say(content: string): Promise<boolean> {
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)
}
/**
......
......@@ -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
......
......@@ -35,8 +35,8 @@ export abstract class Puppet extends EventEmitter implements Sayable {
public abstract self(): Contact
public abstract send(message: Message | MediaMessage): Promise<void>
public abstract say(content: string): Promise<void>
public abstract send(message: Message | MediaMessage): Promise<boolean>
public abstract say(content: string): Promise<boolean>
public abstract reset(reason?: string): void
public abstract logout(): Promise<void>
......
......@@ -163,12 +163,12 @@ export class Room extends EventEmitter implements Sayable {
return this
}
public say(mediaMessage: MediaMessage): Promise<any>
public say(content: string): Promise<any>
public say(content: string, replyTo: Contact): Promise<void>
public say(content: string, replyTo: Contact[]): Promise<void>
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<void> {
public say(textOrMedia: string | MediaMessage, replyTo?: Contact|Contact[]): Promise<boolean> {
const content = textOrMedia instanceof MediaMessage ? textOrMedia.filename() : textOrMedia
log.verbose('Room', 'say(%s, %s)',
content,
......
......@@ -424,29 +424,27 @@ export class Wechaty extends EventEmitter implements Sayable {
/**
* @todo document me
*/
public async send(message: Message | MediaMessage): Promise<void> {
public async send(message: Message | MediaMessage): Promise<boolean> {
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<void> {
public async say(content: string): Promise<boolean> {
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)
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册