UNPKG

bale-bot-ts

Version:

A TypeScript SDK for Bale.ai bots

601 lines (600 loc) 24.9 kB
import EventEmitter from 'events'; interface BotResponse<T> { ok: boolean; result: T; description?: string; error_code?: number; parameters?: object; } interface User { id: number; is_bot: boolean; first_name: string; last_name?: string; username?: string; language_code?: string; } interface Chat { id: number; type: 'private' | 'group' | 'channel'; title?: string; username?: string; first_name?: string; last_name?: string; photo?: ChatPhoto; } interface Message { message_id: number; from?: User; date: number; chat: Chat; forward_from?: User; forward_from_chat?: Chat; forward_from_message_id?: number; forward_date?: number; reply_to_message?: Message; edit_date?: number; text?: string; animation?: Animation; audio?: Audio; document?: Document; photo?: PhotoSize[]; sticker?: Sticker; video?: Video; voice?: Voice; caption?: string; contact?: Contact; location?: Location; new_chat_members?: User[]; left_chat_member?: User; invoice?: Invoice; successful_payment?: SuccessfulPayment; web_app_data?: WebAppData; reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup; } interface MessageId { message_id: number; } interface PhotoSize { file_id: string; file_unique_id: string; width: number; height: number; file_size?: number; } interface Animation { file_id: string; file_unique_id: string; width: number; height: number; duration: number; thumbnail?: PhotoSize; file_name?: string; mime_type?: string; file_size?: number; } interface Audio { file_id: string; file_unique_id: string; duration: number; title?: string; file_name?: string; mime_type?: string; file_size?: number; } interface Document { file_id: string; file_unique_id: string; thumbnail?: PhotoSize; file_name?: string; mime_type?: string; file_size?: number; } interface Video { file_id: string; file_unique_id: string; width: number; height: number; duration: number; file_name?: string; mime_type?: string; file_size?: number; } interface Voice { file_id: string; file_unique_id: string; duration?: number; } interface Contact { phone_number: string; first_name: string; last_name?: string; user_id?: number; } interface Location { longitude: number; latitude: number; } interface File { file_id: string; file_unique_id: string; file_size?: number; file_path?: string; } interface ReplyKeyboardMarkup { keyboard: KeyboardButton[][]; } interface KeyboardButton { text: string; request_contact?: boolean; request_location?: boolean; web_app?: WebAppInfo; } interface InlineKeyboardMarkup { inline_keyboard: InlineKeyboardButton[][]; } interface InlineKeyboardButton { text: string; url?: string; callback_data?: string; web_app?: WebAppInfo; copy_text?: CopyTextButton; } interface ReplyKeyboardRemove { remove_keyboard: true; } interface CallbackQuery { id: string; from: User; message?: Message; data?: string; } interface WebAppData { data: string; } interface WebAppInfo { url: string; } interface CopyTextButton { text: string; } interface ChatPhoto { small_file_id: string; small_file_unique_id: string; big_file_id: string; big_file_unique_id: string; } type InputFile = Buffer | NodeJS.ReadableStream | string; interface InputMediaBase { type: string; media: string; caption?: string; } interface InputMediaPhoto extends InputMediaBase { type: 'photo'; } interface InputMediaVideo extends InputMediaBase { type: 'video'; thumbnail?: string; width?: number; height?: number; duration?: number; } interface InputMediaAnimation extends InputMediaBase { type: 'animation'; thumbnail?: string; width?: number; height?: number; duration?: number; } interface InputMediaAudio extends InputMediaBase { type: 'audio'; thumbnail?: string; duration?: number; title?: string; } interface InputMediaDocument extends InputMediaBase { type: 'document'; thumbnail?: string; } type InputMedia = InputMediaAnimation | InputMediaDocument | InputMediaAudio | InputMediaPhoto | InputMediaVideo; interface Sticker { file_id: string; file_unique_id: string; type: 'regular' | 'mask'; width: number; height: number; file_size?: number; } interface InputSticker { sticker: string; } interface LabeledPrice { label: string; amount: number; } interface Invoice { title: string; description: string; payload: string; currency: string; total_amount: number; } interface PreCheckoutQuery { id: string; from: User; currency: string; total_amount: number; invoice_payload: string; shipping_option_id?: string; order_info?: OrderInfo; } interface OrderInfo { name?: string; phone_number?: string; email?: string; shipping_address?: ShippingAddress; } interface ShippingAddress { country_code: string; state: string; city: string; street_line1: string; street_line2: string; post_code: string; } interface SuccessfulPayment { currency: string; total_amount: number; invoice_payload: string; telegram_payment_charge_id: string; provider_payment_charge_id?: string; } interface Transaction { id: string; status: 'pending' | 'succeed' | 'failed' | 'rejected' | 'timeout'; userID: number; amount: number; createdAt: number; } interface SendMessageOptions { chat_id: number | string; text: string; parse_mode?: 'html' | 'markdown'; disable_web_page_preview?: boolean; disable_notification?: boolean; reply_to_message_id?: number; reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove; } interface GetUpdatesOptions { offset?: number; limit?: number; timeout?: number; } interface SetWebhookOptions { url: string; } interface WebhookInfo { url: string; has_custom_certificate: boolean; pending_update_count: number; ip_address?: string; last_error_date?: number; last_error_message?: string; last_synchronization_error_date?: number; max_connections?: number; allowed_updates?: string[]; } interface Update { update_id: number; message?: Message; edited_message?: Message; channel_post?: Message; edited_channel_post?: Message; callback_query?: CallbackQuery; pre_checkout_query?: PreCheckoutQuery; } export declare class BaleBotClient extends EventEmitter { private token; private api; private readonly baseURL; private pollingInterval; private lastUpdateId; /** * Creates a new BaleBotClient instance. * @param token - The unique authentication token for your bot. */ constructor(token: string); private sendRequest; startPolling(interval?: number): void; stopPolling(): void; /** * A simple method for testing your bot's authentication token. * @returns Basic information about the bot in the form of a User object. */ getMe(): Promise<BotResponse<User>>; /** * Use this method to log out from the cloud Bot API server before launching the bot in a test environment. * @returns True on success. */ logout(): Promise<BotResponse<boolean>>; /** * Use this method to close the bot instance before moving it from a local server or test environment to the production server. * @returns True on success. */ close(): Promise<BotResponse<boolean>>; /** * Use this method to send text messages. * @param options - Options for sending the message. * @returns The sent Message object on success. */ sendMessage(options: SendMessageOptions): Promise<BotResponse<Message>>; /** * Use this method to forward messages of any kind. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param from_chat_id - Unique identifier for the chat where the original message was sent. * @param message_id - Unique message identifier in the original chat. * @returns The forwarded Message on success. */ forwardMessage(chat_id: string | number, from_chat_id: string | number, message_id: number): Promise<BotResponse<Message>>; /** * Use this method to copy messages of any kind. Service messages and invoice messages can't be copied. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param from_chat_id - Unique identifier for the chat where the original message was sent. * @param message_id - Unique message identifier in the original chat. * @returns The MessageId of the sent message on success. */ copyMessage(chat_id: string | number, from_chat_id: string | number, message_id: number): Promise<BotResponse<MessageId>>; /** * Use this method to send photos. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param photo - Photo to send. Pass a file_id, HTTP URL, or upload a new photo using multipart/form-data. * @param caption - Photo caption (0-1024 characters). * @param reply_to_message_id - If the message is a reply, ID of the original message. * @param reply_markup - Additional interface options. * @returns The sent Message on success. */ sendPhoto(chat_id: string | number, photo: string | InputFile, caption?: string, reply_to_message_id?: number, reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove): Promise<BotResponse<Message>>; /** * Use this method to send audio files, if you want Bale clients to display them in the music player. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param audio - Audio file to send. Pass a file_id, HTTP URL, or upload a new audio file using multipart/form-data. * @param caption - Audio caption (0-1024 characters). * @param reply_to_message_id - If the message is a reply, ID of the original message. * @param reply_markup - Additional interface options. * @returns The sent Message on success. */ sendAudio(chat_id: string | number, audio: string | InputFile, caption?: string, reply_to_message_id?: number, reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove): Promise<BotResponse<Message>>; /** * Use this method to send general files. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param document - File to send. Pass a file_id, HTTP URL, or upload a new file using multipart/form-data. * @param caption - Document caption (0-1024 characters). * @param reply_to_message_id - If the message is a reply, ID of the original message. * @param reply_markup - Additional interface options. * @returns The sent Message on success. */ sendDocument(chat_id: string | number, document: string | InputFile, caption?: string, reply_to_message_id?: number, reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove): Promise<BotResponse<Message>>; /** * Use this method to send video files. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param video - Video to send. Pass a file_id, HTTP URL, or upload a new video file using multipart/form-data. * @param caption - Video caption (0-1024 characters). * @param reply_to_message_id - If the message is a reply, ID of the original message. * @param reply_markup - Additional interface options. * @returns The sent Message on success. */ sendVideo(chat_id: string | number, video: string | InputFile, caption?: string, reply_to_message_id?: number, reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove): Promise<BotResponse<Message>>; /** * Use this method to send animation files (GIF or H.264/MPEG-4 AVC video without sound). * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param animation - Animation to send. Pass a file_id, HTTP URL, or upload a new animation file using multipart/form-data. * @param reply_to_message_id - If the message is a reply, ID of the original message. * @param reply_markup - Additional interface options. * @returns The sent Message on success. */ sendAnimation(chat_id: string | number, animation: string | InputFile, reply_to_message_id?: number, reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove): Promise<BotResponse<Message>>; /** * Use this method to send audio files, if you want Bale clients to display the file as a playable voice message. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param voice - Audio file to send. Pass a file_id, HTTP URL, or upload a new voice file using multipart/form-data. * @param caption - Voice message caption (0-1024 characters). * @param reply_to_message_id - If the message is a reply, ID of the original message. * @param reply_markup - Additional interface options. * @returns The sent Message on success. */ sendVoice(chat_id: string | number, voice: string | InputFile, caption?: string, reply_to_message_id?: number, reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove): Promise<BotResponse<Message>>; /** * Use this method to send a group of photos, videos, documents or audios as an album. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param media - A JSON-serialized array describing messages to be sent. * @param reply_to_message_id - If the message is a reply, ID of the original message. * @returns An array of sent Messages on success. */ sendMediaGroup(chat_id: string | number, media: InputMedia[], reply_to_message_id?: number): Promise<BotResponse<Message[]>>; /** * Use this method to send point on the map. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param latitude - Latitude of the location. * @param longitude - Longitude of the location. * @param horizontal_accuracy - The radius of uncertainty for the location, measured in meters. 0-1500. * @param reply_to_message_id - If the message is a reply, ID of the original message. * @param reply_markup - Additional interface options. * @returns The sent Message on success. */ sendLocation(chat_id: string | number, latitude: number, longitude: number, horizontal_accuracy?: number, reply_to_message_id?: number, reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove): Promise<BotResponse<Message>>; /** * Use this method to send phone contacts. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param phone_number - Contact's phone number. * @param first_name - Contact's first name. * @param last_name - Contact's last name. * @param reply_to_message_id - If the message is a reply, ID of the original message. * @param reply_markup - Additional interface options. * @returns The sent Message on success. */ sendContact(chat_id: string | number, phone_number: string, first_name: string, last_name?: string, reply_to_message_id?: number, reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove): Promise<BotResponse<Message>>; /** * Use this method to get basic info about a file and prepare it for downloading. * @param file_id - File identifier to get info about. * @returns A File object on success. */ getFile(file_id: string): Promise<BotResponse<File>>; /** * Use this method to receive incoming updates using long polling. * @param options - Options for getting updates. * @returns An Array of Update objects on success. */ getUpdates(options?: GetUpdatesOptions): Promise<BotResponse<Update[]>>; /** * Use this method to specify a URL and receive incoming updates via an outgoing webhook. * @param options - Options for setting the webhook. * @returns True on success. */ setWebhook(options: SetWebhookOptions): Promise<BotResponse<boolean>>; /** * Use this method to get current webhook status. * @returns A WebhookInfo object on success. */ getWebhookInfo(): Promise<BotResponse<WebhookInfo>>; /** * Use this method to get up-to-date information about the chat. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @returns A Chat object on success. */ getChat(chat_id: string | number): Promise<BotResponse<Chat>>; /** * Use this method to get the number of members in a chat. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @returns An Integer representing the number of chat members. */ getChatMembersCount(chat_id: string | number): Promise<BotResponse<number>>; /** * Use this method to pin a message in a group or a channel. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param message_id - Identifier of the message to pin. * @returns True on success. */ pinChatMessage(chat_id: string | number, message_id: number): Promise<BotResponse<boolean>>; /** * Use this method to unpin a message in a group or a channel. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param message_id - Identifier of the message to unpin. * @returns True on success. */ unpinChatMessage(chat_id: string | number, message_id: number): Promise<BotResponse<boolean>>; /** * Use this method to unpin all pinned messages in a chat. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @returns True on success. */ unpinAllChatMessages(chat_id: string | number): Promise<BotResponse<boolean>>; /** * Use this method to change the title of a chat. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param title - New chat title. * @returns True on success. */ setChatTitle(chat_id: string | number, title: string): Promise<BotResponse<boolean>>; /** * Use this method to change the description of a group or a channel. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param description - New chat description. * @returns True on success. */ setChatDescription(chat_id: string | number, description: string): Promise<BotResponse<boolean>>; /** * Use this method to delete a chat photo. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @returns True on success. */ deleteChatPhoto(chat_id: string | number): Promise<BotResponse<boolean>>; /** * Use this method to create a new invite link for a chat. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @returns The new invite link on success. */ createChatInviteLink(chat_id: string | number): Promise<BotResponse<string>>; /** * Use this method to revoke an invite link for a group. After revocation, a new link is generated and returned. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param invite_link - The invite link to revoke. * @returns The new invite link on success. */ revokeChatInviteLink(chat_id: string | number, invite_link: string): Promise<BotResponse<string>>; /** * Use this method for the bot to create a new invite link for a chat. If already created, it will be revoked and a new one will be created. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @returns The new invite link on success. */ exportChatInviteLink(chat_id: string | number): Promise<BotResponse<string>>; /** * Use this method to edit text messages. * @param text - New text of the message (1-4096 characters). * @param chat_id - Required if inline_message_id is not specified. Unique identifier for the target chat or username of the target channel. * @param message_id - Required if inline_message_id is not specified. Identifier of the message to edit. * @param reply_markup - A JSON-serialized object for an inline keyboard. * @returns The edited Message object on success. */ editMessageText(text: string, chat_id?: string | number, message_id?: number, reply_markup?: InlineKeyboardMarkup): Promise<BotResponse<Message>>; /** * Use this method to delete a message, including service messages. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param message_id - Identifier of the message to delete. * @returns True on success. */ deleteMessage(chat_id: string | number, message_id: number): Promise<BotResponse<boolean>>; /** * Use this method to upload a file for future use in newStickerSet and addStickerToSet methods. * @param user_id - User identifier of sticker file owner. * @param sticker - A file of .WEBP, .PNG, .TGS, or .WEBM format. * @returns The uploaded File object on success. */ uploadStickerFile(user_id: number, sticker: InputFile): Promise<BotResponse<File>>; /** * Use this method to create a new sticker set owned by a user. * @param user_id - User identifier of created sticker set owner. * @param name - Short name of the sticker set. * @param title - Sticker set title. * @param stickers - A JSON-serialized list of 1-50 initial stickers to be added to the sticker set. * @returns True on success. */ createNewStickerSet(user_id: number, name: string, title: string, stickers: InputSticker[]): Promise<BotResponse<boolean>>; /** * Use this method to add a new sticker to a set created by the bot. * @param user_id - User identifier of sticker set owner. * @param name - Sticker set name. * @param sticker - A JSON-serialized object with information about the added sticker. * @returns True on success. */ addStickerToSet(user_id: number, name: string, sticker: InputSticker): Promise<BotResponse<boolean>>; /** * Use this method to send invoices. * @param chat_id - Unique identifier for the target chat or username of the target channel. * @param title - Product name (1-32 characters). * @param description - Product description (1-255 characters). * @param payload - Bot-defined invoice payload (1-128 bytes). * @param provider_token - Payment provider token (card number or wallet token). * @param prices - A JSON-serialized list of items and their prices. * @param photo_url - URL of the product photo. * @param reply_to_message_id - If the message is a reply, ID of the original message. * @returns The sent Message on success. */ sendInvoice(chat_id: string | number, title: string, description: string, payload: string, provider_token: string, prices: LabeledPrice[], photo_url?: string, reply_to_message_id?: number): Promise<BotResponse<Message>>; /** * Use this method to respond to a pre-checkout query. * @param pre_checkout_query_id - Unique identifier for the pre-checkout query to be answered. * @param ok - True if everything is alright, False otherwise. * @param error_message - Required if ok is False. Error message to be displayed to the user. * @returns True on success. */ answerPreCheckoutQuery(pre_checkout_query_id: string, ok: boolean, error_message?: string): Promise<BotResponse<boolean>>; /** * Use this method to inquire about the status of a specific transaction by sending its unique ID. * Note: This method is NOT present in standard Telegram bot libraries and requires direct HTTP request. * @param transaction_id - Unique ID of the transaction to inquire. * @returns A Transaction object on success. */ inquireTransaction(transaction_id: string): Promise<BotResponse<Transaction>>; } export {};