Skip to content

Commit

Permalink
fix(wechat): fix missing properties, fix #193
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 15, 2023
1 parent 8d39dff commit f302796
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion adapters/wechat-official/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@satorijs/adapter-wechat-official",
"description": "Wechat Official Adapter for Satorijs",
"version": "2.0.6",
"version": "2.1.1",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down
4 changes: 4 additions & 0 deletions adapters/wechat-official/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export class WechatOfficialBot<C extends Context = Context> extends Bot<C, Wecha
},
})
}

$toMediaUrl(mediaId: string) {
return `${this.ctx.server.config.selfUrl}/wechat-official/assets/${this.selfId}/${mediaId}`
}
}

export namespace WechatOfficialBot {
Expand Down
6 changes: 3 additions & 3 deletions adapters/wechat-official/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function decodeMessage<C extends Context>(bot: WechatOfficialBot<C>
session.timestamp = message.CreateTime * 1000
session.wechatOfficial = message
session.userId = message.FromUserName
// session.channelId = session.userId
session.channelId = session.userId
// session.guildId = session.userId
session.messageId = message.MsgId
if (message.MsgType === 'text') {
Expand All @@ -24,13 +24,13 @@ export async function decodeMessage<C extends Context>(bot: WechatOfficialBot<C>
} else if (message.MsgType === 'voice') {
session.isDirect = true
session.type = 'message'
session.elements = [h.audio(`${bot.ctx.server.config.selfUrl}/wechat-official/assets/${bot.selfId}/${message.MediaId}`)]
session.elements = [h.audio(bot.$toMediaUrl(message.MediaId))]
// https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Get_temporary_materials.html
return session
} else if (message.MsgType === 'video') {
session.isDirect = true
session.type = 'message'
session.elements = [h.video(`${bot.ctx.server.config.selfUrl}/wechat-official/assets/${bot.selfId}/${message.MediaId}`)]
session.elements = [h.video(bot.$toMediaUrl(message.MediaId))]
// const { video_url } = await bot.getMedia(message.MediaId)
// session.elements = [h.video(video_url)]
return session
Expand Down
2 changes: 1 addition & 1 deletion adapters/wecom/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@satorijs/adapter-wecom",
"description": "Wecom Adapter for Satorijs",
"version": "2.0.6",
"version": "2.1.1",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand Down
4 changes: 4 additions & 0 deletions adapters/wecom/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export class WecomBot<C extends Context = Context> extends Bot<C, WecomBot.Confi
})
}

$toMediaUrl(mediaId: string) {
return `${this.ctx.server.config.selfUrl}/wecom/assets/${this.selfId}/${mediaId}`
}

/** https://developer.work.weixin.qq.com/document/path/90196 */
async getUser(userId: string, guildId?: string): Promise<Universal.User> {
const data = await this.http.get('/cgi-bin/user/get', {
Expand Down
10 changes: 8 additions & 2 deletions adapters/wecom/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ import FormData from 'form-data'

export class WecomMessageEncoder<C extends Context = Context> extends MessageEncoder<C, WecomBot<C>> {
buffer = ''
upsertSend(msgId: string) {

upsertSend(msgId: string, payload: any) {
const session = this.bot.session()
session.type = 'message'
session.messageId = msgId
session.isDirect = true
session.userId = this.bot.selfId
session.timestamp = new Date().valueOf()
session.elements = payload.msgtype === 'text'
? [h.text(payload.text.content)]
: [h(payload.msgtype === 'voice' ? 'audio' : payload.msgtype, {
src: this.bot.$toMediaUrl(payload[payload.msgtype].media_id),
})]
session.app.emit(session, 'send', session)
this.results.push(session.event.message)
}
Expand All @@ -29,7 +35,7 @@ export class WecomMessageEncoder<C extends Context = Context> extends MessageEnc
params: { access_token: this.bot.token },
})

this.upsertSend(msgid)
this.upsertSend(msgid, payload)
}

async flushMedia(element: h) {
Expand Down
6 changes: 3 additions & 3 deletions adapters/wecom/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function decodeMessage<C extends Context>(bot: WecomBot<C>, message
// session.wechatOfficial = message
session.guildId = bot.config.corpId
session.userId = message.FromUserName
// session.channelId = session.userId
session.channelId = session.userId
// session.guildId = session.userId
session.messageId = message.MsgId
if (message.MsgType === 'text') {
Expand All @@ -24,13 +24,13 @@ export async function decodeMessage<C extends Context>(bot: WecomBot<C>, message
} else if (message.MsgType === 'voice') {
session.isDirect = true
session.type = 'message'
session.elements = [h.audio(`${bot.ctx.server.config.selfUrl}/wecom/assets/${bot.selfId}/${message.MediaId}`)]
session.elements = [h.audio(bot.$toMediaUrl(message.MediaId))]
// https://developer.work.weixin.qq.com/document/path/90254
return session
} else if (message.MsgType === 'video') {
session.isDirect = true
session.type = 'message'
session.elements = [h.video(`${bot.ctx.server.config.selfUrl}/wecom/assets/${bot.selfId}/${message.MediaId}`)]
session.elements = [h.video(bot.$toMediaUrl(message.MediaId))]
return session
} else if (message.MsgType === 'location') {
session.isDirect = true
Expand Down

0 comments on commit f302796

Please sign in to comment.