mic-bot/bot/normal_mode/safebooru.ts

31 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-10-26 16:21:50 +00:00
import { InlineQueryResultBuilder, type InlineQueryContext } from "https://deno.land/x/grammy/mod.ts";
import type { InlineQueryResult } from "https://deno.land/x/grammy_types/inline.ts";
import type { Ctx } from "../ctx.ts";
import type { GetSafebooruPostsFunc } from "../../external/safebooru.ts";
import { SAFEBOORU_HOST } from "../../external/safebooru.ts";
export const DEFAULT_POSTS_LIMIT = 30
export const DEFAULT_START_PAGE_ID = 0
export const handleSafebooruQuery = async (
ctx: InlineQueryContext<Ctx>,
getPosts: GetSafebooruPostsFunc
): Promise<void> => {
// Remove the safebooru refix
const query = ctx.inlineQuery.query.slice("safebooru".length)
const tags = query.split(" ")
// So we make a request, get some posts in a structure
const posts = await getPosts(DEFAULT_POSTS_LIMIT, DEFAULT_START_PAGE_ID, tags)
const results: InlineQueryResult[] = []
posts.map((post) => {
results.push(InlineQueryResultBuilder.photo(`id-${post.id}`, post.file_url, {
thumbnail_url: post.preview_url,
caption: `[source](https://${SAFEBOORU_HOST}/index.php?page=post&s=view&id=${post.id})`,
parse_mode: "MarkdownV2",
}))
})
// await ctx.answerInlineQuery(results, { cache_time: 0 })
await ctx.api.answerInlineQuery(ctx.inlineQuery.id, results, { cache_time: 1 })
}