Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/client/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
ChatInviteLink,
Gifts,
PreparedInlineMessage,
PreparedKeyboardButton,
StarAmount,
OwnedGifts,
Story,
Expand Down Expand Up @@ -153,6 +154,9 @@ interface EventHandlers {
purchasedPaidMedia: (
paidMedia: import("../structures/PaidMediaPurchased").PaidMediaPurchased,
) => PossiblyAsync<void>;
managedBot: (
managedBot: import("../structures/ManagedBotUpdated").ManagedBotUpdated,
) => PossiblyAsync<void>;
}

type EventHandlerParameters =
Expand All @@ -173,7 +177,8 @@ type EventHandlerParameters =
| import("../structures/ChatJoinRequest").ChatJoinRequest
| import("../structures/ChatBoostUpdated").ChatBoostUpdated
| import("../structures/ChatBoostRemoved").ChatBoostRemoved
| import("../structures/PaidMediaPurchased").PaidMediaPurchased;
| import("../structures/PaidMediaPurchased").PaidMediaPurchased
| import("../structures/ManagedBotUpdated").ManagedBotUpdated;

class BaseClient extends EventEmitter {
public readonly rest: Rest;
Expand Down Expand Up @@ -1978,6 +1983,36 @@ class BaseClient extends EventEmitter {
.then((res) => new PreparedInlineMessage(res));
}


/** Use this method to get the current token of a managed bot. Returns the token on success. */
async getManagedBotToken(
params: MethodParameters["getManagedBotToken"],
): Promise<MethodsLibReturnType["getManagedBotToken"]> {
return this.rest.request<string>(
"getManagedBotToken",
toSnakeCase(params),
);
}

/** Use this method to replace the token of a managed bot. Returns the new token on success. */
async replaceManagedBotToken(
params: MethodParameters["replaceManagedBotToken"],
): Promise<MethodsLibReturnType["replaceManagedBotToken"]> {
return this.rest.request<string>(
"replaceManagedBotToken",
toSnakeCase(params),
);
}

/** Stores a keyboard button that can be sent by a user of a Mini App. Returns a PreparedKeyboardButton object. */
async savePreparedKeyboardButton(
params: MethodParameters["savePreparedKeyboardButton"],
): Promise<MethodsLibReturnType["savePreparedKeyboardButton"]> {
return this.rest
.request<any>("savePreparedKeyboardButton", toSnakeCase(params))
.then((res) => new PreparedKeyboardButton(this, res));
}

/** Use this method to send invoices. On success, the sent Message is returned. */
async sendInvoice(
params: MethodParameters["sendInvoice"],
Expand Down
14 changes: 14 additions & 0 deletions src/client/WorkerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { CallbackQuery } from "../structures/CallbackQuery";
import { BusinessConnection } from "../structures/business/BusinessConnection";
import { BusinessMessagesDeleted } from "../structures/business/BusinessMessagesDeleted";
import { PaidMediaPurchased } from "../structures/PaidMediaPurchased";
import { ManagedBotUpdated } from "../structures/ManagedBotUpdated";
import type { TelegramClient } from "./TelegramClient";

/**
Expand Down Expand Up @@ -53,6 +54,7 @@ class WorkerClient {
| ChatBoostUpdated
| ChatBoostRemoved
| PaidMediaPurchased
| ManagedBotUpdated
| undefined {
this.client.emit(
Events.RawUpdate,
Expand Down Expand Up @@ -136,6 +138,9 @@ class WorkerClient {
if ("purchased_paid_media" in data) {
return this.onPurchasedPaidMedia(data.purchased_paid_media);
}
if ("managed_bot" in (data as unknown as Record<string, unknown>)) {
return this.onManagedBot((data as any).managed_bot);
}
return;
}

Expand Down Expand Up @@ -458,6 +463,15 @@ class WorkerClient {

return paidMedia;
}

onManagedBot(data: any): ManagedBotUpdated | undefined {
if (!data) return;

const managedBot = new ManagedBotUpdated(this.client, data);
this.client.emit(Events.ManagedBot, managedBot);

return managedBot;
}
}

export { WorkerClient };
18 changes: 18 additions & 0 deletions src/client/interfaces/Markup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ export declare namespace KeyboardButton {
/** If specified, the user will be asked to create a poll and send it to the bot when the button is pressed. Available in private chats only. */
request_poll: KeyboardButtonPollType;
}
export interface RequestManagedBotButton extends CommonButton {
/** If specified, pressing the button will allow the user to select a manager bot and create a managed bot in a service message. Available in private chats only. */
request_managed_bot: KeyboardButtonRequestManagedBot;
}
export interface WebAppButton extends CommonButton {
/** If specified, the described Web App will be launched when the button is pressed. The Web App will be able to send a “web_app_data” service message. Available in private chats only. */
web_app: WebAppInfo;
Expand All @@ -188,6 +192,7 @@ export type KeyboardButton =
| KeyboardButton.RequestContactButton
| KeyboardButton.RequestLocationButton
| KeyboardButton.RequestPollButton
| KeyboardButton.RequestManagedBotButton
| KeyboardButton.WebAppButton
| string;

Expand All @@ -197,6 +202,19 @@ export interface KeyboardButtonPollType {
type?: "quiz" | "regular";
}


/** This object defines the criteria used to request a managed bot. Information about the selected manager bot and created managed bot will be shared with the bot when the corresponding button is pressed. */
export interface KeyboardButtonRequestManagedBot {
/** Signed 32-bit identifier of the request, which will be received back in the ManagedBotCreated object. Must be unique within the message */
request_id: number;
/** The username of the manager bot that can be used to create the managed bot. */
manager_bot_username?: string;
/** Suggested username for the managed bot. */
bot_username?: string;
/** Suggested name for the managed bot. */
bot_name?: string;
}

/** Upon receiving a message with this object, Telegram clients will remove the current custom keyboard and display the default letter-keyboard. By default, custom keyboards are displayed until a new keyboard is sent by a bot. An exception is made for one-time keyboards that are hidden immediately after the user presses a button (see ReplyKeyboardMarkup). Not supported in channels and for messages sent on behalf of a Telegram Business account. */
export interface ReplyKeyboardRemove {
/** Requests clients to remove the custom keyboard (user will not be able to summon this keyboard; if you want to hide the keyboard from sight but keep it accessible, use one_time_keyboard in ReplyKeyboardMarkup) */
Expand Down
6 changes: 5 additions & 1 deletion src/client/interfaces/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ export interface User {
/** User's or bot's username */
username?: string;
/** IETF language tag of the user's language */
language_code?: LanguageCode;
language_code?: LanguageCode | string;
/** True, if this user is a Telegram Premium user */
is_premium?: true;
/** True, if this user added the bot to the attachment menu */
added_to_attachment_menu?: true;
/** True, if the bot can be invited to create managed bots */
can_manage_bots?: true;
}

/** Describes the options used for link preview generation. */
Expand Down Expand Up @@ -121,6 +123,8 @@ export interface ReplyParameters {
chat_id?: number | string;
/** Identifier of the specific checklist task to be replied to */
checklist_task_id?: number;
/** Identifier of the poll option to be replied to */
poll_option_id?: number;
/** Pass True if the message should be sent even if the specified message to be replied to is not found; can be used only for replies in the same chat and forum topic. Always True for messages sent on behalf of a business account. */
allow_sending_without_reply?: boolean;
/** Quoted part of the message to be replied to; 0-1024 characters after entities parsing. The quote must be an exact substring of the message to be replied to, including bold, italic, underline, strikethrough, spoiler, and custom_emoji entities. The message will fail to send if the quote isn't found in the original message. */
Expand Down
53 changes: 48 additions & 5 deletions src/client/interfaces/Methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -952,19 +952,33 @@ export type ApiMethods = {
isAnonymous?: boolean;
/** Poll type, “quiz” or “regular”, defaults to “regular” */
type?: "quiz" | "regular";
/** True, if the poll allows multiple answers, ignored for polls in quiz mode, defaults to False */
/** True, if the poll allows multiple answers, defaults to False */
allowsMultipleAnswers?: boolean;
/** 0-based identifier of the correct answer option, required for polls in quiz mode */
correctOptionId?: number;
/** 0-based identifiers of the correct answer options, required for polls in quiz mode */
correctOptionIds?: number[];
/** True, if users can change their answers after voting */
allowsRevoting?: boolean;
/** True, if poll options should be shuffled */
shuffleOptions?: boolean;
/** True, if users are allowed to add options */
allowAddingOptions?: boolean;
/** True, if results should be hidden until the poll is closed */
hideResultsUntilCloses?: boolean;
/** Text that is shown when a user chooses an incorrect answer or taps on the lamp icon in a quiz-style poll, 0-200 characters with at most 2 line feeds after entities parsing */
explanation?: string;
/** Mode for parsing entities in the explanation. See formatting options for more details. */
explanationParseMode?: ParseMode;
/** A list of special entities that appear in the poll explanation. It can be specified instead of explanationParseMode */
explanationEntities?: MessageEntity[];
/** Amount of time in seconds the poll will be active after creation, 5-600. Can't be used together with closeDate. */
/** Poll description, 0-200 characters */
description?: string;
/** Mode for parsing entities in the poll description */
descriptionParseMode?: ParseMode;
/** List of special entities that appear in the poll description */
descriptionEntities?: MessageEntity[];
/** Amount of time in seconds the poll will be active after creation, 5-2628000. Can't be used together with closeDate. */
openPeriod?: number;
/** Point in time (Unix timestamp) when the poll will be automatically closed. Must be at least 5 and no more than 600 seconds in the future. Can't be used together with openPeriod. */
/** Point in time (Unix timestamp) when the poll will be automatically closed. Must be at least 5 and no more than 2628000 seconds in the future. Can't be used together with openPeriod. */
closeDate?: number;
/** Pass True if the poll needs to be immediately closed. This can be useful for poll preview. */
isClosed?: boolean;
Expand Down Expand Up @@ -2298,6 +2312,35 @@ export type ApiMethods = {
allowChannelChats?: boolean;
}): import("../../structures/misc/PreparedInlineMessage").PreparedInlineMessage;


/** Use this method to get the current token of a managed bot. */
getManagedBotToken(args: {
/** Identifier of the managed bot */
botId: number | string;
}): string;

/** Use this method to replace the token of a managed bot. */
replaceManagedBotToken(args: {
/** Identifier of the managed bot */
botId: number | string;
}): string;

/** Stores a keyboard button that can be sent by a user of a Mini App. */
savePreparedKeyboardButton(args: {
/** Unique identifier of the target user that can use the prepared button */
userId: string | number;
/** The keyboard button to prepare */
button: import("./Markup").KeyboardButton;
/** Pass True if the button can be sent to private chats with users */
allowUserChats?: boolean;
/** Pass True if the button can be sent to private chats with bots */
allowBotChats?: boolean;
/** Pass True if the button can be sent to group and supergroup chats */
allowGroupChats?: boolean;
/** Pass True if the button can be sent to channel chats */
allowChannelChats?: boolean;
}): import("../../structures/misc/PreparedKeyboardButton").PreparedKeyboardButton;

/** Use this method to send invoices. On success, the sent Message is returned. */
sendInvoice(args: {
/** Unique identifier for the target chat or username of the target channel (in the format @channelusername) */
Expand Down
18 changes: 18 additions & 0 deletions src/structures/ManagedBotCreated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @ts-check
const { Base } = require("./Base");

class ManagedBotCreated extends Base {
/**
* @param {import("../client/TelegramClient").TelegramClient | import("../client/BaseClient").BaseClient} client
* @param {any} data
*/
constructor(client, data) {
super(client);

this.requestId = data.request_id;
this.manager = this.client.users._add(data.manager_bot);
this.bot = this.client.users._add(data.bot);
}
}

module.exports = { ManagedBotCreated };
18 changes: 18 additions & 0 deletions src/structures/ManagedBotUpdated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @ts-check
const { Base } = require("./Base");

class ManagedBotUpdated extends Base {
/**
* @param {import("../client/TelegramClient").TelegramClient | import("../client/BaseClient").BaseClient} client
* @param {any} data
*/
constructor(client, data) {
super(client);

if ("bot" in data) this.bot = this.client.users._add(data.bot);
if ("manager_bot" in data) this.manager = this.client.users._add(data.manager_bot);
if ("token" in data) this.token = data.token;
}
}

module.exports = { ManagedBotUpdated };
3 changes: 3 additions & 0 deletions src/structures/PollAnswer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class PollAnswer extends Base {

/** 0-based identifiers of chosen answer options. May be empty if the vote was retracted */
this.ids = data.option_ids;

/** Persistent identifiers of chosen answer options. May be empty if the vote was retracted */
this.persistentIds = (/** @type {any} */ (data)).option_persistent_ids || [];
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/structures/PollOptionAdded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @ts-check
const { Base } = require("./Base");

class PollOptionAdded extends Base {
/**
* @param {import("../client/TelegramClient").TelegramClient | import("../client/BaseClient").BaseClient} client
* @param {any} data
*/
constructor(client, data) {
super(client);
this.pollId = data.poll_id;
this.optionId = data.option_id;
}
}

module.exports = { PollOptionAdded };
16 changes: 16 additions & 0 deletions src/structures/PollOptionDeleted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @ts-check
const { Base } = require("./Base");

class PollOptionDeleted extends Base {
/**
* @param {import("../client/TelegramClient").TelegramClient | import("../client/BaseClient").BaseClient} client
* @param {any} data
*/
constructor(client, data) {
super(client);
this.pollId = data.poll_id;
this.optionId = data.option_id;
}
}

module.exports = { PollOptionDeleted };
6 changes: 6 additions & 0 deletions src/structures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,9 @@ module.exports.Story = require("./story/Story").Story;
module.exports.PaidMedia = require("./media/paid/PaidMedia").PaidMedia;
module.exports.PaidMediaInfo =
require("./media/paid/PaidMediaInfo").PaidMediaInfo;
module.exports.ManagedBotCreated = require("./ManagedBotCreated").ManagedBotCreated;
module.exports.ManagedBotUpdated = require("./ManagedBotUpdated").ManagedBotUpdated;
module.exports.PollOptionAdded = require("./PollOptionAdded").PollOptionAdded;
module.exports.PollOptionDeleted = require("./PollOptionDeleted").PollOptionDeleted;
module.exports.PreparedKeyboardButton =
require("./misc/PreparedKeyboardButton").PreparedKeyboardButton;
Loading
Loading