-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot.ts
More file actions
42 lines (35 loc) · 1.21 KB
/
bot.ts
File metadata and controls
42 lines (35 loc) · 1.21 KB
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
import {
Bot,
Context,
NextFunction,
} from "https://deno.land/x/grammy@v1.38.2/mod.ts";
import { pipeline } from "./helper.ts";
const BOT_TOKEN = Deno.env.get("BOT_TOKEN");
if (!BOT_TOKEN) throw new Error("Bot Token is not set!");
const bot = new Bot(BOT_TOKEN);
async function responseTime(ctx: Context, next: NextFunction): Promise<void> {
console.log(ctx.inlineQuery?.query);
const before = Date.now(); // milliseconds
await next();
const after = Date.now(); // milliseconds
console.log(`Response time: ${after - before} ms`);
}
bot.use(responseTime);
bot.on("inline_query", async (ctx) => {
const userQuery = ctx.update.inline_query.query;
if (typeof userQuery !== "string" || userQuery.trim() === "") return;
const limit = 50;
const offset = ctx.update.inline_query.offset.trim()
? ctx.update.inline_query.offset.split(",")
: [];
const [start, end] = offset.length
? [Number(offset[0]), Number(offset[1])]
: [0, limit];
const results = await pipeline(userQuery);
const { notFound, result } = results;
return ctx.answerInlineQuery(result.slice(start, end), {
cache_time: notFound ? 0 : 2592000, /*30_DAYS*/
next_offset: `${end},${end + limit}`,
});
});
export { bot };