Python Telegram bot API library providing a simple, extensible interface for building Telegram bots with complete Bot API support
—
Update retrieval and processing including polling modes, webhook support, and update filtering with middleware support.
Retrieve updates from Telegram servers using long polling with configurable parameters.
def polling(self, non_stop: bool = False, skip_pending: bool = False,
interval: int = 0, timeout: int = 20,
long_polling_timeout: int = 20,
allowed_updates: Optional[List[str]] = None):
"""
Start polling for updates with configurable behavior.
Parameters:
- non_stop (bool): Continue polling despite API exceptions
- skip_pending (bool): Skip pending updates on startup
- interval (int): Delay between update retrievals
- timeout (int): Request connection timeout
- long_polling_timeout (int): Long polling timeout (see API docs)
- allowed_updates (List[str], optional): Update types to receive
"""
def infinity_polling(self, timeout: int = 20, skip_pending: bool = False,
long_polling_timeout: int = 20, logger_level = logging.ERROR,
allowed_updates: Optional[List[str]] = None, **kwargs):
"""
Infinite polling with automatic exception handling and recovery.
Parameters:
- timeout (int): Request connection timeout
- skip_pending (bool): Skip old updates on start
- long_polling_timeout (int): Long polling timeout
- logger_level: Logging level for infinity polling
- allowed_updates (List[str], optional): Update types to receive
"""
def get_updates(self, offset: Optional[int] = None, limit: Optional[int] = None,
timeout: Optional[int] = 20, allowed_updates: Optional[List[str]] = None,
long_polling_timeout: int = 20) -> List[types.Update]:
"""
Manually retrieve updates using long polling.
Parameters:
- offset (int, optional): Identifier of first update to return
- limit (int, optional): Number of updates to retrieve (1-100)
- timeout (int): Request connection timeout
- allowed_updates (List[str], optional): Update types to receive
- long_polling_timeout (int): Timeout for long polling
Returns:
List[Update]: Array of Update objects
"""
def stop_polling(self):
"""Stop the polling process."""
def stop_bot(self):
"""Stop bot and clean up resources."""Configure webhook endpoints for receiving updates via HTTP POST requests.
def set_webhook(self, url: Optional[str] = None, certificate = None,
max_connections: Optional[int] = None, allowed_updates: Optional[List[str]] = None,
ip_address: Optional[str] = None, drop_pending_updates: Optional[bool] = None,
timeout: Optional[int] = None) -> bool:
"""
Set webhook URL for receiving updates.
Parameters:
- url (str, optional): HTTPS URL for webhook (empty string removes webhook)
- certificate: Public key certificate for self-signed certificates
- max_connections (int, optional): Maximum simultaneous connections (1-100)
- allowed_updates (List[str], optional): Update types to receive
- ip_address (str, optional): Fixed IP address for webhook requests
- drop_pending_updates (bool, optional): Drop all pending updates
- timeout (int, optional): Request timeout
Returns:
bool: True on success
"""
def delete_webhook(self, drop_pending_updates: Optional[bool] = None,
timeout: Optional[int] = None) -> bool:
"""
Remove webhook integration to switch back to getUpdates.
Parameters:
- drop_pending_updates (bool, optional): Drop all pending updates
- timeout (int, optional): Request timeout
Returns:
bool: True on success
"""
def get_webhook_info(self, timeout: Optional[int] = None) -> types.WebhookInfo:
"""
Get current webhook status.
Parameters:
- timeout (int, optional): Request timeout
Returns:
WebhookInfo: Current webhook configuration and status
"""Process individual updates and update batches with handler dispatch.
def process_new_updates(self, updates: List[types.Update]):
"""
Process a list of updates through registered handlers.
Parameters:
- updates (List[Update]): Updates to process
"""
def set_update_listener(self, listener: Callable):
"""
Add listener for all processed updates.
Parameters:
- listener (Callable): Function to call for each update batch
"""import telebot
bot = telebot.TeleBot("YOUR_TOKEN")
@bot.message_handler(commands=['start'])
def start_handler(message):
bot.reply_to(message, "Bot started!")
# Start polling
bot.polling()bot.infinity_polling(
timeout=10,
long_polling_timeout=5,
skip_pending=True,
allowed_updates=['message', 'callback_query']
)# Set webhook
bot.set_webhook(
url="https://yourserver.com/webhook",
max_connections=5,
allowed_updates=['message', 'callback_query']
)
# In your web server:
@app.route('/webhook', methods=['POST'])
def webhook():
json_str = request.get_data().decode('UTF-8')
update = telebot.types.Update.de_json(json_str)
bot.process_new_updates([update])
return 'OK'# Get updates manually
updates = bot.get_updates(timeout=30, long_polling_timeout=25)
# Process updates
for update in updates:
bot.process_new_updates([update])class Update:
update_id: int
message: Optional[Message]
edited_message: Optional[Message]
channel_post: Optional[Message]
edited_channel_post: Optional[Message]
inline_query: Optional[InlineQuery]
chosen_inline_result: Optional[ChosenInlineResult]
callback_query: Optional[CallbackQuery]
shipping_query: Optional[ShippingQuery]
pre_checkout_query: Optional[PreCheckoutQuery]
poll: Optional[Poll]
poll_answer: Optional[PollAnswer]
my_chat_member: Optional[ChatMemberUpdated]
chat_member: Optional[ChatMemberUpdated]
class WebhookInfo:
url: str
has_custom_certificate: bool
pending_update_count: int
ip_address: Optional[str]
last_error_date: Optional[int]
last_error_message: Optional[str]
max_connections: Optional[int]
allowed_updates: Optional[List[str]]Install with Tessl CLI
npx tessl i tessl/pypi-py-telegram-bot-api