mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-26 22:40:24 +01:00
Page:
Adding defaults to your bot
Pages
Adding defaults to your bot
Arbitrary callback_data
Architecture
Ask Right
Ask Support
Avoiding flood limits
Bot API Forward Compatibility
Bots built with PTB
Builder Pattern
Code snippets
Concurrency
Emoji
Examples
Exception Handling
Exceptions, Warnings and Logging
Extensions Advanced Filters
Extensions JobQueue
Extensions Your first Bot
Extensions – Advanced Filters
Extensions – JobQueue
Extensions – Your first Bot
Frequently Asked Questions
Frequently requested design patterns
Handling network errors
Home
Hosting your bot
InlineKeyboard Example
Internal test bots
Introduction to the API
Local Bot API Server
MWE
Making your bot persistent
Notes about GAE Google App Engine
PTB test writing knowledge base
Performance Optimizations
Press
Project Bots, Groups and Channels
Related Projects
Storing bot, user and chat related data
Storing user and chat related data
Telegram Passport
Transition guide to Version 12.0
Transition guide to Version 13.0
Transition guide to Version 20.0
Transition guide to Version 4.0
Transition guide to Version 5.0
Type Checking
Types of Handlers
Webhooks
Where to host Telegram Bots
Working Behind a Proxy
Working with Files and Media
Writing Tests
No results
10
Adding defaults to your bot
Harshil edited this page 2024-07-03 23:14:08 -04:00
Table of Contents
Introduction
As of version 12.4, PTB supports passing default values for arguments such as parse_mode
to reduce the need for repetition. For this purpose, the Defaults class was introduced. This makes it possible to set defaults for often used arguments. These are set at the creation of the bot and are immutable.
What can be set to a default
parse_mode
disable_notification
disable_web_page_preview
allow_sending_without_reply
do_quote
tzinfo
block
protect_content
link_preview_options
Example
Here is a show case for setting parse_mode
to ParseMode.HTML
and tzinfo
to pytz.timezone('Europe/Berlin')
by default:
import logging
import pytz
import datetime as dtm
from telegram.constants import ParseMode
from telegram.ext import MessageHandler, filters, Defaults, Application
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
async def job(context):
chat_id = context.job.chat_id
timezone = context.bot.defaults.tzinfo
local_now = dtm.datetime.now(timezone)
utc_now = dtm.datetime.utcnow()
text = f'Running job at {local_now} in timezone {timezone}, which equals {utc_now} UTC.'
await context.bot.send_message(chat_id=chat_id, text=text)
async def echo(update, context):
text = update.message.text
# Send with default parse mode
await update.message.reply_text(f'<b>{text}</b>')
# Override default parse mode locally
await update.message.reply_text(f'*{text}*', parse_mode=ParseMode.MARKDOWN)
# Send with no parse mode
await update.message.reply_text(f'*{text}*', parse_mode=None)
# Schedule job
context.job_queue.run_once(
job, dtm.datetime.now() + dtm.timedelta(seconds=1), chat_id=update.effective_chat.id
)
def main():
"""Instantiate a Defaults object"""
defaults = Defaults(parse_mode=ParseMode.HTML, tzinfo=pytz.timezone('Europe/Berlin'))
application = (
Application.builder()
.token("TOKEN")
.defaults(defaults)
.build()
)
# on non command text message - echo the message on Telegram
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
# Start the Bot
application.run_polling()
if __name__ == '__main__':
main()
Must read
Concepts & Important Elements
- Architecture Overview
- Builder Pattern for
Application
- Types of Handlers
- Working with Files and Media
- Exceptions, Warnings and Logging
- Concurrency in PTB
Notable Features
- Advanced Filters
- Storing data
- Making your bot persistent
- Adding Defaults
- Job Queue
- Arbitrary
callback_data
- Avoiding flood limits
- Webhooks
- Bot API Forward Compatiblity
Code Resources
- Frequently requested design patterns
- Code snippets
- Performance Optimizations
- Telegram Passport
- Bots built with PTB
- Automated Bot Tests
Examples explained
Networking
Other resources
- Where to host Telegram Bots
- How to host your bot
- Local API Server
- Type Checking with PTB
- Press
- Notes on GAE
- Related Projects
- Emoji
Transition Guides
Administration
- Wiki of
python-telegram-bot
© Copyright 2015-2024 – Licensed by Creative Commons