Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable sending voice message through messageSendFile() #20

Merged
merged 4 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/official-account/official-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,10 @@ class OfficialAccount extends EventEmitter {
async sendFile (args: {
file : FileBox,
touser : string,
msgtype : OAMediaType
msgtype : OAMediaType,
}): Promise<string> {
log.verbose('OfficialAccount', 'sendFile(%s)', JSON.stringify(args))
// log.verbose('OfficialAccount', 'sendFile(%s)', JSON.stringify(args))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep this log verbose output, because we always log a function when it's being called.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i uncommented it. just to note that this line does not work for audio file such as .mp3, and will throw an error.

// JSON.stringify does not support .mp3 filetype

await args.file.ready()
const { buf, info } = await normalizeFileBox(args.file)
Expand All @@ -310,6 +311,7 @@ class OfficialAccount extends EventEmitter {
// and fetched fileBox has no name, which will cause error in upload file process.
// this works for all of the image file
// TODO -> should be improved later.

if (args.file.type() === FileBoxType.Url && args.file.mimeType === 'image/jpeg') {
info.filename = `${args.file.name}.jpeg`
}
Expand All @@ -325,13 +327,14 @@ class OfficialAccount extends EventEmitter {
}

const data = {
image :
{
media_id : mediaResponse.body.media_id,
},
[args.msgtype] :
{
media_id : mediaResponse.body.media_id,
},
msgtype : args.msgtype,
touser : args.touser,
}
}

const messageResponse = await this.simpleUnirest.post<ErrorPayload>(`message/custom/send?access_token=${this.accessToken}`).type('json').send(data)
if (messageResponse.body.errcode) {
log.error('OfficialAccount', 'SendFile() can not send file to wechat user .<%s>', messageResponse.body.errmsg)
Expand Down
12 changes: 5 additions & 7 deletions src/puppet-oa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
import {
OAContactPayload,
OAMessagePayload,
OAMediaType,
} from './official-account/schema'

export type PuppetOAOptions = PuppetOptions & Partial<OfficialAccountOptions>
Expand Down Expand Up @@ -152,7 +153,6 @@ class PuppetOA extends Puppet {

public async start (): Promise<void> {
log.verbose('PuppetOA', 'start()')

if (this.state.on()) {
log.warn('PuppetOA', 'start() is called on a ON puppet. await ready(on) and return.')
await this.state.ready('on')
Expand All @@ -161,7 +161,6 @@ class PuppetOA extends Puppet {

try {
this.state.on('pending')

this.oa = new OfficialAccount({
appId : this.appId,
appSecret : this.appSecret,
Expand All @@ -170,7 +169,6 @@ class PuppetOA extends Puppet {
token : this.token,
webhookProxyUrl : this.webhookProxyUrl,
})

// FIXME: Huan(202008) find a way to get the bot user information
// Official Account Info can be customized by user, so It should be
// configured by environment variable.
Expand All @@ -179,9 +177,7 @@ class PuppetOA extends Puppet {
await this.oa.payloadStore.setContactPayload(this.id, { openid: this.id } as any)

this.bridgeEvents(this.oa)

await this.oa.start()

this.state.on(true)
} catch (e) {
log.error('PuppetOA', 'start() rejection: %s', e)
Expand Down Expand Up @@ -495,6 +491,7 @@ class PuppetOA extends Puppet {
private async messageSend (
conversationId: string,
something: string | FileBox, // | Attachment
mediatype: OAMediaType = 'image'
): Promise<string> {
log.verbose('PuppetOA', 'messageSend(%s, %s)', conversationId, something)
if (!this.id) {
Expand All @@ -516,7 +513,7 @@ class PuppetOA extends Puppet {
msgId = await this.oa?.sendCustomMessage(payload)
}
} else if (something instanceof FileBox) {
await this.oa?.sendFile({ file: something, msgtype: 'image', touser: conversationId })
await this.oa?.sendFile({ file: something, msgtype: mediatype, touser: conversationId })
}
if (!msgId) {
throw new Error('PuppetOA messageSend() can"t get msgId response')
Expand All @@ -535,7 +532,8 @@ class PuppetOA extends Puppet {
conversationId: string,
file : FileBox,
): Promise<string> {
return this.messageSend(conversationId, file)
const msgtype: OAMediaType = (file.mimeType === 'image/jpeg') ? 'image' : 'voice'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this logic more robust?

I'd like to suggest that:

  1. we do a switch for checking file.mimeType and set the MsgType correctly
  2. we throw an exception for the unknown mimeType instead of failing silently

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the suggestions, have updated

return this.messageSend(conversationId, file, msgtype)
}

public async messageSendContact (
Expand Down