send_private_msg / send_msg can now reply to a group temp-session message by passing the source group_id. Inbound temp messages surface their source group on sender.group_id; a reply is only made into a temp session the bot has an inbound record of. Group temp replies route through RoutingHead.grpTmp (field 3).
136 lines
5.6 KiB
TypeScript
136 lines
5.6 KiB
TypeScript
import { createLogger } from '@snowluma/common/logger';
|
|
import type { JsonObject, JsonValue } from '../types';
|
|
import type { ApiActionContext } from '../api-handler';
|
|
import { defineAction, groupAction, f } from '../action-kit';
|
|
import { RETCODE, failedResponse, okResponse } from '../types';
|
|
|
|
const log = createLogger('OneBot');
|
|
|
|
/**
|
|
* Re-sign image URLs in a stored message event at read time. `get_msg`
|
|
* returns a copy persisted when the message first arrived, and image rkeys
|
|
* expire — so walk the segment array and refresh each image URL through
|
|
* `ctx.getImageInfo`, which mints a current rkey. Best-effort and in-place;
|
|
* `findEvent` returns a fresh parse, so mutating the array is safe.
|
|
*/
|
|
async function refreshStoredImageUrls(event: JsonObject, ctx: ApiActionContext): Promise<void> {
|
|
const segments = event.message;
|
|
if (!Array.isArray(segments)) return;
|
|
for (const seg of segments) {
|
|
if (!seg || typeof seg !== 'object') continue;
|
|
const segment = seg as { type?: unknown; data?: Record<string, JsonValue> };
|
|
if (segment.type !== 'image') continue;
|
|
const data = segment.data;
|
|
if (!data || typeof data !== 'object') continue;
|
|
const file = typeof data.file === 'string' ? data.file
|
|
: typeof data.file_id === 'string' ? data.file_id
|
|
: '';
|
|
if (!file) continue;
|
|
try {
|
|
const info = await ctx.getImageInfo(file);
|
|
if (info && typeof info.url === 'string' && info.url) data.url = info.url;
|
|
} catch {
|
|
// Keep the stored URL when the refresh fails.
|
|
}
|
|
}
|
|
}
|
|
|
|
export const actions = [
|
|
// send_msg routes on message_type / group_id presence, so the *required*
|
|
// id is conditional — that branch stays in run(). The fields themselves
|
|
// (message required; group_id/user_id valid uints when present) are
|
|
// validated by the spec; message_type is left lenient for parity.
|
|
defineAction({
|
|
name: 'send_msg',
|
|
summary: '发送消息(按 message_type/群号 自动路由群聊或私聊)',
|
|
returns: '{ message_id: number }',
|
|
params: {
|
|
message: f.message(),
|
|
message_type: f.string().optional(),
|
|
group_id: f.groupId().optional(),
|
|
user_id: f.userId().optional(),
|
|
auto_escape: f.bool().default(false),
|
|
},
|
|
run: async (p, ctx) => {
|
|
// Group temp-session reply: message_type=private with BOTH a user_id and
|
|
// a group_id (the source group). Detect it first so a group_id here
|
|
// doesn't get misrouted to a group message.
|
|
const isTempReply = p.message_type === 'private' && p.user_id !== undefined && p.group_id !== undefined;
|
|
if (!isTempReply && (p.message_type === 'group' || p.group_id !== undefined)) {
|
|
if (p.group_id === undefined) return failedResponse(RETCODE.BAD_REQUEST, 'group_id is required');
|
|
const result = await ctx.sendGroupMessage(p.group_id, p.message, p.auto_escape);
|
|
return okResponse({ message_id: result.messageId });
|
|
}
|
|
if (p.user_id === undefined) return failedResponse(RETCODE.BAD_REQUEST, 'user_id is required');
|
|
const result = await ctx.sendPrivateMessage(p.user_id, p.message, p.auto_escape, isTempReply ? p.group_id : undefined);
|
|
return okResponse({ message_id: result.messageId });
|
|
},
|
|
}),
|
|
|
|
defineAction({
|
|
name: 'send_private_msg',
|
|
summary: '发送私聊消息',
|
|
returns: '{ message_id: number }',
|
|
params: {
|
|
user_id: f.userId(),
|
|
message: f.message(),
|
|
// Optional source group: reply into that group's temp session (临时会话)
|
|
// instead of a friend chat. int({min:0}) rather than groupId (which
|
|
// rejects 0): a client that fills every field with group_id:0 must keep
|
|
// working as a plain private send, so 0/absent means "no temp session".
|
|
group_id: f.int({ min: 0 }).optional(),
|
|
auto_escape: f.bool().default(false),
|
|
},
|
|
run: async (p, ctx) => {
|
|
const tempGroupId = p.group_id && p.group_id > 0 ? p.group_id : undefined;
|
|
const result = await ctx.sendPrivateMessage(p.user_id, p.message, p.auto_escape, tempGroupId);
|
|
return okResponse({ message_id: result.messageId });
|
|
},
|
|
}),
|
|
|
|
groupAction({
|
|
name: 'send_group_msg',
|
|
summary: '发送群消息',
|
|
returns: '{ message_id: number }',
|
|
params: { message: f.message(), auto_escape: f.bool().default(false) },
|
|
run: async (p, ctx) => {
|
|
const result = await ctx.sendGroupMessage(p.group_id, p.message, p.auto_escape);
|
|
return okResponse({ message_id: result.messageId });
|
|
},
|
|
}),
|
|
|
|
defineAction({
|
|
name: 'get_msg',
|
|
summary: '获取消息',
|
|
readOnly: true,
|
|
returns: '消息事件对象(首次收到时存储的副本,已去除 post_type/self_id、附带 real_id 字段并刷新图片 URL)。',
|
|
params: { message_id: f.messageId() },
|
|
run: async (p, ctx) => {
|
|
const data = ctx.getMessage(p.message_id);
|
|
if (!data) {
|
|
log.warn('[get_msg] miss message_id=%d', p.message_id);
|
|
return failedResponse(RETCODE.ACTION_FAILED, 'message not found');
|
|
}
|
|
const result: JsonObject = { ...data };
|
|
delete result.post_type;
|
|
delete result.self_id;
|
|
result.real_id = (result.message_id ?? p.message_id) as JsonValue;
|
|
await refreshStoredImageUrls(result, ctx);
|
|
return okResponse(result);
|
|
},
|
|
}),
|
|
|
|
defineAction({
|
|
name: 'delete_msg',
|
|
summary: '撤回消息',
|
|
params: { message_id: f.messageId() },
|
|
run: async (p, ctx) => {
|
|
const meta = ctx.getMessageMeta(p.message_id);
|
|
if (!meta) return failedResponse(RETCODE.ACTION_FAILED, 'message not found or not retractable');
|
|
await ctx.deleteMessage(p.message_id, meta);
|
|
return okResponse();
|
|
},
|
|
}),
|
|
];
|
|
|