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
Iulian Onofrei edited this pageMay 15, 2023 ·10 revisions

The Builder Pattern intelegram.ext

In this library, there are roughly four important components that make up everything:

  1. TheUpdater is responsible for fetching updates that Telegram sent to your bot
  2. TheBot provides high-level access to the methods of the Bot API
  3. TheBaseRequest is responsible to handle the actual networking stuff, i.e. sending the requests to the Bot API
  4. TheApplication binds everything together and is responsible for handling the updates fetched by theUpdater.

In addition to those four, there are several other components, which are not as significant for the structure of apython-telegram-bot program.

All of those components have different parameters. Some of them are optional. Some are required. Some are mutually exclusive.That's a lot to take in and when coding your bot and setting this all up by yourself would be tiresome.

That's whypython-telegram-bot makes an effort to make the setup easy with reasonable defaults.For example, after running

fromtelegram.extimportApplicationapplication=Application.builder().token('TOKEN').build()

you will automatically have

  • theUpdater available asapplication.updater
  • theBot available asapplication.bot orapplication.updater.bot (both are the same object)
  • aBaseRequest object initialized and ready to be used by theapplication.bot
  • several other components & sane default values set up.

But what if you want to customize some arguments thatApplication,Updater,Bot,BaseRequest or other components accept? Do you have to build all those objects yourself and glue them together? No! (Well, you can, but you don't have to.)

This is where thebuilder pattern comes into play. The idea is roughly as follows: You went shopping and have all the ingredients for a nice stew, but you don't want to cook yourself. So you hand everything to a chef. The chef will tell you that some of your ingredients don't match and will discard them. Afterwards, he'll cook a nice stew for you and you never need to worry about how exactly that's done.

Let's get a bit more technical. First, we need the cook:

fromtelegram.extimportApplicationbuilder=Application.builder()

Now, we hand over the ingredients:

builder.token(token)# the bot token is the main ingredientbuilder.context_types(context_types)# In case you want to use custom context types for your `Application`builder.read_timeout(read_timeout)# In case you want to fine tune the networking backend...

Finally, we have the chef cook the stew:

application=builder.build()

All this can also be chained into a single line:

fromtelegram.extimportApplicationapplication=Application.builder().token(token).context_types(context_types).read_timeout(read_timeout).build()

And that's already it!

The docs ofApplicationBuilder have all the info about which "ingredients" it can handle, i.e. which methods it has. Each method will tell you

  • how the parameters will be used (e.g. the token passed toApplicationBuilder.token will be used for theBot available asApplication.bot)
  • What happens if you don't call this method. For most things, PTB will use reasonable defaults.

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