File size: 10,980 Bytes
80d4bc1 fa5fcba 3fc9ca0 80d4bc1 e336b84 80d4bc1 d32f568 d4f30f7 d32f568 59bee0f fa5fcba 4dda124 24503bb 14b96eb 858b76c 59bee0f d4f30f7 858b76c 59bee0f 858b76c 59bee0f 858b76c 14b96eb 858b76c 59bee0f fa5fcba 6c1e734 fa5fcba 6c1e734 fa5fcba e336b84 fa5fcba 6c1e734 59bee0f 6c1e734 e4aadd8 6c1e734 59bee0f 6c1e734 c46400f d32f568 fa5fcba d32f568 80d4bc1 fa5fcba 80d4bc1 e4aadd8 80d4bc1 3fc9ca0 80d4bc1 3fc9ca0 80d4bc1 3fc9ca0 80d4bc1 3fc9ca0 80d4bc1 3fc9ca0 80d4bc1 3fc9ca0 80d4bc1 3fc9ca0 80d4bc1 3fc9ca0 80d4bc1 3fc9ca0 80d4bc1 3fc9ca0 80d4bc1 3fc9ca0 80d4bc1 |
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
import { Markup } from "telegraf";
import { countryData } from "./country";
import { formatPrice } from "./priceUtils";
import { BotContext } from "../types/botTypes";
import { messageManager } from "./messageManager";
import fiveSimProducts from "./5sim_products.json";
import { VirtualNumberService } from "../services/VirtualNumberService";
import { createLogger } from "../../utils/logger";
const logger = createLogger('KeyboardUtils');
export const getMainMenuKeyboard = () => {
return Markup.inlineKeyboard([
[Markup.button.callback(messageManager.getMessage('btn_login'), 'login')],
[Markup.button.callback(messageManager.getMessage('btn_terms'), 'terms')],
[Markup.button.callback(messageManager.getMessage('btn_new_members'), 'new_members')],
// [Markup.button.callback(messageManager.getMessage('btn_stats'), 'stats')],
[Markup.button.callback(messageManager.getMessage('btn_change_language'), 'change_language')],
]);
};
export const getLoggedInMenuKeyboard = () => {
return Markup.inlineKeyboard([
[Markup.button.callback('π Browse Services', 'browse_services')],
[
Markup.button.callback(messageManager.getMessage('btn_profile'), 'profile'),
Markup.button.callback(messageManager.getMessage('btn_change_language'), 'change_language')
],
[
Markup.button.callback(messageManager.getMessage('btn_top_up'), 'top_up_balance'),
Markup.button.callback(messageManager.getMessage('btn_history'), 'history')
],
// [Markup.button.callback('π° Buy with Balance', 'buy_with_balance')],
[
Markup.button.callback(messageManager.getMessage('btn_back'), 'main_menu')
],
]);
};
export const getServicesKeyboard = (page: number = 0, sortBy?: 'az' | 'za') => {
const buttons = [];
const services = Object.entries(fiveSimProducts);
const rowSize = 2; // 2 buttons per row
const servicesPerPage = 20;
// Apply sorting for all services first
let sortedServices = [...services];
if (sortBy === 'az') {
sortedServices.sort((a, b) => a[1].label_en.localeCompare(b[1].label_en));
} else if (sortBy === 'za') {
sortedServices.sort((a, b) => b[1].label_en.localeCompare(a[1].label_en));
}
const totalPages = Math.ceil(sortedServices.length / servicesPerPage);
// Get services for current page from the sorted array
const startIndex = page * servicesPerPage;
const endIndex = Math.min(startIndex + servicesPerPage, sortedServices.length);
const pageServices = sortedServices.slice(startIndex, endIndex);
// Add sort button
const sortButtonText = sortBy === 'az' ? 'Sort Z-A β¬οΈ' : 'Sort A-Z β¬οΈ';
const sortButtonCallback = sortBy === 'az' ? `sort_services_za_${page}` : `sort_services_az_${page}`;
buttons.push([Markup.button.callback(sortButtonText, sortButtonCallback)]);
// Generate service buttons in pairs
for (let i = 0; i < pageServices.length; i += rowSize) {
const row = [];
for (let j = 0; j < rowSize && i + j < pageServices.length; j++) {
const [serviceId, serviceData] = pageServices[i + j];
row.push(
Markup.button.callback(
`${serviceData.icon} ${serviceData.label_en}`,
`service_${serviceId}`
)
);
}
buttons.push(row);
}
// Add pagination buttons
const paginationRow = [];
if (page > 0) {
paginationRow.push(
Markup.button.callback(
'β¬
οΈ Previous',
`services_page_${page - 1}${sortBy ? `_${sortBy}` : ''}`
)
);
}
paginationRow.push(
Markup.button.callback(
`π ${page + 1}/${totalPages}`,
'noop'
)
);
if (page < totalPages - 1) {
paginationRow.push(
Markup.button.callback(
'Next β‘οΈ',
`services_page_${page + 1}${sortBy ? `_${sortBy}` : ''}`
)
);
}
if (paginationRow.length > 0) {
buttons.push(paginationRow);
}
// Add search button
buttons.push([Markup.button.callback('π Search Product', 'search_product')]);
// Add back button to return to main menu
buttons.push([
Markup.button.callback('π Back to Menu', 'logged_in_menu')
]);
return Markup.inlineKeyboard(buttons);
};
export const getServicesPaginationInfo = (page: number = 0) => {
const services = Object.entries(fiveSimProducts);
const servicesPerPage = 10;
const totalPages = Math.ceil(services.length / servicesPerPage);
const startIndex = page * servicesPerPage;
const endIndex = Math.min(startIndex + servicesPerPage, services.length);
return `<b>Services List</b>\n` +
`π Showing services ${startIndex + 1}-${endIndex} of ${services.length}\n` +
`π Page ${page + 1} of ${totalPages}`;
};
export const getBackToMainMenuButton = () => {
return Markup.inlineKeyboard([
[Markup.button.callback(messageManager.getMessage('btn_back'), 'main_menu')]
]);
};
export const mapApiCountriesToButtons = (apiCountries: any[], service: string) => {
const buttons = [];
const rowSize = 2; // 2 buttons per row
// Sort countries by name for better UX
const sortedCountries = apiCountries.sort((a, b) => a.name.localeCompare(b.name));
for (let i = 0; i < sortedCountries.length; i += rowSize) {
const row = [];
for (let j = 0; j < rowSize && i + j < sortedCountries.length; j++) {
const country = sortedCountries[i + j];
const countryInfo = countryData[country.id.toLowerCase()];
if (countryInfo) {
row.push(
Markup.button.callback(
`${countryInfo.label} ${countryInfo.flag} ${countryInfo.code}`,
`country_${service}_${country.id}`
)
);
}
}
if (row.length > 0) {
buttons.push(row);
}
}
return buttons;
};
export const getCountriesKeyboard = async (service: string, page: number = 0) => {
const buttons = [];
const countriesPerPage = 15;
try {
// Get countries from API
const virtualNumberService = VirtualNumberService.getInstance();
const apiCountries = await virtualNumberService.getAvailableCountries(service);
const mappedButtons = mapApiCountriesToButtons(apiCountries, service);
const totalPages = Math.ceil(mappedButtons.length / countriesPerPage);
const startIndex = page * countriesPerPage;
const endIndex = Math.min(startIndex + countriesPerPage, mappedButtons.length);
const pageButtons = mappedButtons.slice(startIndex, endIndex);
buttons.push(...pageButtons);
const paginationRow = [];
if (page > 0) {
paginationRow.push(
Markup.button.callback(
messageManager.getMessage('btn_previous'),
`page_${service}_${page - 1}`
)
);
}
paginationRow.push(
Markup.button.callback(
messageManager.getMessage('btn_page_info')
.replace('{current}', (page + 1).toString())
.replace('{total}', totalPages.toString()),
'noop'
)
);
if (page < totalPages - 1) {
paginationRow.push(
Markup.button.callback(
messageManager.getMessage('btn_next'),
`page_${service}_${page + 1}`
)
);
}
if (paginationRow.length > 0) {
buttons.push(paginationRow);
}
buttons.push([
Markup.button.callback(
messageManager.getMessage('btn_main_menu'),
'main_menu'
)
]);
} catch (error: any) {
logger.error(`Error fetching countries: ${error.message}`);
buttons.push([
Markup.button.callback(
messageManager.getMessage('btn_error_loading_countries'),
'main_menu'
)
]);
}
return Markup.inlineKeyboard(buttons);
};
export const getServicePricesKeyboard = (
prices: any,
service: string,
country: string,
ctx: BotContext
) => {
const buttons = [];
const hasValidPrices = prices && Object.values(prices).some(
(priceInfo: any) => priceInfo && priceInfo.count > 0
);
if (!hasValidPrices) {
buttons.push([
Markup.button.callback(
messageManager.getMessage('btn_no_prices'),
'noop'
),
]);
} else {
Object.entries(prices).forEach(([operator, priceInfo]: [string, any]) => {
if (priceInfo.count > 0) {
buttons.push([
Markup.button.callback(
messageManager.getMessage('btn_buy_format')
.replace('{price}', priceInfo.cost.toFixed(2))
.replace('{count}', priceInfo.count.toString()),
`buy_${service}_${country}_${operator}`
),
]);
}
});
}
buttons.push([
Markup.button.callback(
messageManager.getMessage('btn_back_to_services'),
`service_${service}`
),
]);
buttons.push([
Markup.button.callback(
messageManager.getMessage('btn_main_menu'),
'main_menu'
)
]);
return Markup.inlineKeyboard(buttons);
};
export const getHistoryKeyboard = () => {
return Markup.inlineKeyboard([
[Markup.button.callback(
messageManager.getMessage('btn_numbers_history'),
'numbers_history'
)],
[Markup.button.callback(
messageManager.getMessage('btn_purchases_history'),
'purchases_history'
)],
[Markup.button.callback(
messageManager.getMessage('btn_back_to_main'),
'main_menu'
)]
]);
};
export const getLanguageSelectionKeyboard = (isLoggedIn: boolean = false) => {
const buttons = [
[
Markup.button.callback(
messageManager.getMessage('btn_lang_english'),
'set_language_en'
),
Markup.button.callback(
messageManager.getMessage('btn_lang_arabic'),
'set_language_ar'
)
]
];
buttons.push([
Markup.button.callback(
messageManager.getMessage('btn_back'),
isLoggedIn ? 'logged_in_menu' : 'main_menu'
)
]);
return Markup.inlineKeyboard(buttons);
};
export const getProfileKeyboard = () => {
return Markup.inlineKeyboard([
[Markup.button.callback(
messageManager.getMessage('btn_change_email'),
'change_email'
)],
[Markup.button.callback(
messageManager.getMessage('btn_change_password'),
'change_password'
)],
[Markup.button.callback(
messageManager.getMessage('btn_account_info'),
'account_info'
)],
// [Markup.button.callback(
// messageManager.getMessage('btn_gift_balance'),
// 'gift_balance'
// )],
[Markup.button.callback(
messageManager.getMessage('btn_back_to_logged_in'),
'logged_in_menu'
)]
]);
}; |