fix(protocol,onebot): generate and send ark of contact cards correctly (#149)

This commit is contained in:
Airline233 authored and GitHub committed 2026-06-26 17:04:40 +08:00
1 parent 02de2acea4
commit 39338c3f1d
5 files changed
+43 -29

No files matched your search

+8
View File
@@ -9,6 +9,7 @@ export interface ParseMessageOptions {
resolveReplySequence?: (replyMessageId: number) => number | null;
resolveReplyMeta?: (replyMessageId: number) => { senderUin: number; time: number; random: number } | null;
resolveMentionUid?: (targetUin: number) => string | null | Promise<string | null>;
resolveContactArk?: (contactType: string, contactId: number) => string | null | Promise<string | null>;
musicSignUrl?: string;
}
@@ -291,6 +292,13 @@ export async function segmentToElement(type: string, data: Record<string, unknow
// Contact card — map to json card
const contactType = String(data.type ?? 'qq');
const contactId = String(data.id ?? '');
const numericId = intOr(contactId, 0);
const normalizedContactType = contactType.trim().toLowerCase();
if (numericId > 0 && options?.resolveContactArk && (normalizedContactType === 'qq' || normalizedContactType === 'group')) {
const ark = await options.resolveContactArk(contactType, numericId);
if (!ark) throw new Error(`contact ark unavailable for ${contactType}:${numericId}`);
return { type: 'json', text: ark };
}
const jsonData = JSON.stringify({
app: 'com.tencent.contact.lua',
view: 'contact',
@@ -448,6 +448,13 @@ async function cacheSelfSentMessage(
}
}
function resolveContactArk(ref: OneBotInstanceContext, contactType: string, contactId: number): Promise<string> | null {
const normalized = contactType.trim().toLowerCase();
if (normalized === 'qq') return ref.bridge.apis.contacts.getBuddyRecommendArk(contactId, '');
if (normalized === 'group') return ref.bridge.apis.contacts.getGroupRecommendArk(contactId);
return null;
}
export async function sendPrivateMessage(
ref: OneBotInstanceContext,
userId: number,
@@ -492,6 +499,7 @@ export async function sendPrivateMessage(
return null;
},
resolveMentionUid: (targetUin) => ref.bridge.resolveUserUid(targetUin),
resolveContactArk: (contactType, contactId) => resolveContactArk(ref, contactType, contactId),
musicSignUrl: ref.musicSignUrl,
});
if (elements.length === 0) throw new Error('message is empty');
@@ -595,6 +603,7 @@ export async function sendGroupMessage(
return null;
},
resolveMentionUid: (targetUin) => ref.bridge.resolveUserUid(targetUin, groupId),
resolveContactArk: (contactType, contactId) => resolveContactArk(ref, contactType, contactId),
musicSignUrl: ref.musicSignUrl,
});
if (elements.length === 0) throw new Error('message is empty');