Skip to content

Commit

Permalink
Merge pull request #5 from grammyjs/parse
Browse files Browse the repository at this point in the history
  • Loading branch information
KnorpelSenf authored Jun 1, 2022
2 parents bd6d14e + a0490a6 commit 41124df
Showing 1 changed file with 64 additions and 3 deletions.
67 changes: 64 additions & 3 deletions bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,27 @@ async function search(query: string) {
return await res.json();
}

const replacementExp = /\+([^+]+(?:\|[^+]|))\+/g;

bot.on("inline_query", async (ctx) => {
const query = ctx.inlineQuery.query;
const { hits } = await search(query);
const results = new Array<InlineQueryResultArticle>();
if (query.match(replacementExp)) {
const [message_text, entities, description] = await makeReplacements(query);
results.push(
{
id: crypto.randomUUID(),
type: "article",
title: "Make replacements",
description,
input_message_content: { message_text, entities },
},
);
}
const { hits } = await search(whatToSearch(query));
hits.length = Math.min(50, hits.length);
await ctx.answerInlineQuery(
hits.map((h: any): InlineQueryResultArticle => {
results.concat(hits.map((h: any): InlineQueryResultArticle => {
const { title, iv, url } = getText(h, !h.hierarchy.lvl2);
const message_text = `${title}${ZWSP}\n\n${url}`;
const entities: MessageEntity[] = [
Expand All @@ -67,7 +82,7 @@ bot.on("inline_query", async (ctx) => {
}`,
input_message_content: { message_text, entities },
};
}),
})),
{ cache_time: 24 * 60 * 60 }, // 24 hours (algolia re-indexing)
);
});
Expand Down Expand Up @@ -96,3 +111,49 @@ function stripAnchor(url: string) {
const index = url.lastIndexOf("#");
return index > 0 ? url.substring(0, index) : url;
}

function whatToSearch(query: string): string {
const match = query.match(/\+([^\+]+)$/);
if (match) {
return match[1];
}
return query;
}
async function makeReplacements(
text: string,
): Promise<[string, MessageEntity[], string]> {
const searchQueries = new Array<string>();
text.replace(replacementExp, (_, s, o) => {
s = s.split("|");
searchQueries.push(s[0]);
return s;
});
const urls = new Array<string>();
for (const query of searchQueries) {
urls.push((await search(query)).hits[0].url);
}
let matches = 0;
let lengthChange = 0;
const pathnames = new Array<string>();
const entities = new Array<MessageEntity>();
return [
text.replace(replacementExp, (_, s, o) => {
const url = urls[matches];
const pathname = new URL(url).pathname;
pathnames.push(pathname);
const untouchedS = s;
s = s.split("|")[1] || pathname;
entities.push({
offset: (matches == 0 ? o : o - (matches * 2)) + lengthChange,
length: s.length,
type: "text_link",
url,
});
matches++;
lengthChange += s.length - untouchedS.length;
return s;
}),
entities,
pathnames.join(", "),
];
}

0 comments on commit 41124df

Please sign in to comment.