Skip to content

Commit

Permalink
refa: config.selfUrl moved to ctx.router
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 26, 2023
1 parent d707119 commit 78fa90d
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 14 deletions.
3 changes: 2 additions & 1 deletion adapters/lark/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as Utils from './utils'
const logger = new Logger('lark')

export class LarkBot<C extends Context = Context> extends Bot<C, LarkBot.Config> {
static inject = ['router']
static MessageEncoder = LarkMessageEncoder

_token?: string
Expand All @@ -20,7 +21,7 @@ export class LarkBot<C extends Context = Context> extends Bot<C, LarkBot.Config>
super(ctx, config)

// lark bot needs config.selfUrl to be set as it should be serve on a public url
if (!config.selfUrl && !ctx.root.config.selfUrl) {
if (!config.selfUrl && !ctx.router.config.selfUrl) {
logger.warn('selfUrl is not set, some features may not work')
}

Expand Down
4 changes: 2 additions & 2 deletions adapters/lark/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function adaptSender(sender: Sender, session: Session): Session {

export function adaptMessage(bot: FeishuBot, data: Events['im.message.receive_v1']['event'], session: Session): Session {
const json = JSON.parse(data.message.content) as MessageContentType<MessageType>
const assetEndpoint = trimSlash(bot.config.selfUrl ?? bot.ctx.root.config.selfUrl) + bot.config.path + '/assets'
const assetEndpoint = trimSlash(bot.config.selfUrl ?? bot.ctx.router.config.selfUrl) + bot.config.path + '/assets'
const content: (string | h)[] = []
switch (data.message.message_type) {
case 'text': {
Expand Down Expand Up @@ -88,7 +88,7 @@ export function adaptSession<C extends Context>(bot: FeishuBot<C>, body: AllEven
// TODO: This function has many duplicated code with `adaptMessage`, should refactor them
export async function decodeMessage(bot: LarkBot, body: LarkMessage): Promise<Message> {
const json = JSON.parse(body.body.content) as MessageContentType<MessageType>
const assetEndpoint = trimSlash(bot.config.selfUrl ?? bot.ctx.root.config.selfUrl) + bot.config.path + '/assets'
const assetEndpoint = trimSlash(bot.config.selfUrl ?? bot.ctx.router.config.selfUrl) + bot.config.path + '/assets'
const content: h[] = []
switch (body.msg_type) {
case 'text': {
Expand Down
4 changes: 3 additions & 1 deletion adapters/line/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import { LineMessageEncoder } from './message'
const logger = new Logger('line')

export class LineBot<C extends Context = Context> extends Bot<C, LineBot.Config> {
static inject = ['router']
static MessageEncoder = LineMessageEncoder

public http: Quester
public contentHttp: Quester
public internal: Internal

constructor(ctx: C, config: LineBot.Config) {
super(ctx, config)
if (!ctx.root.config.selfUrl) {
if (!ctx.router.config.selfUrl) {
logger.warn('selfUrl is not set, some features may not work')
}

Expand Down
6 changes: 3 additions & 3 deletions adapters/line/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ export async function adaptMessage(bot: LineBot, message: EventMessage) {
} while (nowPos !== finalLen)
} else if (message.type === 'image') {
if (message.contentProvider.type === 'line') {
return [h.image(`${bot.ctx.root.config.selfUrl}/line/assets/${bot.selfId}/${message.id}`)]
return [h.image(`${bot.ctx.router.config.selfUrl}/line/assets/${bot.selfId}/${message.id}`)]
} else {
return [h.image(message.contentProvider.originalContentUrl)]
}
} else if (message.type === 'video') {
if (message.contentProvider.type === 'line') {
return [h.video(`${bot.ctx.root.config.selfUrl}/line/assets/${bot.selfId}/${message.id}`)]
return [h.video(`${bot.ctx.router.config.selfUrl}/line/assets/${bot.selfId}/${message.id}`)]
} else {
return [h.video(message.contentProvider.originalContentUrl)]
}
} else if (message.type === 'sticker') {
return [h('face', { type: 'sticker', id: `s:${message.packageId}:${message.stickerId}`, platform: bot.platform })]
} else if (message.type === 'file') {
return [h.file(`${bot.ctx.root.config.selfUrl}/line/assets/${bot.selfId}/${message.id}`)]
return [h.file(`${bot.ctx.router.config.selfUrl}/line/assets/${bot.selfId}/${message.id}`)]
}
return result
}
Expand Down
2 changes: 1 addition & 1 deletion adapters/telegram/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class TelegramBot<C extends Context = Context, T extends TelegramBot.Conf
} else if (config.protocol === 'polling') {
ctx.plugin(HttpPolling, this)
}
const selfUrl: string = config['selfUrl'] || ctx.root.config.selfUrl
const selfUrl: string = config['selfUrl'] || ctx.get('router').config.selfUrl
if (config.files.server ?? selfUrl) {
const route = `/telegram/${this.selfId}`
this.server = selfUrl + route
Expand Down
2 changes: 2 additions & 0 deletions adapters/wechat-official/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { WechatOfficialMessageEncoder } from './message'
// import { Internal } from './types/internal'

export class WechatOfficialBot<C extends Context = Context> extends Bot<C, WechatOfficialBot.Config> {
static inject = ['router']
static MessageEncoder = WechatOfficialMessageEncoder

http: Quester
// internal: Internal
refreshTokenTimer: NodeJS.Timeout
Expand Down
4 changes: 2 additions & 2 deletions adapters/wechat-official/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.root.config.selfUrl}/wechat-official/assets/${bot.selfId}/${message.MediaId}`)]
session.elements = [h.audio(`${bot.ctx.router.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}/wechat-official/assets/${bot.selfId}/${message.MediaId}`)]
session.elements = [h.video(`${bot.ctx.router.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
Expand Down
1 change: 1 addition & 0 deletions adapters/wecom/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HttpServer } from './http'
import { WecomMessageEncoder } from './message'

export class WecomBot<C extends Context = Context> extends Bot<C, WecomBot.Config> {
static inject = ['router']
static MessageEncoder = WecomMessageEncoder

http: Quester
Expand Down
4 changes: 2 additions & 2 deletions adapters/wecom/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.root.config.selfUrl}/wecom/assets/${bot.selfId}/${message.MediaId}`)]
session.elements = [h.audio(`${bot.ctx.router.config.selfUrl}/wecom/assets/${bot.selfId}/${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.root.config.selfUrl}/wecom/assets/${bot.selfId}/${message.MediaId}`)]
session.elements = [h.video(`${bot.ctx.router.config.selfUrl}/wecom/assets/${bot.selfId}/${message.MediaId}`)]
return session
} else if (message.MsgType === 'location') {
session.isDirect = true
Expand Down
1 change: 1 addition & 0 deletions adapters/whatsapp/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class HttpServer {
}

export class WhatsAppAdapter<C extends Context = Context> extends Adapter<C, WhatsAppBot<C>> {
static inject = ['router']
static schema = true
static reusable = true

Expand Down
1 change: 1 addition & 0 deletions adapters/whatsapp/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { WhatsAppMessageEncoder } from './message'
import { Internal } from './internal'

export class WhatsAppBot<C extends Context = Context> extends Bot<C> {
static inject = ['router']
static MessageEncoder = WhatsAppMessageEncoder

public internal: Internal
Expand Down
4 changes: 2 additions & 2 deletions adapters/whatsapp/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ export async function decodeMessage(bot: WhatsAppBot, entry: Entry) {
if (message.type === 'document') type = 'file'
const resource = message[message.type]
if (resource.caption) elements.push(h.text(message[message.type].caption))
elements.push(h[type](`${bot.ctx.root.config.selfUrl}/whatsapp/assets/${bot.selfId}/${resource.id}`))
elements.push(h[type](`${bot.ctx.router.config.selfUrl}/whatsapp/assets/${bot.selfId}/${resource.id}`))
session.elements = elements
} else if (message.type === 'sticker') {
session.elements = [h('face', {
id: /* (message.sticker.animated ? 'a:' : '') + */message.sticker.id,
platform: 'whatsapp',
}, [
h.image(`${bot.ctx.root.config.selfUrl}/whatsapp/assets/${bot.selfId}/${message.sticker.id}`),
h.image(`${bot.ctx.router.config.selfUrl}/whatsapp/assets/${bot.selfId}/${message.sticker.id}`),
])]
} else if (message.type === 'location') {
session.elements = [h('whatsapp:location', {
Expand Down

0 comments on commit 78fa90d

Please sign in to comment.