feat(onebot): expose stranger profile signatures
Return the NapCat-compatible long_nick field from get_stranger_info for both freshly fetched and cached user profiles. Keep the field present as an empty string when profile data is unavailable so callers receive a stable response shape. Document the response contract in the Action schema and generated MCP catalog, and cover fetched, cached, and empty fallback paths with regression tests. This change reuses the existing profile signature value and does not add QQ requests or alter protocol packets. Closes #246
This commit is contained in:
6 files changed
+83
-15
No files matched your search
@@ -5751,7 +5751,7 @@
|
||||
"name": "get_stranger_info",
|
||||
"aliases": [],
|
||||
"summary": "获取陌生人信息",
|
||||
"returns": "陌生人资料:QQ 号、昵称、性别、年龄,命中资料时另含等级。",
|
||||
"returns": "陌生人资料:QQ 号、昵称、性别、年龄与个性签名,命中资料时另含等级。",
|
||||
"returnsSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
@@ -5771,6 +5771,10 @@
|
||||
"type": "integer",
|
||||
"description": "年龄"
|
||||
},
|
||||
"long_nick": {
|
||||
"type": "string",
|
||||
"description": "个性签名"
|
||||
},
|
||||
"qq_level": {
|
||||
"type": "integer",
|
||||
"description": "QQ 等级(仅查到资料时返回)"
|
||||
@@ -5784,7 +5788,8 @@
|
||||
"user_id",
|
||||
"nickname",
|
||||
"sex",
|
||||
"age"
|
||||
"age",
|
||||
"long_nick"
|
||||
]
|
||||
},
|
||||
"readOnly": true,
|
||||
|
||||
@@ -5754,7 +5754,7 @@ export const ACTIONS: CatalogAction[] = [
|
||||
"name": "get_stranger_info",
|
||||
"aliases": [],
|
||||
"summary": "获取陌生人信息",
|
||||
"returns": "陌生人资料:QQ 号、昵称、性别、年龄,命中资料时另含等级。",
|
||||
"returns": "陌生人资料:QQ 号、昵称、性别、年龄与个性签名,命中资料时另含等级。",
|
||||
"returnsSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
@@ -5774,6 +5774,10 @@ export const ACTIONS: CatalogAction[] = [
|
||||
"type": "integer",
|
||||
"description": "年龄"
|
||||
},
|
||||
"long_nick": {
|
||||
"type": "string",
|
||||
"description": "个性签名"
|
||||
},
|
||||
"qq_level": {
|
||||
"type": "integer",
|
||||
"description": "QQ 等级(仅查到资料时返回)"
|
||||
@@ -5787,7 +5791,8 @@ export const ACTIONS: CatalogAction[] = [
|
||||
"user_id",
|
||||
"nickname",
|
||||
"sex",
|
||||
"age"
|
||||
"age",
|
||||
"long_nick"
|
||||
]
|
||||
},
|
||||
"readOnly": true,
|
||||
|
||||
@@ -32,7 +32,7 @@ export const actions = [
|
||||
name: 'get_stranger_info',
|
||||
summary: '获取陌生人信息',
|
||||
readOnly: true,
|
||||
returns: '陌生人资料:QQ 号、昵称、性别、年龄,命中资料时另含等级。',
|
||||
returns: '陌生人资料:QQ 号、昵称、性别、年龄与个性签名,命中资料时另含等级。',
|
||||
returnsSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
@@ -40,19 +40,24 @@ export const actions = [
|
||||
nickname: { type: 'string', description: '昵称' },
|
||||
sex: { type: 'string', description: '性别(male/female/unknown)' },
|
||||
age: { type: 'integer', description: '年龄' },
|
||||
long_nick: { type: 'string', description: '个性签名' },
|
||||
qq_level: { type: 'integer', description: 'QQ 等级(仅查到资料时返回)' },
|
||||
level: { type: 'integer', description: 'QQ 等级,同 qq_level(仅查到资料时返回)' },
|
||||
},
|
||||
required: ['user_id', 'nickname', 'sex', 'age'],
|
||||
required: ['user_id', 'nickname', 'sex', 'age', 'long_nick'],
|
||||
},
|
||||
params: { user_id: f.userId().describe('QQ 号') },
|
||||
run: async (p, ctx) => {
|
||||
const userId = p.user_id;
|
||||
if (ctx.getStrangerInfo) {
|
||||
const info = await ctx.getStrangerInfo(userId);
|
||||
return okResponse(info ?? { user_id: userId, nickname: '', sex: 'unknown', age: 0 });
|
||||
return okResponse(info ?? {
|
||||
user_id: userId, nickname: '', sex: 'unknown', age: 0, long_nick: '',
|
||||
});
|
||||
}
|
||||
return okResponse({ user_id: userId, nickname: '', sex: 'unknown', age: 0 });
|
||||
return okResponse({
|
||||
user_id: userId, nickname: '', sex: 'unknown', age: 0, long_nick: '',
|
||||
});
|
||||
},
|
||||
}),
|
||||
|
||||
@@ -66,4 +71,3 @@ export const actions = [
|
||||
},
|
||||
}),
|
||||
];
|
||||
|
||||
@@ -225,6 +225,7 @@ export async function getStrangerInfo(
|
||||
nickname: p.nickname,
|
||||
sex: p.sex,
|
||||
age: p.age,
|
||||
long_nick: p.sign,
|
||||
qq_level: p.level,
|
||||
level: p.level,
|
||||
};
|
||||
@@ -236,6 +237,7 @@ export async function getStrangerInfo(
|
||||
nickname: p.nickname,
|
||||
sex: p.sex,
|
||||
age: p.age,
|
||||
long_nick: p.sign,
|
||||
qq_level: p.level,
|
||||
level: p.level,
|
||||
};
|
||||
|
||||
@@ -89,9 +89,15 @@ function makeMember(uin: number, nickname: string, card = ''): GroupMemberInfo {
|
||||
};
|
||||
}
|
||||
|
||||
function makeProfile(uin: number, nickname: string, sex: 'male' | 'female' | 'unknown' = 'unknown', age = 0): UserProfileInfo {
|
||||
function makeProfile(
|
||||
uin: number,
|
||||
nickname: string,
|
||||
sex: 'male' | 'female' | 'unknown' = 'unknown',
|
||||
age = 0,
|
||||
sign = '',
|
||||
): UserProfileInfo {
|
||||
return {
|
||||
uin, uid: `u_${uin}`, nickname, remark: '', qid: '', sex, age, sign: '', avatar: '',
|
||||
uin, uid: `u_${uin}`, nickname, remark: '', qid: '', sex, age, sign, avatar: '',
|
||||
};
|
||||
}
|
||||
|
||||
@@ -353,10 +359,16 @@ describe('onebot/contact-actions / getGroupMemberInfo', () => {
|
||||
describe('onebot/contact-actions / getStrangerInfo', () => {
|
||||
it('returns a fetched profile', async () => {
|
||||
const bridge = fakeBridge({
|
||||
fetchUserProfile: vi.fn(async () => makeProfile(55555, 'Eve', 'female', 25)),
|
||||
fetchUserProfile: vi.fn(async () => makeProfile(55555, 'Eve', 'female', 25, 'Stay curious')),
|
||||
});
|
||||
const out = await getStrangerInfo(bridge, 55555);
|
||||
expect(out).toMatchObject({ user_id: 55555, nickname: 'Eve', sex: 'female', age: 25 });
|
||||
expect(out).toMatchObject({
|
||||
user_id: 55555,
|
||||
nickname: 'Eve',
|
||||
sex: 'female',
|
||||
age: 25,
|
||||
long_nick: 'Stay curious',
|
||||
});
|
||||
});
|
||||
|
||||
it('falls back to identity.findUserProfile when fetch fails but the profile is cached', async () => {
|
||||
@@ -364,11 +376,15 @@ describe('onebot/contact-actions / getStrangerInfo', () => {
|
||||
fetchUserProfile: vi.fn(async () => { throw new Error('net'); }),
|
||||
identity: fakeIdentity({
|
||||
findUserProfile: (uin: number) =>
|
||||
uin === 66666 ? makeProfile(66666, 'Frank', 'male', 30) : null,
|
||||
uin === 66666 ? makeProfile(66666, 'Frank', 'male', 30, 'Cached signature') : null,
|
||||
}),
|
||||
});
|
||||
const out = await getStrangerInfo(bridge, 66666);
|
||||
expect(out).toMatchObject({ user_id: 66666, nickname: 'Frank' });
|
||||
expect(out).toMatchObject({
|
||||
user_id: 66666,
|
||||
nickname: 'Frank',
|
||||
long_nick: 'Cached signature',
|
||||
});
|
||||
});
|
||||
|
||||
it('returns null when neither fetch nor cache produces a profile', async () => {
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { ApiActionContext } from '../src/api-handler';
|
||||
import { actions } from '../src/actions/friend';
|
||||
|
||||
const getStrangerInfo = actions.find((action) => action.names[0] === 'get_stranger_info');
|
||||
if (!getStrangerInfo) throw new Error('get_stranger_info action missing');
|
||||
|
||||
describe('get_stranger_info action', () => {
|
||||
it('returns an empty long_nick when no profile provider is available', async () => {
|
||||
const response = await getStrangerInfo.toHandler({} as ApiActionContext)({ user_id: 123456 });
|
||||
|
||||
expect(response).toMatchObject({
|
||||
status: 'ok',
|
||||
retcode: 0,
|
||||
data: {
|
||||
user_id: 123456,
|
||||
nickname: '',
|
||||
sex: 'unknown',
|
||||
age: 0,
|
||||
long_nick: '',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('documents long_nick as a required string response field', () => {
|
||||
const schema = getStrangerInfo.describe().returnsSchema;
|
||||
|
||||
expect(schema).toMatchObject({
|
||||
type: 'object',
|
||||
properties: {
|
||||
long_nick: { type: 'string', description: '个性签名' },
|
||||
},
|
||||
});
|
||||
expect(schema?.required).toContain('long_nick');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user