From 7617bd9dd6f3689192d719f8e3c7cc1227ce27b5 Mon Sep 17 00:00:00 2001 From: Eluda <111eluda111@gmail.com> Date: Sat, 5 Nov 2022 20:47:49 +0100 Subject: [PATCH] Handle inline queries for three text effects. --- src/bot.ts | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/bot.ts b/src/bot.ts index 38766b95..92cb4b50 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -1,7 +1,7 @@ import { Bot, InlineKeyboard, webhookCallback } from "grammy"; import { chunk } from "lodash"; import express from "express"; -import { applyTextEffect } from "./textEffects"; +import { applyTextEffect, Variant } from "./textEffects"; import type { Variant as TextEffectVariant } from "./textEffects"; @@ -95,6 +95,44 @@ bot.command("effect", (ctx) => }) ); +// Handle inline queries +const queryRegEx = /effect (monospace|bold|italic) (.*)/; +bot.inlineQuery(queryRegEx, async (ctx) => { + const fullQuery = ctx.inlineQuery.query; + const fullQueryMatch = fullQuery.match(queryRegEx); + if (!fullQueryMatch) return; + + const effectLabel = fullQueryMatch[1]; + const originalText = fullQueryMatch[2]; + + const effectCode = allEffects.find( + (effect) => effect.label.toLowerCase() === effectLabel.toLowerCase() + )?.code; + const modifiedText = applyTextEffect(originalText, effectCode as Variant); + + await ctx.answerInlineQuery( + [ + { + type: "article", + id: "text-effect", + title: "Text Effects", + input_message_content: { + message_text: `Original: ${originalText} +Modified: ${modifiedText}`, + parse_mode: "HTML", + }, + reply_markup: new InlineKeyboard().switchInline("Share", fullQuery), + url: "http://t.me/EludaDevSmarterBot", + description: "Create stylish Unicode text, all within Telegram.", + }, + ], + { cache_time: 30 * 24 * 3600 } // one month in seconds + ); +}); + +// Return empty result list for other queries. +bot.on("inline_query", (ctx) => ctx.answerInlineQuery([])); + // Handle text effects from the effect keyboard for (const effect of allEffects) { const allEffectCodes = allEffects.map((effect) => effect.code);