|
| 1 | +import { |
| 2 | + Bot, |
| 3 | + Context, |
| 4 | + InlineKeyboard, |
| 5 | + NextFunction, |
| 6 | +} from "https://deno.land/x/[email protected]/mod.ts"; |
| 7 | + |
| 8 | +import { escape } from "./formatter.ts"; |
| 9 | +import { forecast, weather, astronomy, api } from "./handler.ts"; |
| 10 | + |
| 11 | +const BOT_TOKEN = Deno.env.get("BOT_TOKEN"); |
| 12 | +if (!BOT_TOKEN) throw new Error("Bot Token is not set!"); |
| 13 | + |
| 14 | +const bot = new Bot(BOT_TOKEN); |
| 15 | + |
| 16 | +async function responseTime(ctx: Context, next: NextFunction): Promise<void> { |
| 17 | + if (!ctx.inlineQuery?.query) return await next(); |
| 18 | + console.log(ctx.inlineQuery?.query); |
| 19 | + const before = Date.now(); // milliseconds |
| 20 | + await next(); |
| 21 | + const after = Date.now(); // milliseconds |
| 22 | + console.log(`Response time: ${after - before} ms`); |
| 23 | +} |
| 24 | + |
| 25 | +bot.use(responseTime); |
| 26 | + |
| 27 | +bot.inlineQuery(/^[\w\s'-]+$/, async (ctx) => { |
| 28 | + const query = ctx.inlineQuery.query?.trim(); |
| 29 | + if (!query) return; |
| 30 | + const locations = await api("search", query); |
| 31 | + if (!locations || !locations.length) return; |
| 32 | + return ctx.answerInlineQuery( |
| 33 | + locations.map((location) => ({ |
| 34 | + type: "article", |
| 35 | + id: `id:${location.id}`, |
| 36 | + title: escape(`${location.name}`), |
| 37 | + description: escape(`${location.region}, ${location.country}`), |
| 38 | + input_message_content: { |
| 39 | + message_text: "Crunching weather data for you...", |
| 40 | + }, |
| 41 | + reply_markup: new InlineKeyboard().switchInlineCurrent( |
| 42 | + "Try another location", |
| 43 | + query.slice(0, -1) |
| 44 | + ), |
| 45 | + })), |
| 46 | + { |
| 47 | + cache_time: 0, |
| 48 | + } |
| 49 | + ); |
| 50 | +}); |
| 51 | + |
| 52 | +bot.on("chosen_inline_result", (ctx) => { |
| 53 | + const location = ctx.chosenInlineResult.result_id; |
| 54 | + const messageId = ctx.inlineMessageId; |
| 55 | + if (!messageId) return; |
| 56 | + return weather(ctx, location, messageId, ctx.from.id); |
| 57 | +}); |
| 58 | + |
| 59 | +bot.on("callback_query:data", async (ctx) => { |
| 60 | + const messageId = ctx.inlineMessageId; |
| 61 | + if (!messageId) return; |
| 62 | + const data = JSON.parse(ctx.callbackQuery.data) as { |
| 63 | + t: string; |
| 64 | + lc: string; |
| 65 | + uid?: number; |
| 66 | + }; |
| 67 | + if (ctx.callbackQuery?.from?.id !== data.uid) { |
| 68 | + return ctx.answerCallbackQuery( |
| 69 | + "Only the person who sent the above message can use this button." |
| 70 | + ); |
| 71 | + } |
| 72 | + if (data.t === "astronomy") { |
| 73 | + return await astronomy(ctx, data.lc, messageId, data.uid); |
| 74 | + } |
| 75 | + if (data.t === "weather") { |
| 76 | + return await weather(ctx, data.lc, messageId, data.uid); |
| 77 | + } |
| 78 | + if (data.t === "forecast") { |
| 79 | + return await forecast(ctx, data.lc, messageId, data.uid); |
| 80 | + } |
| 81 | + return ctx.api.editMessageTextInline(messageId, "Something went wrong!", { |
| 82 | + parse_mode: "HTML", |
| 83 | + reply_markup: new InlineKeyboard() |
| 84 | + .text( |
| 85 | + "Astronomy", |
| 86 | + `${JSON.stringify({ t: "astronomy", lc: data.lc, uid: data.uid })}` |
| 87 | + ) |
| 88 | + .text( |
| 89 | + "Weather", |
| 90 | + `${JSON.stringify({ t: "weather", lc: data.lc, uid: data.uid })}` |
| 91 | + ) |
| 92 | + .text( |
| 93 | + "Forecast", |
| 94 | + `${JSON.stringify({ t: "forecast", lc: data.lc, uid: data.uid })}` |
| 95 | + ), |
| 96 | + }); |
| 97 | +}); |
| 98 | +export { bot }; |
0 commit comments