import { createInteractiveConversationBindingHelpers, dispatchPluginInteractiveHandler, type PluginConversationBinding, type PluginConversationBindingRequestParams, type PluginConversationBindingRequestResult, type PluginInteractiveRegistration, } from "openclaw/plugin-sdk/plugin-runtime"; export type TelegramInteractiveButtons = Array< Array<{ text: string; callback_data: string; style?: "danger" | "success" | "primary" }> >; export type TelegramInteractiveHandlerContext = { channel: "telegram"; accountId: string; callbackId: string; conversationId: string; parentConversationId?: string; senderId?: string; senderUsername?: string; threadId?: number; isGroup: boolean; isForum: boolean; auth: { isAuthorizedSender: boolean; }; callback: { data: string; namespace: string; payload: string; messageId: number; chatId: string; messageText?: string; }; respond: { reply: (params: { text: string; buttons?: TelegramInteractiveButtons }) => Promise; editMessage: (params: { text: string; buttons?: TelegramInteractiveButtons }) => Promise; editButtons: (params: { buttons: TelegramInteractiveButtons }) => Promise; clearButtons: () => Promise; deleteMessage: () => Promise; }; requestConversationBinding: ( params?: PluginConversationBindingRequestParams, ) => Promise; detachConversationBinding: () => Promise<{ removed: boolean }>; getCurrentConversationBinding: () => Promise; }; export type TelegramInteractiveHandlerRegistration = PluginInteractiveRegistration< TelegramInteractiveHandlerContext, "telegram" >; export type TelegramInteractiveDispatchContext = Omit< TelegramInteractiveHandlerContext, | "callback" | "respond" | "channel" | "requestConversationBinding" | "detachConversationBinding" | "getCurrentConversationBinding" > & { callbackMessage: { messageId: number; chatId: string; messageText?: string; }; }; export async function dispatchTelegramPluginInteractiveHandler(params: { data: string; callbackId: string; ctx: TelegramInteractiveDispatchContext; respond: { reply: (params: { text: string; buttons?: TelegramInteractiveButtons }) => Promise; editMessage: (params: { text: string; buttons?: TelegramInteractiveButtons }) => Promise; editButtons: (params: { buttons: TelegramInteractiveButtons }) => Promise; clearButtons: () => Promise; deleteMessage: () => Promise; }; onMatched?: () => Promise | void; }) { return await dispatchPluginInteractiveHandler({ channel: "telegram", data: params.data, dedupeId: params.callbackId, onMatched: params.onMatched, invoke: ({ registration, namespace, payload }) => { const { callbackMessage, ...handlerContext } = params.ctx; return registration.handler({ ...handlerContext, channel: "telegram", callback: { data: params.data, namespace, payload, messageId: callbackMessage.messageId, chatId: callbackMessage.chatId, messageText: callbackMessage.messageText, }, respond: params.respond, ...createInteractiveConversationBindingHelpers({ registration, senderId: handlerContext.senderId, conversation: { channel: "telegram", accountId: handlerContext.accountId, conversationId: handlerContext.conversationId, parentConversationId: handlerContext.parentConversationId, threadId: handlerContext.threadId, }, }), }); }, }); }