From af0c5c3eee8b4baeacdf86f4d2cfe59caae50650 Mon Sep 17 00:00:00 2001 From: drendog Date: Tue, 23 Jan 2024 17:57:36 +0100 Subject: [PATCH] refactor: :recycle: minor refactor on inline keyboards --- src/callbackQueries/mod.ts | 1 + src/callbackQueries/setupCallbackQueries.ts | 13 ++++--- src/callbackQueries/utils.ts | 4 +++ src/callbackQueries/voteCallback.ts | 4 +-- src/conversations/post.ts | 4 +-- src/keyboards/mod.ts | 6 ++-- src/keyboards/utils.ts | 4 +++ src/keyboards/vote.ts | 38 +++++++-------------- 8 files changed, 36 insertions(+), 38 deletions(-) create mode 100644 src/callbackQueries/utils.ts create mode 100644 src/keyboards/utils.ts diff --git a/src/callbackQueries/mod.ts b/src/callbackQueries/mod.ts index 3ce3083..4504be9 100644 --- a/src/callbackQueries/mod.ts +++ b/src/callbackQueries/mod.ts @@ -2,6 +2,7 @@ export { setupCallbackQueries } from "./setupCallbackQueries.ts"; export { voteCallback } from "./voteCallback.ts"; export enum CallbackQueryEnum { + NOOP = "noop", APPROVE = "approve", REJECT = "reject", } diff --git a/src/callbackQueries/setupCallbackQueries.ts b/src/callbackQueries/setupCallbackQueries.ts index 752608b..19c2cd6 100644 --- a/src/callbackQueries/setupCallbackQueries.ts +++ b/src/callbackQueries/setupCallbackQueries.ts @@ -1,23 +1,28 @@ import { Bot } from "grammy"; import { BotContext } from "../mod.ts"; -import { IButtonCallbackData } from "../keyboards/mod.ts"; import { CallbackQueryEnum, voteCallback } from "./mod.ts"; +import { parseCallbackQueryData } from "./utils.ts"; + + export const setupCallbackQueries = (bot: Bot) => { bot.on("callback_query:data", (ctx) => { - const callbackQueryData = JSON.parse( - ctx.callbackQuery.data, - ) as IButtonCallbackData; + const callbackQueryData = parseCallbackQueryData(ctx.callbackQuery.data); switch (callbackQueryData.cq) { case CallbackQueryEnum.APPROVE: case CallbackQueryEnum.REJECT: voteCallback(ctx, bot, callbackQueryData); break; + case CallbackQueryEnum.NOOP: + break; default: throw new Error("Invalid callback query data"); } + ctx.answerCallbackQuery(); }); console.log("Callback Queries setup complete"); }; + + diff --git a/src/callbackQueries/utils.ts b/src/callbackQueries/utils.ts new file mode 100644 index 0000000..f9b90fa --- /dev/null +++ b/src/callbackQueries/utils.ts @@ -0,0 +1,4 @@ +import { IButtonCallbackData } from "../keyboards/mod.ts"; + +export const parseCallbackQueryData = (data: string) => + JSON.parse(data) as IButtonCallbackData \ No newline at end of file diff --git a/src/callbackQueries/voteCallback.ts b/src/callbackQueries/voteCallback.ts index caa1ca3..ef2d9a5 100644 --- a/src/callbackQueries/voteCallback.ts +++ b/src/callbackQueries/voteCallback.ts @@ -32,8 +32,6 @@ export const voteCallback = async ( // edit the message to show the outcome of approval await ctx.editMessageReplyMarkup({ - reply_markup: { - inline_keyboard: getPostOutcomeKeyboard(isApproved), - }, + reply_markup: getPostOutcomeKeyboard(isApproved) }); }; diff --git a/src/conversations/post.ts b/src/conversations/post.ts index 06a02f6..6378ab4 100644 --- a/src/conversations/post.ts +++ b/src/conversations/post.ts @@ -14,8 +14,6 @@ export const post = async ( // Wait for a message response const postContext = await conversation.wait(); await postContext.copyMessage(GROUP_ID, { - reply_markup: { - inline_keyboard: getPostApprovalKeyboard(ctx.session.sessionId), - }, + reply_markup: getPostApprovalKeyboard(ctx.session.sessionId), }); }; diff --git a/src/keyboards/mod.ts b/src/keyboards/mod.ts index 72bf186..16a83a8 100644 --- a/src/keyboards/mod.ts +++ b/src/keyboards/mod.ts @@ -1,11 +1,13 @@ export * from "./vote.ts"; +export { getCallbackQueryData } from "./utils.ts"; import { CallbackQueryEnum } from "../callbackQueries/mod.ts"; export interface IButtonCallbackData { /** Callback Query */ - cq: CallbackQueryEnum.APPROVE | CallbackQueryEnum.REJECT; + cq: CallbackQueryEnum; /** Session ID */ - sid: number; + sid?: number; } + diff --git a/src/keyboards/utils.ts b/src/keyboards/utils.ts new file mode 100644 index 0000000..188d83b --- /dev/null +++ b/src/keyboards/utils.ts @@ -0,0 +1,4 @@ +import { IButtonCallbackData } from './mod.ts'; + +export const getCallbackQueryData = (buttonCallbackData: IButtonCallbackData) => + JSON.stringify(buttonCallbackData); \ No newline at end of file diff --git a/src/keyboards/vote.ts b/src/keyboards/vote.ts index e7d5a73..3f5b28f 100644 --- a/src/keyboards/vote.ts +++ b/src/keyboards/vote.ts @@ -1,35 +1,21 @@ -import { InlineKeyboardButton } from "https://deno.land/x/grammy_types@v3.4.6/mod.ts"; import { CallbackQueryEnum } from "../callbackQueries/mod.ts"; -import { IButtonCallbackData } from "./mod.ts"; +import { getCallbackQueryData } from "./mod.ts"; +import { InlineKeyboard } from "grammy"; -const getVoteButtonCallbackData = (sid: number, isApprove: boolean) => - JSON.stringify({ - cq: isApprove ? CallbackQueryEnum.APPROVE : CallbackQueryEnum.REJECT, +const getVoteButtonCallbackData = (sid: number, isApproved: boolean) => + getCallbackQueryData({ + cq: isApproved ? CallbackQueryEnum.APPROVE : CallbackQueryEnum.REJECT, sid, - } as IButtonCallbackData); + }); export const getPostApprovalKeyboard = ( sid: number, -): InlineKeyboardButton[][] => [ - [ - { - text: "🟢", - callback_data: getVoteButtonCallbackData(sid, true), - }, - { - text: "🔴", - callback_data: getVoteButtonCallbackData(sid, false), - }, - ], -]; +) => new InlineKeyboard() + .text("🟢", getVoteButtonCallbackData(sid, true)) + .text("🔴", getVoteButtonCallbackData(sid, false)) export const getPostOutcomeKeyboard = ( isApproved: boolean, -): InlineKeyboardButton[][] => [ - [ - { - text: isApproved ? "✅ Approved" : "❌ Rejected", - callback_data: "noop", - }, - ], -]; +) => new InlineKeyboard() + .text(isApproved ? "✅ Approved" : "❌ Rejected", + getCallbackQueryData({ cq: CallbackQueryEnum.NOOP })); \ No newline at end of file