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

Transition guide to Version 4.0

Harshil edited this pageMar 25, 2023 ·12 revisions

What's new?

All changes can also be reviewed in ourofficial documentation!

Dispatcher

For users coming from RC release predating 26th of April, 2016:

  • Changes in "filters" syntax (upper/lower case)
  • Handler groups are now identified byint only, and are processed in order, smallest first.

TheDispatcher class has now a cleaner interface and more precise Message filtering. Instead of many methods with long names likeDispatcher.addTelegramMessageHandler(handler), we now only have two of those methods:

add_handler(handler, group=0)

Register a handler.

TL;DR: Order and priority counts. 0 or 1 handlers per group will be used.

A handler must be an instance of a subclass oftelegram.ext.Handler. All handlers are organized in groups with a numeric value. The default group is0. All groups will be evaluated for handling an update, but only 0 or 1 handler per group will be used.

The priority/order of handlers is determined as follows:

  • Priority of the group (lower group number == higher priority)
  • The first handler in a group which should handle an update will be used. Other handlers from the group will not be used. The order in which handlers were added to the group defines the priority.
Parameters:

handler (Handler) - A Handler instance
group (optional[int]) - The group identifier. Default is0

add_error_handler(callback)

This method remains unchanged, only the name has been changed to snake_case.

So, theadd_handler method is accepting an object of a subclass oftelegram.ext.Handler. Let's see how that looks in real life:

fromtelegram.extimportMessageHandler,Filtersdeftext_callback(bot,update):print("New text message: "+update.message.text)dispatcher.add_handler(MessageHandler([Filters.text],text_callback))

As you can see here, theMessageHandler class is one of the includedHandler subclasses. All that was possible before is still possible, but now more organized and more explicit. Lets take a quick look at another handler class, theRegexHandler:

fromtelegram.extimportRegexHandlerdefname_callback(bot,update,groupdict):print("The name of the user is: "+groupdict['name'])name_regex=r'My name is (?P<name>.*)'dispatcher.add_handler(RegexHandler(name_regex,text_callback,pass_groupdict=True))

Here you can see the optional argumentgroupdict passed to the handler callback function. Note that it is necessary to specify this explicitly when creating theHandler object.

Other changes

  • You can easily implement your own handlers. Just subclasstelegram.ext.handler and take a look at the implementation of the provided handlers.
  • Instead ofaddTelegramInlineHandler there are nowInlineHandler,ChosenInlineResultHandler andCallbackQueryHandler
  • There is no replacement foraddUnknownTelegramCommandHandler. Instead, it is recommended to useRegexHandler(r'/.*', ...) and add it as the last handler
  • TheUpdateQueue class andcontext parameters have been removed

Bot API 2.0

Please read the documentation of theTelegram Bot API to learn about all the new things in version 2 of the bot API. This section covers only those changes that are not backwards compatible and not listed in theRecent Changes list.

  • new_chat_participant andleft_chat_participant of theMessage class are nownew_chat_member andleft_chat_member
  • The following parameters onInlineResult andInlineQueryResult objects are removed in favor ofInlineMessageContent:
  • message_text
  • parse_mode
  • disable_web_page_preview
  • InInlineQueryResultPhoto the parametermime_type has been removed. JPEG is now required.
  • ReplyKeyboardMarkup now takes a list of a list ofKeyboardButton instead of strings.
  • From v4.0.2 you can usestr again

Thetelegram.ext module

The classesUpdater,Dispatcher andJobQueue that were previously available for import directlyfrom telegram are now located intelegram.ext.

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