Skip to content

Commit

Permalink
update to Bot API 7.11
Browse files Browse the repository at this point in the history
  • Loading branch information
Borodin committed Oct 31, 2024
1 parent 08761a2 commit bde8948
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 12 deletions.
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![npm](https://img.shields.io/npm/v/typescript-telegram-bot-api)](https://www.npmjs.com/package/typescript-telegram-bot-api)
[![npm](https://img.shields.io/npm/dt/typescript-telegram-bot-api)](https://www.npmjs.com/package/typescript-telegram-bot-api)
[![codecov](https://codecov.io/github/Borodin/typescript-telegram-bot-api/graph/badge.svg?token=509N5AZDTV)](https://codecov.io/github/Borodin/typescript-telegram-bot-api)
[![GitHub](https://img.shields.io/badge/Bot_API-v7.10-0088cc)](https://core.telegram.org/bots/api#september-6-2024)
[![GitHub](https://img.shields.io/badge/Bot_API-v7.11-0088cc)](https://core.telegram.org/bots/api#october-31-2024)


This is a TypeScript wrapper for the [Telegram Bot API](https://core.telegram.org/bots/api) Node.js and browsers. It allows you to easily interact with the Telegram Bot API using TypeScript.
Expand Down Expand Up @@ -120,6 +120,39 @@ bot.on('message:audio', (message) => {
});
```

## Error Handling
Wrap asynchronous calls in `try...catch` blocks or use `.catch()` on promises to handle exceptions properly.

```typescript
try {
await bot.sendPhoto({
chat_id: chat_id,
photo: createReadStream('photo.jpg'),
caption: 'stream',
});
} catch (error: unknown) {
if (TelegramBot.isTelegramError(error)) {
// Handle Telegram API errors
if(error.response.description === 'Bad Request: chat not found'){
console.info('Message not sent: chat not found');
} else if (error.response.description === 'Request Entity Too Large'){
console.info('Message not sent: file too large');
} else if (error.response.description === 'Bad Request: IMAGE_PROCESS_FAILED'){
console.info('Message not sent: image processing failed');
} else if (error.response.description === 'Forbidden: bot was blocked by the user'){
console.info('Message not sent: user blocked bot');
} else {
console.error('Telegram API Error:', error.message);
}
} else if (error instanceof Error) {
// Handle system errors (example: no such file)
console.error('System Error:', error.message);
} else {
console.error('Unknown Error:', error);
}
}
```

## Webhooks
To use webhooks, you need to set up a server that will receive updates from Telegram. You can use the [express](https://www.npmjs.com/package/express) library for this purpose.

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "commonjs",
"name": "typescript-telegram-bot-api",
"version": "0.3.6",
"version": "0.4.0",
"description": "Telegram Bot API wrapper for Node.js written in TypeScript",
"repository": "github:Borodin/typescript-telegram-bot-api",
"main": "dist/index.js",
Expand Down Expand Up @@ -46,8 +46,8 @@
"typescript-eslint": "^7.13.0"
},
"dependencies": {
"axios": "^1.7.2",
"form-data": "^4.0.0"
"axios": "^1.7.7",
"form-data": "^4.0.1"
},
"jest": {
"preset": "ts-jest",
Expand Down
145 changes: 140 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,13 @@ export class TelegramBot extends EventEmitter {
*/
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;

/**
* Unique identifier of the message effect to be added to the message; for private chats only
*/
Expand Down Expand Up @@ -541,6 +548,13 @@ export class TelegramBot extends EventEmitter {
show_caption_above_media?: boolean;
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
}): Promise<MessageId> {
Expand Down Expand Up @@ -596,6 +610,13 @@ export class TelegramBot extends EventEmitter {
has_spoiler?: boolean;
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
message_effect_id?: string;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
Expand Down Expand Up @@ -630,6 +651,13 @@ export class TelegramBot extends EventEmitter {
thumbnail?: InputFile | string;
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
message_effect_id?: string;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
Expand Down Expand Up @@ -658,6 +686,13 @@ export class TelegramBot extends EventEmitter {
disable_content_type_detection?: boolean;
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
message_effect_id?: string;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
Expand Down Expand Up @@ -693,6 +728,13 @@ export class TelegramBot extends EventEmitter {
supports_streaming?: boolean;
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
message_effect_id?: string;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
Expand Down Expand Up @@ -727,6 +769,13 @@ export class TelegramBot extends EventEmitter {
has_spoiler?: boolean;
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
message_effect_id?: string;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
Expand Down Expand Up @@ -757,6 +806,13 @@ export class TelegramBot extends EventEmitter {
duration?: number;
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
message_effect_id?: string;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
Expand Down Expand Up @@ -784,6 +840,13 @@ export class TelegramBot extends EventEmitter {
thumbnail?: InputFile | string;
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
message_effect_id?: string;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
Expand Down Expand Up @@ -811,6 +874,13 @@ export class TelegramBot extends EventEmitter {
show_caption_above_media?: boolean;
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
}): Promise<Message> {
Expand All @@ -836,6 +906,13 @@ export class TelegramBot extends EventEmitter {
media: (InputMediaAudio | InputMediaDocument | InputMediaPhoto | InputMediaVideo)[];
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
message_effect_id?: string;
reply_parameters?: ReplyParameters;
}): Promise<Message[]> {
Expand All @@ -862,6 +939,13 @@ export class TelegramBot extends EventEmitter {
proximity_alert_radius?: number;
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
message_effect_id?: string;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
Expand Down Expand Up @@ -891,6 +975,13 @@ export class TelegramBot extends EventEmitter {
google_place_type?: string;
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
message_effect_id?: string;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
Expand All @@ -916,6 +1007,13 @@ export class TelegramBot extends EventEmitter {
vcard?: string;
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
message_effect_id?: string;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
Expand Down Expand Up @@ -951,6 +1049,13 @@ export class TelegramBot extends EventEmitter {
is_closed?: boolean;
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
message_effect_id?: string;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
Expand All @@ -976,6 +1081,13 @@ export class TelegramBot extends EventEmitter {
emoji?: '🎲' | '🎯' | '🏀' | '⚽' | '🎳' | '🎰';
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
message_effect_id?: string;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
Expand Down Expand Up @@ -1836,11 +1948,13 @@ export class TelegramBot extends EventEmitter {

/**
* ## editMessageMedia
* Use this method to edit animation, audio, document, photo, or video messages. If a message is part of a message
* album, then it can be edited only to an audio for audio albums, only to a document for document albums and to a
* photo or a video otherwise. When an inline message is edited, a new file can't be uploaded; use a previously
* uploaded file via its file_id or specify a URL. On success, if the edited message is not an inline message, the
* edited Message is returned, otherwise True is returned.
* Use this method to edit animation, audio, document, photo, or video messages, or to add media to text messages. If
* a message is part of a message album, then it can be edited only to an audio for audio albums, only to a document
* for document albums and to a photo or a video otherwise. When an inline message is edited, a new file can't be
* uploaded; use a previously uploaded file via its file_id or specify a URL. On success, if the edited message is not
* an inline message, the edited Message is returned, otherwise True is returned. Note that business messages that
* were not sent by the bot and do not contain an inline keyboard can only be edited within 48 hours from the time
* they were sent.
* @see https://core.telegram.org/bots/api#editmessagemedia
*/
async editMessageMedia(
Expand Down Expand Up @@ -1979,6 +2093,13 @@ export class TelegramBot extends EventEmitter {
emoji?: string;
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
message_effect_id?: string;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply;
Expand Down Expand Up @@ -2242,6 +2363,13 @@ export class TelegramBot extends EventEmitter {
is_flexible?: T extends 'XTR' ? never : boolean;
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
message_effect_id?: string;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup;
Expand Down Expand Up @@ -2377,6 +2505,13 @@ export class TelegramBot extends EventEmitter {
game_short_name: string;
disable_notification?: boolean;
protect_content?: boolean;

/**
* Pass True to allow up to 1000 messages per second, ignoring
* [broadcasting limits](https://core.telegram.org/bots/faq#how-can-i-message-all-of-my-bot-39s-subscribers-at-once)
* for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance
*/
allow_paid_broadcast?: boolean;
message_effect_id?: string;
reply_parameters?: ReplyParameters;
reply_markup?: InlineKeyboardMarkup;
Expand Down
11 changes: 11 additions & 0 deletions src/types/CopyTextButton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* ## CopyTextButton
* This object represents an inline keyboard button that copies specified text to the clipboard.
* @see https://core.telegram.org/bots/api#copytextbutton
*/
export type CopyTextButton = {
/**
* The text to be copied to the clipboard; 1-256 characters
*/
text: string;
};
7 changes: 6 additions & 1 deletion src/types/InlineKeyboardButton.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CallbackGame, WebAppInfo, LoginUrl, SwitchInlineQueryChosenChat } from './';
import { CallbackGame, WebAppInfo, LoginUrl, SwitchInlineQueryChosenChat, CopyTextButton } from './';

/**
* ## InlineKeyboardButton
Expand Down Expand Up @@ -60,6 +60,11 @@ export type InlineKeyboardButton = {
*/
switch_inline_query_chosen_chat?: SwitchInlineQueryChosenChat;

/**
* Optional. Description of the button that copies the specified text to the clipboard.
*/
copy_text?: CopyTextButton;

/**
* Optional. Description of the game that will be launched when the user presses the button.
* NOTE: This type of button must always be the first button in the first row.
Expand Down
2 changes: 1 addition & 1 deletion src/types/MessageEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type MessageEntity = {
/**
* Type of the entity. Currently, can be
* “mention” (@username),
* “hashtag” (#hashtag),
* “hashtag” (#hashtag or #hashtag@chatusername),
* “cashtag” ($USD),
* “bot_command” (/start@jobs_bot),
* “url” (https://telegram.org),
Expand Down
Loading

0 comments on commit bde8948

Please sign in to comment.