|
1 | 1 | // Telegram plugin module implements interactive dispatch behavior. |
2 | 2 | import { |
3 | | - createInteractiveConversationBindingHelpers, |
4 | | - dispatchPluginInteractiveHandler, |
| 3 | + createChannelInteractiveDispatcher, |
5 | 4 | type PluginConversationBinding, |
6 | 5 | type PluginConversationBindingRequestParams, |
7 | 6 | type PluginConversationBindingRequestResult, |
@@ -63,78 +62,28 @@ export type TelegramInteractiveHandlerRegistration = PluginInteractiveRegistrati |
63 | 62 | TelegramInteractiveHandlerResult |
64 | 63 | >; |
65 | 64 |
|
66 | | -type TelegramInteractiveDispatchContext = Omit< |
| 65 | +const dispatchTelegramInteractive = createChannelInteractiveDispatcher< |
| 66 | + "telegram", |
| 67 | + "callback", |
67 | 68 | TelegramInteractiveHandlerContext, |
68 | | - | "callback" |
69 | | - | "respond" |
70 | | - | "channel" |
71 | | - | "requestConversationBinding" |
72 | | - | "detachConversationBinding" |
73 | | - | "getCurrentConversationBinding" |
74 | | -> & { |
75 | | - callbackMessage: { |
76 | | - messageId: number; |
77 | | - chatId: string; |
78 | | - messageText?: string; |
79 | | - }; |
80 | | -}; |
| 69 | + TelegramInteractiveHandlerResult, |
| 70 | + "callbackMessage" |
| 71 | +>({ |
| 72 | + channel: "telegram", |
| 73 | + interactiveKey: "callback", |
| 74 | + dispatchInteractiveKey: "callbackMessage", |
| 75 | +}); |
81 | 76 |
|
82 | 77 | export async function dispatchTelegramPluginInteractiveHandler(params: { |
83 | 78 | data: string; |
84 | 79 | callbackId: string; |
85 | | - ctx: TelegramInteractiveDispatchContext; |
86 | | - respond: { |
87 | | - reply: (params: { text: string; buttons?: TelegramInteractiveButtons }) => Promise<void>; |
88 | | - editMessage: (params: { text: string; buttons?: TelegramInteractiveButtons }) => Promise<void>; |
89 | | - editButtons: (params: { buttons: TelegramInteractiveButtons }) => Promise<void>; |
90 | | - clearButtons: () => Promise<void>; |
91 | | - deleteMessage: () => Promise<void>; |
92 | | - }; |
| 80 | + ctx: Parameters<typeof dispatchTelegramInteractive>[0]["ctx"]; |
| 81 | + respond: TelegramInteractiveHandlerContext["respond"]; |
93 | 82 | onMatched?: () => Promise<void> | void; |
94 | 83 | afterInvoke?: (result: TelegramInteractiveHandlerResult) => Promise<void> | void; |
95 | 84 | }) { |
96 | | - return await dispatchPluginInteractiveHandler< |
97 | | - TelegramInteractiveHandlerRegistration, |
98 | | - TelegramInteractiveHandlerResult |
99 | | - >({ |
100 | | - channel: "telegram", |
101 | | - data: params.data, |
| 85 | + return await dispatchTelegramInteractive({ |
| 86 | + ...params, |
102 | 87 | dedupeId: params.callbackId, |
103 | | - onMatched: params.onMatched, |
104 | | - afterInvoke: params.afterInvoke, |
105 | | - invoke: ({ registration, namespace, payload }) => { |
106 | | - const { callbackMessage, ...handlerContext } = params.ctx; |
107 | | - return registration.handler({ |
108 | | - ...handlerContext, |
109 | | - channel: "telegram", |
110 | | - callback: { |
111 | | - data: params.data, |
112 | | - namespace, |
113 | | - payload, |
114 | | - messageId: callbackMessage.messageId, |
115 | | - chatId: callbackMessage.chatId, |
116 | | - messageText: callbackMessage.messageText, |
117 | | - }, |
118 | | - respond: params.respond, |
119 | | - ...createInteractiveConversationBindingHelpers({ |
120 | | - // Untrusted callbacks may reach their plugin, but never inherit conversation-binding authority. |
121 | | - registration: |
122 | | - handlerContext.auth?.isAuthorizedSender && |
123 | | - handlerContext.senderId?.trim() && |
124 | | - handlerContext.accountId?.trim() && |
125 | | - handlerContext.conversationId?.trim() |
126 | | - ? registration |
127 | | - : { ...registration, pluginRoot: undefined }, |
128 | | - senderId: handlerContext.senderId, |
129 | | - conversation: { |
130 | | - channel: "telegram", |
131 | | - accountId: handlerContext.accountId, |
132 | | - conversationId: handlerContext.conversationId, |
133 | | - parentConversationId: handlerContext.parentConversationId, |
134 | | - threadId: handlerContext.threadId, |
135 | | - }, |
136 | | - }), |
137 | | - }); |
138 | | - }, |
139 | 88 | }); |
140 | 89 | } |
0 commit comments