KOW-2 cleaner search and better codename search w/ fallback, export codename search
This commit is contained in:
parent
ea0ae1a47d
commit
0c364a1814
2 changed files with 29 additions and 20 deletions
|
@ -14,21 +14,18 @@ interface Device {
|
||||||
brand: string;
|
brand: string;
|
||||||
codename: string;
|
codename: string;
|
||||||
model: string;
|
model: string;
|
||||||
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getDeviceList({ Strings, ctx }: { Strings: any, ctx: Context & { message: { text: string } } }) {
|
export async function getDeviceByCodename(codename: string): Promise<Device | null> {
|
||||||
const reply_to_message_id = replyToMessageId(ctx);
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.get(Resources.codenameApi);
|
const response = await axios.get(Resources.codenameApi);
|
||||||
return response.data
|
const jsonRes = response.data;
|
||||||
|
const deviceDetails = jsonRes[codename];
|
||||||
|
if (!deviceDetails) return null;
|
||||||
|
return deviceDetails.find((item: Device) => item.brand) || deviceDetails[0];
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message = Strings.codenameCheck.apiErr
|
return null;
|
||||||
.replace('{error}', error.message);
|
|
||||||
|
|
||||||
return ctx.reply(message, {
|
|
||||||
parse_mode: "Markdown",
|
|
||||||
...({ reply_to_message_id })
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,18 +40,15 @@ export default (bot: Telegraf<Context>) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const jsonRes = await getDeviceList({ Strings, ctx })
|
const device = await getDeviceByCodename(userInput);
|
||||||
const phoneSearch = Object.keys(jsonRes).find((codename) => codename === userInput);
|
|
||||||
|
|
||||||
if (!phoneSearch) {
|
if (!device) {
|
||||||
return ctx.reply(Strings.codenameCheck.notFound, {
|
return ctx.reply(Strings.codenameCheck.notFound, {
|
||||||
parse_mode: "Markdown",
|
parse_mode: "Markdown",
|
||||||
...({ reply_to_message_id })
|
...({ reply_to_message_id })
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const deviceDetails = jsonRes[phoneSearch];
|
|
||||||
const device = deviceDetails.find((item: Device) => item.brand) || deviceDetails[0];
|
|
||||||
const message = Strings.codenameCheck.resultMsg
|
const message = Strings.codenameCheck.resultMsg
|
||||||
.replace('{brand}', device.brand)
|
.replace('{brand}', device.brand)
|
||||||
.replace('{codename}', userInput)
|
.replace('{codename}', userInput)
|
||||||
|
|
|
@ -8,6 +8,7 @@ import { isOnSpamWatch } from '../spamwatch/spamwatch';
|
||||||
import spamwatchMiddlewareModule from '../spamwatch/Middleware';
|
import spamwatchMiddlewareModule from '../spamwatch/Middleware';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { parse } from 'node-html-parser';
|
import { parse } from 'node-html-parser';
|
||||||
|
import { getDeviceByCodename } from './codename';
|
||||||
|
|
||||||
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
|
const spamwatchMiddleware = spamwatchMiddlewareModule(isOnSpamWatch);
|
||||||
|
|
||||||
|
@ -216,12 +217,27 @@ export default (bot) => {
|
||||||
return ctx.reply("Please provide the phone name.", { reply_to_message_id: ctx.message.message_id });
|
return ctx.reply("Please provide the phone name.", { reply_to_message_id: ctx.message.message_id });
|
||||||
}
|
}
|
||||||
|
|
||||||
const results = await searchPhone(phone);
|
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' });
|
||||||
|
|
||||||
|
let results = await searchPhone(phone);
|
||||||
if (results.length === 0) {
|
if (results.length === 0) {
|
||||||
return ctx.reply("No phones found.", { reply_to_message_id: ctx.message.message_id });
|
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' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await ctx.telegram.editMessageText(ctx.chat.id, statusMsg.message_id, undefined, `Searching for ${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' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
results = nameResults;
|
||||||
}
|
}
|
||||||
|
|
||||||
const testUser = `<a href="tg://user?id=${userId}">${userName}</a>, please select your device:`;
|
const testUser = `<a href=\"tg://user?id=${userId}\">${userName}</a>, please select your device:`;
|
||||||
const options = {
|
const options = {
|
||||||
parse_mode: 'HTML',
|
parse_mode: 'HTML',
|
||||||
reply_to_message_id: ctx.message.message_id,
|
reply_to_message_id: ctx.message.message_id,
|
||||||
|
@ -230,8 +246,7 @@ export default (bot) => {
|
||||||
inline_keyboard: results.map(result => [{ text: result.name, callback_data: `details:${result.url}:${ctx.from.id}` }])
|
inline_keyboard: results.map(result => [{ text: result.name, callback_data: `details:${result.url}:${ctx.from.id}` }])
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
ctx.reply(testUser, options);
|
await ctx.telegram.editMessageText(ctx.chat.id, statusMsg.message_id, undefined, testUser, options);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.action(/details:(.+):(.+)/, async (ctx) => {
|
bot.action(/details:(.+):(.+)/, async (ctx) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue