Skip to content

Commit

Permalink
fix(wechat-official): fix bot.platform
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 24, 2023
1 parent 0db8aa3 commit 0323122
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
5 changes: 2 additions & 3 deletions adapters/wechat-official/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ export class WechatOfficialBot extends Bot<WechatOfficialBot.Config> {
logger = new Logger('wo')
constructor(ctx: Context, config: WechatOfficialBot.Config) {
super(ctx, config)
this.platform = 'wechat-official'
this.selfId = config.account
this.http = ctx.http.extend(config)
// this.internal = new Internal(this.http, this)

ctx.plugin(HttpServer, this)
}

Expand Down Expand Up @@ -101,5 +102,3 @@ export namespace WechatOfficialBot {
Quester.createConfig('https://api.weixin.qq.com/'),
])
}

WechatOfficialBot.prototype.platform = 'wechatofficial'
14 changes: 6 additions & 8 deletions adapters/wechat-official/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ export class HttpServer extends Adapter.Server<WechatOfficialBot> {
}

async start(bot: WechatOfficialBot) {
bot.selfId = bot.config.account
bot.platform = 'wechatofficial'
await bot.refreshToken()
await bot.ensureCustom()
// https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html
bot.ctx.router.get('/wechatofficial', async (ctx) => {
bot.ctx.router.get('/wechat-official', async (ctx) => {
let success = false
const { signature, timestamp, nonce, echostr } = ctx.request.query

for (const localBot of this.bots.filter(v => v.platform === 'wechatofficial')) {
const localSign = getSignature(localBot.config.token, timestamp?.toString(), nonce?.toString(), '')
for (const bot of this.bots) {
const localSign = getSignature(bot.config.token, timestamp?.toString(), nonce?.toString(), '')
if (localSign === signature) {
success = true
break
Expand All @@ -31,7 +29,7 @@ export class HttpServer extends Adapter.Server<WechatOfficialBot> {
ctx.body = echostr
})

bot.ctx.router.post('/wechatofficial', async (ctx) => {
bot.ctx.router.post('/wechat-official', async (ctx) => {
const { timestamp, nonce, msg_signature } = ctx.request.query
let { xml: data }: {
xml: Message
Expand Down Expand Up @@ -63,7 +61,7 @@ export class HttpServer extends Adapter.Server<WechatOfficialBot> {
const timeout = setTimeout(() => {
ctx.status = 200
ctx.body = 'success'
reject('timeout')
reject(new Error('timeout'))
}, 4500)
resolveFunction = (text: string) => {
resolve(text)
Expand Down Expand Up @@ -103,7 +101,7 @@ export class HttpServer extends Adapter.Server<WechatOfficialBot> {
ctx.body = 'success'
}
})
bot.ctx.router.get('/wechatofficial/assets/:self_id/:media_id', async (ctx) => {
bot.ctx.router.get('/wechat-official/assets/:self_id/:media_id', async (ctx) => {
const mediaId = ctx.params.media_id
const selfId = ctx.params.self_id
const localBot = this.bots.find((bot) => bot.selfId === selfId)
Expand Down
7 changes: 4 additions & 3 deletions adapters/wechat-official/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Message } from './types'
import { WechatOfficialBot } from './bot'
import { h } from '@satorijs/satori'

export async function decodeMessage(bot: WechatOfficialBot, message: Message) {
const session = bot.session()
// https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_standard_messages.html
Expand All @@ -26,20 +27,20 @@ export async function decodeMessage(bot: WechatOfficialBot, message: Message) {
} else if (message.MsgType === 'voice') {
session.isDirect = true
session.type = 'message'
session.elements = [h.audio(`${bot.ctx.root.config.selfUrl}/wechatofficial/assets/${bot.selfId}/${message.MediaId}`)]
session.elements = [h.audio(`${bot.ctx.root.config.selfUrl}/wechat-official/assets/${bot.selfId}/${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.root.config.selfUrl}/wechatofficial/assets/${bot.selfId}/${message.MediaId}`)]
session.elements = [h.video(`${bot.ctx.root.config.selfUrl}/wechat-official/assets/${bot.selfId}/${message.MediaId}`)]
// const { video_url } = await bot.getMedia(message.MediaId)
// session.elements = [h.video(video_url)]
return session
} else if (message.MsgType === 'location') {
session.isDirect = true
session.type = 'message'
session.elements = [h('wechatofficial:location', {
session.elements = [h('wechat-official:location', {
latitude: message.Location_X,
longitude: message.Location_Y,
label: message.Label,
Expand Down

0 comments on commit 0323122

Please sign in to comment.