Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for locales #24

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ search query as the link text.
– Use +search query/+ with a '/' suffix to share the \
URL itself instead of the page title.

To find results in specific locales, you can prepend '!locale-code' to your search query.
- For Spanish, use '!es search query'.
- For Indonesian, use '!id search query'.
- For Ukrainian, use '!uk search query'.
- For Chinese, use '!zh search query'.

You may also put them all together like this:
'!es +search query|this link+'

Join @grammyjs!`,
{ reply_markup: searchKeyboard },
),
Expand All @@ -57,7 +66,7 @@ bot.drop((ctx) => ctx.msg?.via_bot?.id === ctx.me.id)

bot.on("inline_query", async (ctx) => {
const { query, offset } = ctx.inlineQuery;
const { currentQuery, texts, completedQueries } = parse(query);
const { currentQuery, texts, completedQueries, lang } = parse(query);
const links = await Promise.all(
completedQueries.map(async (query) => ({
query,
Expand All @@ -73,8 +82,8 @@ bot.on("inline_query", async (ctx) => {
// pending current query
const off = parseInt(offset, 10);
const hits = isNaN(off)
? await search(currentQuery.query)
: await search(currentQuery.query, { offset: off });
? await search(currentQuery.query, lang)
: await search(currentQuery.query, lang, { offset: off });
nextOffset += hits.length;
if (texts.length === 0) {
// no rendering
Expand Down
25 changes: 25 additions & 0 deletions parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,41 @@ export interface ParseResult {
completedQueries: LabeledQuery[];
/** Interspersed text fragments */
texts: string[];
lang: string;
}
export interface LabeledQuery {
query: string;
label?: string;
}

export function parse(query: string) {
let facetFilters = "lang:en-US";
const re = /^!(en|es|id|uk|zh)(?:\s)(.*)$/i;
const match = query.match(re);

if (match) {
const [, lang, _query] = match;
switch (lang.toLowerCase()) {
case "es":
facetFilters = "lang:es-ES";
break;
case "id":
facetFilters = "lang:id";
break;
case "uk":
facetFilters = "lang:uk-UA";
break;
case "zh":
facetFilters = "lang:zh";
break;
}
query = _query;
}

const result: ParseResult = {
completedQueries: [],
texts: [],
lang: facetFilters,
};
const parts = query.split("+");
const len = parts.length;
Expand Down
3 changes: 2 additions & 1 deletion search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export async function searchOne(query: string) {
}
export async function search(
query: string,
lang: string,
options?: SearchPaginationOptions,
): Promise<Hit[]> {
const page = options === undefined ? undefined : {
Expand All @@ -35,7 +36,7 @@ export async function search(
const params = new URLSearchParams({
query,
...page,
facetFilters: '["lang:en-US"]',
facetFilters: `["${lang}"]`,
});
const body = enc.encode(JSON.stringify({ params: params.toString() }));
const res = await fetch(SEARCH_URL, { method: "POST", headers, body });
Expand Down