Skip to content

Commit

Permalink
feat(lark): use openid as selfId
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX committed Apr 7, 2024
1 parent a237551 commit 772be0f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
9 changes: 5 additions & 4 deletions adapters/lark/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export class LarkBot<C extends Context = Context> extends Bot<C, LarkBot.Config>
assetsQuester: Quester
internal: Internal

public openId: string

constructor(ctx: C, config: LarkBot.Config) {
super(ctx, config, 'lark')

Expand All @@ -25,7 +23,6 @@ export class LarkBot<C extends Context = Context> extends Bot<C, LarkBot.Config>
this.logger.warn('selfUrl is not set, some features may not work')
}

this.selfId = config.appId
this.http = ctx.http.extend({
endpoint: config.endpoint,
})
Expand All @@ -35,6 +32,10 @@ export class LarkBot<C extends Context = Context> extends Bot<C, LarkBot.Config>
ctx.plugin(HttpServer, this)
}

get appId() {
return this.config.appId
}

async initialize() {
await this.refreshToken()
const { bot } = await this.http.get<{
Expand All @@ -46,7 +47,7 @@ export class LarkBot<C extends Context = Context> extends Bot<C, LarkBot.Config>
open_id: string
}
}>('/bot/v3/info')
this.openId = bot.open_id
this.selfId = bot.open_id
this.user.avatar = bot.avatar_url
this.user.name = bot.app_name
this.online()
Expand Down
5 changes: 2 additions & 3 deletions adapters/lark/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, FeishuBo
params: { type },
responseType: 'stream',
})

ctx.set('content-type', resp.headers.get('content-type'))
ctx.status = 200
ctx.response.headers['Content-Type'] = resp.headers.get('content-type')
ctx.response.body = Readable.fromWeb(resp.data)
})
}
Expand All @@ -104,7 +103,7 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, FeishuBo
if (!header) return
const { app_id, event_type } = header
body.type = event_type // add type to body to ease typescript type narrowing
const bot = this.bots.find((bot) => bot.selfId === app_id)
const bot = this.bots.find((bot) => bot.appId === app_id)
const session = await adaptSession(bot, body)
bot.dispatch(session)
}
Expand Down
4 changes: 1 addition & 3 deletions adapters/lark/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ export async function adaptMessage(bot: FeishuBot, data: Events['im.message.rece
text.split(' ').forEach((word) => {
if (word.startsWith('@')) {
const mention = data.message.mentions.find((mention) => mention.key === word)
let id = mention.id.open_id
if (id === bot.openId) id = bot.selfId
content.push(h.at(id, { name: mention.name }))
content.push(h.at(mention.id.open_id, { name: mention.name }))
} else {
content.push(word)
}
Expand Down

0 comments on commit 772be0f

Please sign in to comment.