Skip to content

Commit

Permalink
fix(tgapi): Refactor Message options type
Browse files Browse the repository at this point in the history
  • Loading branch information
n0th1ng-else committed Jul 12, 2023
1 parent 8257ab8 commit 783cc3b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/telegram/actions/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export abstract class GenericAction {
this.text.t(part, meta.lang),
meta.lang,
{
buttons: meta.options,
buttons: meta.options?.buttons,
},
forumThreadId,
)
Expand All @@ -95,7 +95,7 @@ export abstract class GenericAction {
): Promise<void> {
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`));
}
Expand Down
2 changes: 1 addition & 1 deletion src/telegram/actions/fund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class FundAction extends GenericAction {
LabelId.FundCommandMessage,
{
lang,
options: buttons,
options: { buttons },
},
prefix,
model.forumThreadId,
Expand Down
28 changes: 15 additions & 13 deletions src/telegram/actions/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/telegram/actions/support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class SupportAction extends GenericAction {
LabelId.SupportCommand,
{
lang,
options: buttons,
options: { buttons },
},
prefix,
model.forumThreadId,
Expand Down
12 changes: 3 additions & 9 deletions src/telegram/api/tgapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {
PreCheckoutQueryDto,
TgCore,
TgFile,
TgInlineKeyboardButton,
TgInvoice,
TgLeaveChatSchema,
TgMessage,
TgMessageOptions,
TgSetWebHookSchema,
TgWebHook,
TgWebHookSchema,
Expand Down Expand Up @@ -81,10 +81,7 @@ export class TelegramApi {
public sendMessage(
chatId: number,
text: string,
options: {
buttons?: TgInlineKeyboardButton[][];
disableMarkup?: boolean;
} = {},
options: TgMessageOptions = {},
forumThreadId?: number,
): Promise<TgMessage> {
const data: MessageDto = {
Expand Down Expand Up @@ -113,10 +110,7 @@ export class TelegramApi {
chatId: number,
messageId: number,
text: string,
options: {
buttons?: TgInlineKeyboardButton[][];
disableMarkup?: boolean;
} = {},
options: TgMessageOptions = {},
): Promise<TgMessage> {
const data: MessageDto = {
text,
Expand Down
9 changes: 9 additions & 0 deletions src/telegram/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof TgMessageOptionsSchema>;

const BotCommandSchema = z
.object({
command: z.string(),
Expand Down
4 changes: 2 additions & 2 deletions src/telegram/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -85,7 +85,7 @@ export class BotMessageModel {

export interface MessageOptions {
lang: LanguageCode;
options?: TgInlineKeyboardButton[][];
options?: TgMessageOptions;
}

export class TelegramMessagePrefix {
Expand Down

0 comments on commit 783cc3b

Please sign in to comment.