Skip to content

Commit

Permalink
Simplify sayable message say
Browse files Browse the repository at this point in the history
  • Loading branch information
huan committed Jul 11, 2020
1 parent 65ccb9e commit ab9fcfa
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 27 deletions.
16 changes: 4 additions & 12 deletions src/talkers/message-talker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
import {
Message,
log,
FileBox,
Contact,
UrlLink,
MiniProgram,
} from 'wechaty'
import Mustache from 'mustache'

Expand Down Expand Up @@ -36,22 +32,18 @@ export function messageTalker<T = void> (options?: MessageTalkerOptions) {
if (!msg) { continue }

if (typeof msg === 'string') {

let text = msg
if (mustacheView) {
text = Mustache.render(msg, mustacheView)
}
await message.say(text)

} else {
/**
* Super verbose:
* https://github.com/microsoft/TypeScript/issues/14107
* FIXME(huan): https://github.com/microsoft/TypeScript/issues/14107
*/
if (msg instanceof FileBox) { await message.say(msg) }
else if (msg instanceof Contact) { await message.say(msg) }
else if (msg instanceof UrlLink) { await message.say(msg) }
else if (msg instanceof MiniProgram) { await message.say(msg) }
else if (msg instanceof Message) { await message.say(msg) }
else { throw new Error('unknown msg type: ' + typeof msg) }
await message.say(msg as any)
}

await message.wechaty.sleep(1000)
Expand Down
17 changes: 2 additions & 15 deletions src/talkers/room-talker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import {
log,
Room,
Contact,
FileBox,
UrlLink,
MiniProgram,
Message,
} from 'wechaty'
import Mustache from 'mustache'

Expand Down Expand Up @@ -60,18 +56,9 @@ export function roomTalker<T = void> (options?: RoomTalkerOptions) {
}

/**
* Super verbose:
* https://github.com/microsoft/TypeScript/issues/14107
* FIXME(huan): https://github.com/microsoft/TypeScript/issues/14107
*/
else if (msg instanceof FileBox) { await room.say(msg) }
else if (msg instanceof Contact) { await room.say(msg) }
else if (msg instanceof UrlLink) { await room.say(msg) }
else if (msg instanceof MiniProgram) { await room.say(msg) }
else if (msg instanceof Message) { await room.say(msg) }

else {
throw new Error('talkRoom() msg unknown: ' + msg)
}
await room.say(msg as any)

await room.wechaty.sleep(1000)
}
Expand Down

0 comments on commit ab9fcfa

Please sign in to comment.