Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Bot API Forward Compatibility

Bibo-Joshi edited this pageFeb 8, 2024 ·4 revisions

Thetelegram package contains PTBs Python implementation of the classes and methods defined in theBot API.This implementation is manually updated whenever Telegram releases a new version of the API.Due to the nature of this open-source library, the implementation work usually takes some time, depending on how much changed.However, users of PTB might want to immediately use the new functionality provided by Telegram.PTB therefore provides mechanism that make exactly this possible, i.e. accessing information from the Bot API that has no native PTB implementation yet.Below, we will describe the mechanisms in detail.

Warning

It is important to note that all these mechanism are intended to be used as temporary solutions only.If you use these mechanisms in your code and then upgrade to a PTB version that adds native support for the new Bot API functionality, you'll have to adapt your code to use PTBs native interfaces instead.

Object Attributes

Receiving Objects

Telegram sends data in the form of JSON objects either for updates that your bot receives or as return value of methods.When Telegram adds new attributes ("fields") to these objects that, the corresponding attributes in PTBs classes will be missing.However, any additional data that is not yet covered by the native Python attributes will be gathered in theTelegramObject.api_kwargs attribute (note thatTelegramObject is the base class of (almost) all other classes in thetelegram package).

For example, imagine that Telegram adds a new field calledread_date to theMessage class. Then accessingupdate.message.read_date is possible only after PTB was updated, however accessingupdate.message.api_kwargs["read_date"] would give the value provided by Telegram.

Important

TelegramObject.api_kwargs will always contain the raw JSON data received from Telegram. For example, a date will be a simple unix timestamp instead of a timezone awaredatetime.datetime object. These kinds of conversions are available only once PTB is updated to the new API version.

As another example, imagine that Telegram adds a new filed calleduser_online_status_updated to theUpdate class. Then accessingupdate.user_online_status_updated is again not immediately possible. Moreover, there is not yet a handler intelegram.ext to handle these new kinds of updates. However, in this case you can e.g. useTypeHandler to catch these updates, like this

fromtelegramimportUpdatefromtelegram.extimportTypeHandlerasyncdefcallback(update,context):ifstatus_updated:=update.api_kwargs.get("user_online_status_updated"):print(status_updated)application.add_handler(TypeHandler(Update,callback))

Of course you can also implement your own subclass ofBaseHandler that checks for the presence of the keyuser_online_status_updated inupdate.api_kwargs. See alsothis page.

Sending Objects

The Bot API also receives objects from you, namely as input arguments for the different Bot methods. For example,Bot.set_my_command expects an array ofBotCommand objects.If Telegram adds new fields to objects that you send as input to Telegram, the corresponding arguments in PTBs classes will be missing.However, any additional data that is not yet covered by the native Python attributes can be passed via the argumentapi_kwargs ofTelegramObject (note thatTelegramObject is the base class of (almost) all other classes in thetelegram package).

For example, imagine that Telegram adds a new field calledemoji to theBotCommand class such that this emoji is shown somewhere in the chat when the command is selected by the user. Then passing the argument viaBotCommand("command", "description", emoji="💥") is possible only after PTB was updated. However, you can already do

BotCommand("command","description",api_kwargs={"emoji":"💥"})

and PTB will pass the data along to Telegram.

Important

The argumentapi_kwargs ofTelegramObject has the same limitations as the argumentapi_kwargs ofBot.do_api_request - seebelow.

Using Bot Methods

New Parameters

All bot methods have a number of parameters defined by Telegram.If Telegram adds new parameters to methods that you can specify, the corresponding arguments in PTBs the methods oftelegram.Bot (and the corresponding shortcuts) will be missing.However, any additional data that is not yet covered by the native Python arguments can be passed via the argumentapi_kwargs of the respective method.

For example, imagine that Telegram adds a new parameter calleddelete_after to thesend_message method such that the message is deleted automatically after the specified time. Then passing the argument viaawait bot.send_message(chat_id=123, text="Hello!", delete_after=42) is possible only after PTB was updated. However, you can already do

awaitbot.send_message(chat_id=123,text="Hello!",api_kwargs={"delete_after":42})

and PTB will pass the data along to Telegram.

Important

The argumentapi_kwargs of bot methods has the same limitations as the argumentapi_kwargs ofBot.do_api_request - seebelow.

New Methods

Sometimes, Telegram adds entirely new methods to the API that you can use to trigger novel functionality.If Telegram adds such a new method, the corresponding method of PTBs classtelegram.Bot will be missing.However, PTB provides the methodBot.do_api_request that allows you to make any API call to Telegram.

For example, imagine that Telegram adds a new method calledget_message allowing your bot to fetch information about a specific method. Then calling that method viaawait bot.get_message(chat_id=123, message_id=456) is possible only after PTB was updated. However, you can already do

awaitbot.do_api_request(endpoint="get_message",api_kwargs={"message_id":456,"chat_id":123},return_type=Message)

and PTB will call the corresponding Bot API method with the given parameters and return the result as aMessage object.

Important

The argumentapi_kwargs will always accept a dictionary with string keys. Note that the type of the values should in general be either

  • JSON serializable (e.g.str,int,list/tuple ordict isstr keys) or
  • a object of typeTelegramObject (or a subclass) or
  • adatetime.datetime object or
  • anInputFile object

Otherwise, PTB will not be able to correctly passapi_kwargs along to Telegram, or Telegram by not be able to parse the input.For more details on the limitations ofapi_kwargs, seehere.

Wiki ofpython-telegram-bot © Copyright 2015-2025 – Licensed byCreative Commons

Must read

  1. Introduction to the API
  2. Tutorial: Your first bot
  3. FAQ
  4. How to ask good questions
  5. How to write an MWE

Concepts & Important Elements

  1. Architecture Overview
  2. Builder Pattern forApplication
  3. Types of Handlers
  4. Working with Files and Media
  5. Exceptions, Warnings and Logging
  6. Concurrency in PTB

Notable Features

  1. Advanced Filters
  2. Storing data
  3. Making your bot persistent
  4. Adding Defaults
  5. Job Queue
  6. Arbitrarycallback_data
  7. Avoiding flood limits
  8. Webhooks
  9. Bot API Forward Compatiblity

Code Resources

  1. Frequently requested design patterns
  2. Code snippets
  3. Performance Optimizations
  4. Telegram Passport
  5. Bots built with PTB
  6. Automated Bot Tests

Examples explained

  1. InlineKeyboard Example

Networking

  1. Working Behind a Proxy
  2. Handling network errors

Other resources

  1. Where to host Telegram Bots
  2. How to host your bot
  3. Local API Server
  4. Type Checking with PTB
  5. Press
  6. Notes on GAE
  7. Related Projects
  8. Emoji

Transition Guides

Administration

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp