import { TelegramClient } from "telegram"; import { telegramClients, activeTasks } from "./models"; export function camelToSnake(obj: Record): Record { const snakeObj: Record = {}; for (const key in obj) { if (obj.hasOwnProperty(key)) { const snakeKey = key.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`); snakeObj[snakeKey] = obj[key]; } } return snakeObj; } export function snakeToCamel(obj: Record): Record { const camelObj: Record = {}; for (const key in obj) { if (obj.hasOwnProperty(key)) { const camelKey = key.replace(/_([a-z])/g, (_, match) => match.toUpperCase()); camelObj[camelKey] = obj[key]; } } return camelObj; } export function getCurrentDate() { const date = new Date() const day = date.getDate() const month = date.getMonth() + 1 const year = date.getFullYear() return `${year}-${month}-${day}` } export function isEmailValid(email: string): boolean { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(email); } export const sendResponse = (res: any, status: number, data: any) => { if (status === 200) { return res.status(200).json(data); } else { return res .status(status) .json({ error: data.error || "An error occurred" }); } }; export const getActiveClients = () => { return Array.from(telegramClients.entries()).map(([phone, info]) => ({ phoneNumber: phone, })); }; export const getActiveTasks = () => { return Array.from(activeTasks.entries()).map(([taskId, task]) => ({ taskId, phoneNumber: taskId.split("-")[0], taskType: task.taskType, taskData: task.taskData, // listeners: Array.from(task.listeners.keys()), })); }; export const sendMessage = async (client: TelegramClient, recipient: any, message: string) => { try { await client.sendMessage(recipient, { message }); } catch (error: any) { // Send an error message to the recipient if sending fails await client.sendMessage(recipient, { message: `Error sending message: ${error.message}` }); console.error("Error sending message:", error.message); } }; import * as unorm from 'unorm'; import { fetchDataFromTable } from "./db/supabaseHelper"; /** * Normalizes Arabic text by: * 1. Removing diacritics. * 2. Standardizing certain characters. * 3. Normalizing demonstrative pronouns. * * @param text - The input Arabic text to normalize. * @returns The normalized Arabic text. */ export function normalizeArabicText(text: string): string { // Remove diacritics using Unicode normalization (NFD) text = unorm.nfd(text).replace(/[\u064B-\u065F]/g, ''); // Normalize certain characters text = text .replace(/أ/g, 'ا') .replace(/إ/g, 'ا') .replace(/آ/g, 'ا') .replace(/ى/g, 'ي') .replace(/ة/g, 'ه'); // Standardize demonstrative pronouns text = text .replace(/هذة/g, 'هذا') .replace(/هذه/g, 'هذا') .replace(/هذي/g, 'هذا'); return text; } export async function getKeywords(): Promise { try { const filters ={ is_active:true } const keywordsResponse = await fetchDataFromTable("keyword", 1000, 0, filters); return keywordsResponse.data.map((keyword: any) => normalizeArabicText(keyword.text.toLowerCase()) ); } catch (error: any) { console.error("Error fetching keywords:", error.message); return []; } } /** * Fetches block keywords from the database and normalizes them. * @param userId - The user ID to filter block keywords by. * @returns A list of normalized block keywords. */ export async function getBlockKeywords(): Promise { try { const filters ={ is_active:true } const blockKeywordsResponse = await fetchDataFromTable("blockkeyword", 1000, 0, filters); return blockKeywordsResponse.data.map((keyword: any) => normalizeArabicText(keyword.text.toLowerCase()) ); } catch (error: any) { console.error("Error fetching block keywords:", error.message); return []; } } export const sendReplayTelegramMessage = async ( client: any, peer: any, message: string, replyToMsgId: number ) => { try { await client.sendMessage(peer, { message: message, replyTo: replyToMsgId, }); return true; } catch (error) { console.error('Error sending reply message:', error); return false; } };