You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Safely send a message with error handling for blocked users and other common errors
53
-
*
54
-
* @param bot - The Telegraf bot instance
55
-
* @param chatId - The chat ID to send the message to
56
-
* @param text - The message text
57
-
* @param options - Additional options for sendMessage
58
-
* @returns The sent message object or null if failed
52
+
* Sends a message to a specified chat, handling common Telegram errors such as blocked users, chat not found, and rate limits.
53
+
*
54
+
* Attempts to send a message and performs cleanup if the bot is blocked or the chat does not exist. Returns null if sending fails due to these conditions or rate limiting; otherwise, returns the sent message object.
55
+
*
56
+
* @param chatId - The target chat ID
57
+
* @param text - The message text to send
58
+
* @param options - Optional parameters for message formatting and behavior
59
+
* @returns The sent message object, or null if the message could not be delivered due to blocking, chat not found, or rate limiting
59
60
*/
60
61
exportasyncfunctionsafeSendMessage(
61
62
bot: Telegraf<BotContext>,
@@ -106,12 +107,14 @@ export async function safeSendMessage(
106
107
}
107
108
108
109
/**
109
-
* Safely reply to a message with cleanup handling for blocked users
110
-
*
111
-
* @param ctx - The Telegraf context object
112
-
* @param text - The message text to reply with
113
-
* @param options - Additional options for the reply
114
-
* @returns The sent message object or null if failed
110
+
* Replies to a message in the given context, handling errors such as blocked users, missing chats, and rate limits.
111
+
*
112
+
* If the bot is blocked or the chat is not found, associated user data is cleaned up and null is returned. If rate limits are exceeded, a warning is logged and null is returned. Other errors are logged and re-thrown.
113
+
*
114
+
* @param ctx - The Telegraf context for the incoming message
115
+
* @param text - The reply message text
116
+
* @param options - Optional parameters for the reply
117
+
* @returns The sent message object, or null if the reply could not be sent due to blocking, missing chat, or rate limiting
115
118
*/
116
119
exportasyncfunctionsafeReply(
117
120
ctx: BotContext,
@@ -170,15 +173,17 @@ export async function safeReply(
170
173
}
171
174
172
175
/**
173
-
* Safely edit a message text with cleanup handling for blocked users
174
-
*
176
+
* Attempts to edit the text of a Telegram message, handling common errors such as blocked users, missing chats, rate limits, and non-critical edit failures.
177
+
*
178
+
* If the bot is blocked or the chat is not found, associated user data is cleaned up and `null` is returned. Rate limit errors also result in `null`. If the message is not found or already modified, the error is logged and `null` is returned. Other errors are logged and re-thrown.
179
+
*
175
180
* @param ctx - The Telegraf context object
176
-
* @param chatId - The chat ID
177
-
* @param messageId - The message ID to edit
178
-
* @param inlineMessageId - Inline message ID (if applicable)
179
-
* @param text - The new message text
180
-
* @param options - Additional options for editing
181
-
* @returns The edited message object or null if failed
181
+
* @param chatId - The target chat ID
182
+
* @param messageId - The ID of the message to edit
183
+
* @param inlineMessageId - The inline message ID, if applicable
184
+
* @param text - The new text for the message
185
+
* @param options - Additional options for editing the message
186
+
* @returns The edited message object, or `null` if the operation fails due to handled errors
182
187
*/
183
188
exportasyncfunctionsafeEditMessageText(
184
189
ctx: BotContext,
@@ -248,10 +253,11 @@ export async function safeEditMessageText(
248
253
}
249
254
250
255
/**
251
-
* Clean up user data when bot is blocked or chat is not found
252
-
* This implements the fix from GitHub issue telegraf/telegraf#1513
253
-
*
254
-
* @param chatId - The chat ID of the blocked user
256
+
* Cleans up all local data associated with a chat when the bot is blocked or the chat is not found.
257
+
*
258
+
* Removes tickets and customer mappings related to the specified chat from persistent storage. User state data is not explicitly removed but will expire automatically. Errors during cleanup are logged but do not interrupt bot operation.
259
+
*
260
+
* @param chatId - The Telegram chat ID to clean up data for
* Handles the email field input and completes the ticket process
523
+
* Processes the email input step of the support ticket conversation and completes ticket creation.
524
+
*
525
+
* If the user enters "skip", an auto-generated email is used. The function then finalizes the ticket by interacting with external services to create the customer, user, and ticket records, updates the user with confirmation or error messages, and clears the user's conversation state.
* This function routes messages to appropriate handlers based on chat type
33
-
* and processes text messages against registered patterns
34
+
* Main handler for incoming Telegram messages, routing them to appropriate processors based on chat type and message context.
35
+
*
36
+
* Determines whether to process the message as a command, support conversation, ticket reply, private chat, or group chat, and delegates handling accordingly. Prevents automatic responses in group chats and ensures that only relevant handlers are invoked for each message type.
* Processes replies to ticket confirmation or agent messages and routes them for handling.
107
+
*
108
+
* Checks if the incoming message is a reply to a ticket confirmation or agent message, and if so, processes the reply accordingly. Returns true if the reply was handled, or false otherwise.
109
+
*
110
+
* @returns True if the reply was processed as a ticket or agent message reply; false otherwise.
@@ -157,7 +164,11 @@ async function handleTicketReply(ctx: BotContext): Promise<boolean> {
157
164
}
158
165
159
166
/**
160
-
* Handles replies to ticket confirmation messages
167
+
* Processes a reply to a ticket confirmation message by validating the reply, sending the message to the ticket conversation, and updating the user with a status message.
168
+
*
169
+
* @param ctx - The Telegram bot context for the incoming message
170
+
* @param ticketInfo - Information about the ticket to which the reply is associated
171
+
* @returns True if the reply was processed (successfully or with an error status message), or false if validation failed or an unexpected error occurred
* Processes and sends the ticket message to Unthread
259
+
* Sends a user's message to the specified ticket conversation in Unthread.
260
+
*
261
+
* Retrieves or creates user data based on the Telegram user ID and username, then sends the provided message to the ticket conversation identified by the ticket information.
@@ -272,7 +287,9 @@ async function processTicketMessage(ticketInfo: any, telegramUserId: number, use
272
287
}
273
288
274
289
/**
275
-
* Updates the status message and handles cleanup
290
+
* Updates a status message to indicate success or error, then deletes it after a short delay.
291
+
*
292
+
* The message is updated to show a checkmark for success or an error icon for failure, and is automatically removed after 3 seconds (success) or 5 seconds (error).
@@ -307,7 +324,11 @@ async function updateStatusMessage(ctx: BotContext, statusMsg: any, isSuccess: b
307
324
}
308
325
309
326
/**
310
-
* Handles replies to agent messages
327
+
* Processes a user's reply to an agent message by forwarding it to the corresponding Unthread conversation.
328
+
*
329
+
* Sends a status message indicating progress, updates it upon success or error, and deletes the status message after a delay. Returns true if the reply was processed, false otherwise.
330
+
*
331
+
* @returns True if the reply was handled (successfully sent or error occurred), false if the context was invalid.
* Executes an asynchronous operation with retries and exponential backoff on failure.
197
+
*
198
+
* Retries the provided async operation up to a specified number of times, increasing the delay between attempts exponentially up to a maximum delay. Logs warnings on each retry and an error if all attempts fail.
199
+
*
200
+
* @param operation - The asynchronous function to execute and retry on failure
201
+
* @param maxRetries - Maximum number of retry attempts (default: 5)
202
+
* @param initialDelayMs - Initial delay in milliseconds before the first retry (default: 1000)
203
+
* @param maxDelayMs - Maximum delay in milliseconds between retries (default: 30000)
204
+
* @param operationName - Name used in log messages to identify the operation (default: 'operation')
205
+
* @returns The result of the successful operation
206
+
* @throws The last encountered error if all retries fail
* Clean up user data when bot is blocked or chat is not found
425
-
* This implements the fix from GitHub issue telegraf/telegraf#1513
426
-
* Global version for use in error handlers
427
-
*
428
-
* @param chatId - The chat ID of the blocked user
434
+
* Removes all tickets and customer mappings associated with a Telegram chat when the bot is blocked or the chat is not found.
435
+
*
436
+
* Cleans up related tickets and customer data in storage for the specified chat ID. User state data is not explicitly removed but will expire automatically.
437
+
*
438
+
* @param chatId - The Telegram chat ID to clean up.
@@ -485,9 +495,9 @@ async function cleanupBlockedUserGlobal(chatId: number): Promise<void> {
485
495
}
486
496
487
497
/**
488
-
* Performs graceful shutdown of all services
489
-
*
490
-
* Properly close database connections, stop webhook consumer, and stop the bot
498
+
* Shuts down all services and resources used by the bot, ensuring a clean exit.
499
+
*
500
+
* Stops the webhook consumer if running, shuts down the BotsStore, closes database connections, and exits the process. Logs each shutdown step and exits with an error code if any shutdown operation fails.
0 commit comments