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

Local Bot API Server

Hinrich Mahler edited this pageAug 2, 2024 ·10 revisions

Since Bot API 5.0, Telegram made theBot API server open source, allowing you to host your own instance. For details on what benefits hosting your own instance has and how it works, please see theofficial docs.

Bot API 5.0 (and therefore local API server) is supported by PTB since v13.1.

How to use a local Bot API Server with PTB

  • Before you can move your bot from the official server cloud to a self hosted server, you need to call thelog_out method.
  • Before moving from one self hosted instance to another, you need to use thedelete_webhook andclose methods.
  • To make PTB aware that you're not using the official server, pass the following to yourApplicationBuilder (orBot):
    • base_url='your-bot-api-server.com/bot'

Working with files

  • When running the server with the--local flag
    • you need to setlocal_mode=True
    • get_file will give you the local file path asfile_path. PTB detects that, so thatawait get_file(…).download_to_drive() just returns the local file string instead of downloading it.
    • you can send files by passing'file:///absolute/path/to/file' instead of an URL or a file handle.
    • passing relative paths (without prefix) or even passingpathlib.Path objects is supported as well, even if you're not running inlocal mode, in which case it will be opened and send via upload as usual.
    • To adjust for larger file sizes transmitted between PTB and the local API server, it may be beneficial to extend the timeout values for networking operations. See the wiki pages onhandling networking errors as well as onadding defaults. Additionally, it may be beneficial to not read the complete file into memory before sending it, but rather stream it. This can be achieved by setting the parameterread_file_handle toFalse. See alsothis wiki page.
  • When running the serverwithout the--local flag
    • you need to setbase_file_url='your-bot-api-server.com/file/bot'
    • the Bot API server doesnot automatically serve the files obtained byget_file(), seetelegram-bot-api/#26. So be aware that you have to run a web server which serves them, otherwise you will run into 404 errors.

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