Spaces:
Paused
Paused
File size: 4,488 Bytes
959b027 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
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,
// 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<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 [];
}
}
/**
* 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<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;
}
};
|