add postgres db, use settings and user data, lots of cleanup and logic fixes, bug fixes, better error handling, update docs and docker
Some checks are pending
njsscan sarif / njsscan code scanning (push) Waiting to run
Update AUTHORS File / update-authors (push) Waiting to run
Some checks are pending
njsscan sarif / njsscan code scanning (push) Waiting to run
Update AUTHORS File / update-authors (push) Waiting to run
This commit is contained in:
parent
765b1144fa
commit
4d540078f5
30 changed files with 1664 additions and 727 deletions
|
@ -9,6 +9,8 @@ import spamwatchMiddlewareModule from '../spamwatch/Middleware';
|
|||
import axios from 'axios';
|
||||
import { parse } from 'node-html-parser';
|
||||
import { getDeviceByCodename } from './codename';
|
||||
import { getStrings } from '../plugins/checklang';
|
||||
import { languageCode } from '../utils/language-code';
|
||||
|
||||
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
|
||||
|
||||
|
@ -207,68 +209,130 @@ function getUsername(ctx){
|
|||
return userName;
|
||||
}
|
||||
|
||||
const deviceSelectionCache: Record<number, { results: PhoneSearchResult[], timeout: NodeJS.Timeout }> = {};
|
||||
const lastSelectionMessageId: Record<number, number> = {};
|
||||
|
||||
export default (bot) => {
|
||||
bot.command(['d', 'device'], spamwatchMiddleware, async (ctx) => {
|
||||
const userId = ctx.from.id;
|
||||
const userName = getUsername(ctx);
|
||||
const Strings = getStrings(languageCode(ctx));
|
||||
|
||||
const phone = ctx.message.text.split(" ").slice(1).join(" ");
|
||||
if (!phone) {
|
||||
return ctx.reply("Please provide the phone name.", { reply_to_message_id: ctx.message.message_id });
|
||||
return ctx.reply(Strings.gsmarenaProvidePhoneName || "[TODO: Add gsmarenaProvidePhoneName to locales] Please provide the phone name.", { ...(ctx.message?.message_id ? { reply_parameters: { message_id: ctx.message.message_id } } : {}) });
|
||||
}
|
||||
|
||||
console.log("[GSMArena] Searching for", phone);
|
||||
const statusMsg = await ctx.reply(`Searching for \`${phone}\`...`, { reply_to_message_id: ctx.message.message_id, parse_mode: 'Markdown' });
|
||||
const statusMsg = await ctx.reply((Strings.gsmarenaSearchingFor || "[TODO: Add gsmarenaSearchingFor to locales] Searching for {phone}...").replace('{phone}', phone), { ...(ctx.message?.message_id ? { reply_parameters: { message_id: ctx.message.message_id } } : {}), parse_mode: 'Markdown' });
|
||||
|
||||
let results = await searchPhone(phone);
|
||||
if (results.length === 0) {
|
||||
const codenameResults = await getDeviceByCodename(phone.split(" ")[0]);
|
||||
if (!codenameResults) {
|
||||
await ctx.telegram.editMessageText(ctx.chat.id, statusMsg.message_id, undefined, `No phones found for \`${phone}\`.`, { parse_mode: 'Markdown' });
|
||||
await ctx.telegram.editMessageText(ctx.chat.id, statusMsg.message_id, undefined, (Strings.gsmarenaNoPhonesFound || "[TODO: Add gsmarenaNoPhonesFound to locales] No phones found for {phone}.").replace('{phone}', phone), { parse_mode: 'Markdown' });
|
||||
return;
|
||||
}
|
||||
|
||||
await ctx.telegram.editMessageText(ctx.chat.id, statusMsg.message_id, undefined, `Searching for ${codenameResults.name}...`, { parse_mode: 'Markdown' });
|
||||
await ctx.telegram.editMessageText(ctx.chat.id, statusMsg.message_id, undefined, (Strings.gsmarenaSearchingFor || "[TODO: Add gsmarenaSearchingFor to locales] Searching for {phone}...").replace('{phone}', codenameResults.name), { parse_mode: 'Markdown' });
|
||||
const nameResults = await searchPhone(codenameResults.name);
|
||||
if (nameResults.length === 0) {
|
||||
await ctx.telegram.editMessageText(ctx.chat.id, statusMsg.message_id, undefined, `No phones found for \`${codenameResults.name}\` and \`${phone}\`.`, { parse_mode: 'Markdown' });
|
||||
await ctx.telegram.editMessageText(ctx.chat.id, statusMsg.message_id, undefined, (Strings.gsmarenaNoPhonesFoundBoth || "[TODO: Add gsmarenaNoPhonesFoundBoth to locales] No phones found for {name} and {phone}.").replace('{name}', codenameResults.name).replace('{phone}', phone), { parse_mode: 'Markdown' });
|
||||
return;
|
||||
}
|
||||
results = nameResults;
|
||||
}
|
||||
|
||||
const testUser = `<a href=\"tg://user?id=${userId}\">${userName}</a>, please select your device:`;
|
||||
const options = {
|
||||
parse_mode: 'HTML',
|
||||
reply_to_message_id: ctx.message.message_id,
|
||||
disable_web_page_preview: true,
|
||||
reply_markup: {
|
||||
inline_keyboard: results.map(result => [{ text: result.name, callback_data: `details:${result.url}:${ctx.from.id}` }])
|
||||
}
|
||||
if (deviceSelectionCache[userId]?.timeout) {
|
||||
clearTimeout(deviceSelectionCache[userId].timeout);
|
||||
}
|
||||
deviceSelectionCache[userId] = {
|
||||
results,
|
||||
timeout: setTimeout(() => { delete deviceSelectionCache[userId]; }, 5 * 60 * 1000)
|
||||
};
|
||||
await ctx.telegram.editMessageText(ctx.chat.id, statusMsg.message_id, undefined, testUser, options);
|
||||
|
||||
if (lastSelectionMessageId[userId]) {
|
||||
try {
|
||||
await ctx.telegram.editMessageText(
|
||||
ctx.chat.id,
|
||||
lastSelectionMessageId[userId],
|
||||
undefined,
|
||||
Strings.gsmarenaSelectDevice || "[TODO: Add gsmarenaSelectDevice to locales] Please select your device:",
|
||||
{
|
||||
parse_mode: 'HTML',
|
||||
reply_to_message_id: ctx.message.message_id,
|
||||
disable_web_page_preview: true,
|
||||
reply_markup: {
|
||||
inline_keyboard: results.map((result, idx) => {
|
||||
const callbackData = `gsmadetails:${idx}:${ctx.from.id}`;
|
||||
return [{ text: result.name, callback_data: callbackData }];
|
||||
})
|
||||
}
|
||||
}
|
||||
);
|
||||
} catch (e) {
|
||||
const testUser = `<a href=\"tg://user?id=${userId}\">${userName}</a>, ${Strings.gsmarenaSelectDevice || "[TODO: Add gsmarenaSelectDevice to locales] please select your device:"}`;
|
||||
const options = {
|
||||
parse_mode: 'HTML',
|
||||
reply_to_message_id: ctx.message.message_id,
|
||||
disable_web_page_preview: true,
|
||||
reply_markup: {
|
||||
inline_keyboard: results.map((result, idx) => {
|
||||
const callbackData = `gsmadetails:${idx}:${ctx.from.id}`;
|
||||
return [{ text: result.name, callback_data: callbackData }];
|
||||
})
|
||||
}
|
||||
};
|
||||
const selectionMsg = await ctx.reply(testUser, options);
|
||||
lastSelectionMessageId[userId] = selectionMsg.message_id;
|
||||
}
|
||||
} else {
|
||||
const testUser = `<a href=\"tg://user?id=${userId}\">${userName}</a>, ${Strings.gsmarenaSelectDevice || "[TODO: Add gsmarenaSelectDevice to locales] please select your device:"}`;
|
||||
const inlineKeyboard = results.map((result, idx) => {
|
||||
const callbackData = `gsmadetails:${idx}:${ctx.from.id}`;
|
||||
return [{ text: result.name, callback_data: callbackData }];
|
||||
});
|
||||
const options = {
|
||||
parse_mode: 'HTML',
|
||||
reply_to_message_id: ctx.message.message_id,
|
||||
disable_web_page_preview: true,
|
||||
reply_markup: {
|
||||
inline_keyboard: inlineKeyboard
|
||||
}
|
||||
};
|
||||
const selectionMsg = await ctx.reply(testUser, options);
|
||||
lastSelectionMessageId[userId] = selectionMsg.message_id;
|
||||
}
|
||||
await ctx.telegram.deleteMessage(ctx.chat.id, statusMsg.message_id).catch(() => {});
|
||||
});
|
||||
|
||||
bot.action(/details:(.+):(.+)/, async (ctx) => {
|
||||
const url = ctx.match[1];
|
||||
bot.action(/gsmadetails:(\d+):(\d+)/, async (ctx) => {
|
||||
const idx = parseInt(ctx.match[1]);
|
||||
const userId = parseInt(ctx.match[2]);
|
||||
const userName = getUsername(ctx);
|
||||
const Strings = getStrings(languageCode(ctx));
|
||||
|
||||
const callbackQueryUserId = ctx.update.callback_query.from.id;
|
||||
|
||||
if (userId !== callbackQueryUserId) {
|
||||
return ctx.answerCbQuery(`${userName}, you are not allowed to interact with this.`);
|
||||
return ctx.answerCbQuery(`${userName}, ${Strings.gsmarenaNotAllowed || "[TODO: Add gsmarenaNotAllowed to locales] you are not allowed to interact with this."}`);
|
||||
}
|
||||
|
||||
ctx.answerCbQuery();
|
||||
|
||||
const cache = deviceSelectionCache[userId];
|
||||
if (!cache || !cache.results[idx]) {
|
||||
return ctx.reply(Strings.gsmarenaInvalidOrExpired || "[TODO: Add gsmarenaInvalidOrExpired to locales] Whoops, invalid or expired option. Please try again.", { ...(ctx.message?.message_id ? { reply_parameters: { message_id: ctx.message.message_id } } : {}) });
|
||||
}
|
||||
const url = cache.results[idx].url;
|
||||
|
||||
const phoneDetails = await checkPhoneDetails(url);
|
||||
|
||||
if (phoneDetails.name) {
|
||||
const message = formatPhone(phoneDetails);
|
||||
ctx.editMessageText(`<b><a href="tg://user?id=${userId}">${userName}</a>, these are the details of your device:</b>` + message, { parse_mode: 'HTML', disable_web_page_preview: false });
|
||||
ctx.editMessageText(`<b><a href=\"tg://user?id=${userId}\">${userName}</a>, ${Strings.gsmarenaDeviceDetails || "[TODO: Add gsmarenaDeviceDetails to locales] these are the details of your device:"}</b>` + message, { parse_mode: 'HTML', disable_web_page_preview: false });
|
||||
} else {
|
||||
ctx.reply("Error fetching phone details.", { reply_to_message_id: ctx.message.message_id });
|
||||
ctx.reply(Strings.gsmarenaErrorFetchingDetails || "[TODO: Add gsmarenaErrorFetchingDetails to locales] Error fetching phone details.", { ...(ctx.message?.message_id ? { reply_parameters: { message_id: ctx.message.message_id } } : {}) });
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue