|
|
import { TelegramClient } from "telegram"; |
|
|
import { telegramClients, activeTasks } from "./models"; |
|
|
|
|
|
export function camelToSnake(obj: Record<string, any>): Record<string, any> { |
|
|
const snakeObj: Record<string, any> = {}; |
|
|
|
|
|
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<string, any>): Record<string, any> { |
|
|
const camelObj: Record<string, any> = {}; |
|
|
|
|
|
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, |
|
|
|
|
|
})); |
|
|
}; |
|
|
|
|
|
|
|
|
export const sendMessage = async (client: TelegramClient, recipient: any, message: string) => { |
|
|
try { |
|
|
await client.sendMessage(recipient, { message }); |
|
|
} catch (error: any) { |
|
|
|
|
|
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"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function normalizeArabicText(text: string): string { |
|
|
|
|
|
text = unorm.nfd(text).replace(/[\u064B-\u065F]/g, ''); |
|
|
|
|
|
|
|
|
text = text |
|
|
.replace(/أ/g, 'ا') |
|
|
.replace(/إ/g, 'ا') |
|
|
.replace(/آ/g, 'ا') |
|
|
.replace(/ى/g, 'ي') |
|
|
.replace(/ة/g, 'ه'); |
|
|
|
|
|
|
|
|
text = text |
|
|
.replace(/هذة/g, 'هذا') |
|
|
.replace(/هذه/g, 'هذا') |
|
|
.replace(/هذي/g, 'هذا'); |
|
|
|
|
|
return text; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function getKeywords(): Promise<string[]> { |
|
|
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 []; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function getBlockKeywords(): Promise<string[]> { |
|
|
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; |
|
|
} |
|
|
}; |
|
|
|