mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 06:52:44 +00:00
Handle Telegram poll vote updates for agent context
This commit is contained in:
@@ -50,6 +50,7 @@ import {
|
||||
parseModelCallbackData,
|
||||
type ProviderInfo,
|
||||
} from "./model-buttons.js";
|
||||
import { getSentPoll } from "./poll-vote-cache.js";
|
||||
import { buildInlineKeyboard } from "./send.js";
|
||||
|
||||
export const registerTelegramHandlers = ({
|
||||
@@ -749,6 +750,65 @@ export const registerTelegramHandlers = ({
|
||||
}
|
||||
});
|
||||
|
||||
bot.on("poll_answer", async (ctx) => {
|
||||
try {
|
||||
if (shouldSkipUpdate(ctx)) {
|
||||
return;
|
||||
}
|
||||
const pollAnswer = (ctx.update as { poll_answer?: unknown })?.poll_answer as
|
||||
| {
|
||||
poll_id?: string;
|
||||
user?: { id?: number; username?: string; first_name?: string };
|
||||
option_ids?: number[];
|
||||
}
|
||||
| undefined;
|
||||
if (!pollAnswer) {
|
||||
return;
|
||||
}
|
||||
const pollId = pollAnswer?.poll_id?.trim();
|
||||
if (!pollId) {
|
||||
return;
|
||||
}
|
||||
const pollMeta = getSentPoll(pollId);
|
||||
if (!pollMeta) {
|
||||
return;
|
||||
}
|
||||
if (pollMeta.accountId && pollMeta.accountId !== accountId) {
|
||||
return;
|
||||
}
|
||||
const userId = pollAnswer.user?.id;
|
||||
if (typeof userId !== "number") {
|
||||
return;
|
||||
}
|
||||
const optionIds = Array.isArray(pollAnswer.option_ids) ? pollAnswer.option_ids : [];
|
||||
const selected = optionIds.map((id) => pollMeta.options[id] ?? `option#${id + 1}`);
|
||||
const selectedText = selected.length > 0 ? selected.join(", ") : "(cleared vote)";
|
||||
const syntheticText = `Poll vote update: "${pollMeta.question}" -> ${selectedText}`;
|
||||
const syntheticMessage = {
|
||||
message_id: Date.now(),
|
||||
date: Math.floor(Date.now() / 1000),
|
||||
chat: {
|
||||
id: Number(pollMeta.chatId),
|
||||
type: String(pollMeta.chatId).startsWith("-") ? "supergroup" : "private",
|
||||
},
|
||||
from: {
|
||||
id: userId,
|
||||
is_bot: false,
|
||||
first_name: pollAnswer.user?.first_name ?? "User",
|
||||
username: pollAnswer.user?.username,
|
||||
},
|
||||
text: syntheticText,
|
||||
} as unknown as Message;
|
||||
const storeAllowFrom = await loadStoreAllowFrom();
|
||||
await processMessage(buildSyntheticContext(ctx, syntheticMessage), [], storeAllowFrom, {
|
||||
forceWasMentioned: true,
|
||||
messageIdOverride: `poll:${pollId}:${userId}:${Date.now()}`,
|
||||
});
|
||||
} catch (err) {
|
||||
runtime.error?.(danger(`poll_answer handler failed: ${String(err)}`));
|
||||
}
|
||||
});
|
||||
|
||||
// Handle group migration to supergroup (chat ID changes)
|
||||
bot.on("message:migrate_to_chat_id", async (ctx) => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user