Skip to content

Commit

Permalink
feat(lark): support editMessage for cards
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 23, 2024
1 parent 7e3453f commit 27347ec
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion adapters/lark/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@satorijs/adapter-lark",
"description": "Lark (飞书) Adapter for Satorijs",
"version": "3.8.2",
"version": "3.8.3",
"type": "module",
"main": "lib/index.cjs",
"types": "lib/index.d.ts",
Expand Down
7 changes: 3 additions & 4 deletions adapters/lark/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ export class LarkBot<C extends Context = Context> extends Bot<C, LarkBot.Config>
}

async editMessage(channelId: string, messageId: string, content: h.Fragment) {
await this.internal.updateImMessage(messageId, {
content: h.normalize(content).join(''),
msg_type: 'text',
})
const encoder = new LarkMessageEncoder(this, channelId)
encoder.editMessageIds = [messageId]
await encoder.send(content)
}

async deleteMessage(channelId: string, messageId: string) {
Expand Down
15 changes: 14 additions & 1 deletion adapters/lark/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
private noteElements: MessageContent.Card.NoteElement.InnerElement[] | undefined
private actionElements: MessageContent.Card.Element[] = []

public editMessageIds: string[] | undefined

async post(data?: any) {
try {
let resp: Lark.Message
if (this.quote?.id) {
if (this.editMessageIds) {
const messageId = this.editMessageIds.pop()
if (!messageId) throw new Error('No message to edit')
if (data.msg_type === 'interactive') {
delete data.msg_type
await this.bot.internal.patchImMessage(messageId, data)
} else {
await this.bot.internal.updateImMessage(messageId, data)
}
} else if (this.quote?.id) {
resp = await this.bot.internal.replyImMessage(this.quote.id, {
...data,
reply_in_thread: this.quote.replyInThread,
Expand All @@ -25,6 +36,7 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
receive_id_type: extractIdType(this.channelId),
})
}
if (!resp) return
const session = this.bot.session()
session.messageId = resp.message_id
session.timestamp = Number(resp.create_time) * 1000
Expand Down Expand Up @@ -70,6 +82,7 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
await this.post({
msg_type: 'interactive',
content: JSON.stringify({
header: this.card.header,
elements: this.card.elements,
}),
})
Expand Down
2 changes: 1 addition & 1 deletion adapters/satori/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@satorijs/adapter-satori",
"description": "Satori Adapter for Satorijs",
"version": "1.4.5",
"version": "1.4.6",
"type": "module",
"exports": {
".": {
Expand Down
2 changes: 1 addition & 1 deletion adapters/satori/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ for (const [key, method] of Object.entries(Universal.Methods)) {
} else {
payload = {}
for (const [index, field] of method.fields.entries()) {
if (method.name === 'createMessage' && field.name === 'content') {
if ((method.name === 'createMessage' || method.name === 'editMessage') && field.name === 'content') {
const session = this.session({
type: 'send',
channel: { id: args[0], type: 0 },
Expand Down

0 comments on commit 27347ec

Please sign in to comment.