From 783cc3ba5cdc7c6b5f24dff36572dadb600bd283 Mon Sep 17 00:00:00 2001 From: Sergey Nikitin Date: Wed, 12 Jul 2023 21:28:58 +0200 Subject: [PATCH] fix(tgapi): Refactor Message options type --- src/telegram/actions/common.ts | 4 ++-- src/telegram/actions/fund.ts | 2 +- src/telegram/actions/lang.ts | 28 +++++++++++++++------------- src/telegram/actions/support.ts | 2 +- src/telegram/api/tgapi.ts | 12 +++--------- src/telegram/api/types.ts | 9 +++++++++ src/telegram/types.ts | 4 ++-- 7 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/telegram/actions/common.ts b/src/telegram/actions/common.ts index 553d2680..f862b838 100644 --- a/src/telegram/actions/common.ts +++ b/src/telegram/actions/common.ts @@ -73,7 +73,7 @@ export abstract class GenericAction { this.text.t(part, meta.lang), meta.lang, { - buttons: meta.options, + buttons: meta.options?.buttons, }, forumThreadId, ) @@ -95,7 +95,7 @@ export abstract class GenericAction { ): Promise { return this.bot .editMessageText(chatId, messageId, this.text.t(id, meta.lang), { - buttons: meta.options, + buttons: meta.options?.buttons, }) .then(() => logger.info(`${prefix.getPrefix()} Updated message`)); } diff --git a/src/telegram/actions/fund.ts b/src/telegram/actions/fund.ts index f231f841..0a75e19a 100644 --- a/src/telegram/actions/fund.ts +++ b/src/telegram/actions/fund.ts @@ -71,7 +71,7 @@ export class FundAction extends GenericAction { LabelId.FundCommandMessage, { lang, - options: buttons, + options: { buttons }, }, prefix, model.forumThreadId, diff --git a/src/telegram/actions/lang.ts b/src/telegram/actions/lang.ts index f4ca8e78..3d66a44f 100644 --- a/src/telegram/actions/lang.ts +++ b/src/telegram/actions/lang.ts @@ -184,20 +184,22 @@ export class LangAction extends GenericAction { LabelId.ChangeLangTitle, { lang, - options: [ - [ - { - text: this.text.t(LabelId.BtnRussian, lang), - callback_data: RuData.getDtoString(), - }, + options: { + buttons: [ + [ + { + text: this.text.t(LabelId.BtnRussian, lang), + callback_data: RuData.getDtoString(), + }, + ], + [ + { + text: this.text.t(LabelId.BtnEnglish, lang), + callback_data: EnData.getDtoString(), + }, + ], ], - [ - { - text: this.text.t(LabelId.BtnEnglish, lang), - callback_data: EnData.getDtoString(), - }, - ], - ], + }, }, prefix, model.forumThreadId, diff --git a/src/telegram/actions/support.ts b/src/telegram/actions/support.ts index 864309ef..b8fbbbb6 100644 --- a/src/telegram/actions/support.ts +++ b/src/telegram/actions/support.ts @@ -65,7 +65,7 @@ export class SupportAction extends GenericAction { LabelId.SupportCommand, { lang, - options: buttons, + options: { buttons }, }, prefix, model.forumThreadId, diff --git a/src/telegram/api/tgapi.ts b/src/telegram/api/tgapi.ts index 0ca1d696..0e38e91c 100644 --- a/src/telegram/api/tgapi.ts +++ b/src/telegram/api/tgapi.ts @@ -11,10 +11,10 @@ import { PreCheckoutQueryDto, TgCore, TgFile, - TgInlineKeyboardButton, TgInvoice, TgLeaveChatSchema, TgMessage, + TgMessageOptions, TgSetWebHookSchema, TgWebHook, TgWebHookSchema, @@ -81,10 +81,7 @@ export class TelegramApi { public sendMessage( chatId: number, text: string, - options: { - buttons?: TgInlineKeyboardButton[][]; - disableMarkup?: boolean; - } = {}, + options: TgMessageOptions = {}, forumThreadId?: number, ): Promise { const data: MessageDto = { @@ -113,10 +110,7 @@ export class TelegramApi { chatId: number, messageId: number, text: string, - options: { - buttons?: TgInlineKeyboardButton[][]; - disableMarkup?: boolean; - } = {}, + options: TgMessageOptions = {}, ): Promise { const data: MessageDto = { text, diff --git a/src/telegram/api/types.ts b/src/telegram/api/types.ts index cafbd5d2..980a6da7 100644 --- a/src/telegram/api/types.ts +++ b/src/telegram/api/types.ts @@ -176,6 +176,15 @@ export type TgInlineKeyboardButton = z.infer< typeof TgInlineKeyboardButtonSchema >; +const TgMessageOptionsSchema = z + .object({ + buttons: z.optional(z.array(z.array(TgInlineKeyboardButtonSchema))), + disableMarkup: z.optional(z.boolean()), + }) + .describe("Telegram message options schema"); + +export type TgMessageOptions = z.infer; + const BotCommandSchema = z .object({ command: z.string(), diff --git a/src/telegram/types.ts b/src/telegram/types.ts index 970474a8..3ffc6711 100644 --- a/src/telegram/types.ts +++ b/src/telegram/types.ts @@ -17,7 +17,7 @@ import type { LanguageCode } from "../recognition/types.js"; import { Logger } from "../logger/index.js"; import { MenuLabel } from "../text/labels.js"; import { TextModel } from "../text/index.js"; -import { TgInlineKeyboardButton, TgMessage } from "./api/types.js"; +import { TgMessage, TgMessageOptions } from "./api/types.js"; import { AnalyticsData } from "../analytics/ga/types.js"; export enum VoiceContentReason { @@ -85,7 +85,7 @@ export class BotMessageModel { export interface MessageOptions { lang: LanguageCode; - options?: TgInlineKeyboardButton[][]; + options?: TgMessageOptions; } export class TelegramMessagePrefix {