|
|
|
|
|
|
|
|
import { BotContext } from "../types/botTypes"; |
|
|
import { createLogger } from '../../utils/logger'; |
|
|
import { AuthService } from '../services/auth'; |
|
|
import { messageManager } from './messageManager'; |
|
|
import { getMainMenuKeyboard } from './keyboardUtils'; |
|
|
|
|
|
const logger = createLogger('HandlerUtils'); |
|
|
const authService = AuthService.getInstance(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const messageReplyHandler = ( |
|
|
replyFunction: (ctx: BotContext) => Promise<{message: string, options?: any}> | {message: string, options?: any} |
|
|
) => async (ctx: BotContext) => { |
|
|
try { |
|
|
|
|
|
const result = await Promise.resolve(replyFunction(ctx)); |
|
|
|
|
|
|
|
|
const replyToMessageId = ctx.message?.message_id; |
|
|
|
|
|
|
|
|
const defaultOptions = { |
|
|
parse_mode: 'HTML' |
|
|
}; |
|
|
|
|
|
|
|
|
const finalOptions = { |
|
|
...defaultOptions, |
|
|
...result.options, |
|
|
reply_to_message_id: replyToMessageId |
|
|
}; |
|
|
|
|
|
|
|
|
return await ctx.reply(result.message, finalOptions); |
|
|
} catch (error: any) { |
|
|
|
|
|
logger.error(`Error in message reply handler: ${error.message}`); |
|
|
|
|
|
|
|
|
return ctx.reply("⚠️ حدث خطأ أثناء معالجة طلبك. الرجاء المحاولة مرة أخرى."); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const callbackReplyHandler = ( |
|
|
replyFunction: (ctx: BotContext) => Promise<{message: string, options?: any}> | {message: string, options?: any} |
|
|
) => async (ctx: BotContext) => { |
|
|
try { |
|
|
|
|
|
await ctx.answerCbQuery(); |
|
|
|
|
|
|
|
|
|
|
|
const result = await Promise.resolve(replyFunction(ctx)); |
|
|
|
|
|
|
|
|
return await ctx.reply(result.message, result.options); |
|
|
} catch (error: any) { |
|
|
|
|
|
logger.error(`Error in callback reply handler: ${error.message}`); |
|
|
|
|
|
|
|
|
return ctx.reply("⚠️ حدث خطأ أثناء معالجة طلبك. الرجاء المحاولة مرة أخرى."); |
|
|
} |
|
|
}; |
|
|
|
|
|
export const authCheckHandler = <T extends any[]>( |
|
|
handler: (ctx: BotContext, ...args: T) => Promise<{message: string, options?: any}> | {message: string, options?: any} |
|
|
) => async (ctx: BotContext, ...args: T) => { |
|
|
const telegramId = ctx.from?.id; |
|
|
|
|
|
if (!telegramId || !authService.isUserLoggedIn(telegramId, ctx)) { |
|
|
return { |
|
|
message: messageManager.getMessage('auth_required'), |
|
|
options: getMainMenuKeyboard() |
|
|
}; |
|
|
} |
|
|
|
|
|
return handler(ctx, ...args); |
|
|
}; |
|
|
|