Skip to content

Commit

Permalink
Merge pull request cyclic-software#4 from eludadev/inline-queries
Browse files Browse the repository at this point in the history
Handle inline queries for three text effects.
  • Loading branch information
eludadev authored Nov 5, 2022
2 parents b707dc0 + 7617bd9 commit 62e0320
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/bot.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 62e0320

Please sign in to comment.