Mohammed Foud commited on
Commit
905125c
·
1 Parent(s): cde7b17
Files changed (1) hide show
  1. src/bots/utils/handlerUtils.ts +15 -14
src/bots/utils/handlerUtils.ts CHANGED
@@ -52,24 +52,25 @@ export const messageReplyHandler = (
52
  * @param replyFunction Function that returns the message and options to send after answering callback
53
  * @returns A function that handles the callback with proper error handling
54
  */
55
- export const callbackReplyHandler = (handler: Function) => async (ctx: BotContext) => {
 
 
56
  try {
57
- // Await the handler. The handler is now responsible for sending/editing messages.
58
- await handler(ctx);
59
- // Always answer the callback query to remove the loading state on the button
60
  await ctx.answerCbQuery();
 
 
 
 
 
 
 
61
  } catch (error: any) {
 
62
  logger.error(`Error in callback reply handler: ${error.message}`);
63
- // Check if the error is due to message not being modified
64
- if (error.response && error.response.error_code === 400 && error.response.description.includes('message is not modified')) {
65
- // If the message is not modified, just answer the callback query and do nothing else.
66
- // This prevents sending an error message when the user clicks the same sort option twice.
67
- await ctx.answerCbQuery();
68
- } else {
69
- // For other errors, reply with a generic error message
70
- await ctx.reply('An unexpected error occurred. Please try again.');
71
- await ctx.answerCbQuery(); // Also answer in this case
72
- }
73
  }
74
  };
75
 
 
52
  * @param replyFunction Function that returns the message and options to send after answering callback
53
  * @returns A function that handles the callback with proper error handling
54
  */
55
+ export const callbackReplyHandler = (
56
+ replyFunction: (ctx: BotContext) => Promise<{message: string, options?: any}> | {message: string, options?: any}
57
+ ) => async (ctx: BotContext) => {
58
  try {
59
+ // Answer the callback query first
 
 
60
  await ctx.answerCbQuery();
61
+
62
+
63
+ // Execute the reply function to get message content and options
64
+ const result = await Promise.resolve(replyFunction(ctx));
65
+
66
+ // Send the reply with the message and any provided options
67
+ return await ctx.reply(result.message, result.options);
68
  } catch (error: any) {
69
+ // Log the error
70
  logger.error(`Error in callback reply handler: ${error.message}`);
71
+
72
+ // Send a generic error message to the user
73
+ return ctx.reply("⚠️ حدث خطأ أثناء معالجة طلبك. الرجاء المحاولة مرة أخرى.");
 
 
 
 
 
 
 
74
  }
75
  };
76