-
Notifications
You must be signed in to change notification settings - Fork 0
Adding defaults to your bot
Bibo-Joshi edited this page Nov 7, 2020
·
11 revisions
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.
parse_mode
disable_notification
disable_web_page_preview
allow_sending_without_reply
timeout
quote
tzinfo
Here is a show case for setting parse_mode
to ParseMode.HTML
and tzinfo
to pytz.timezone('Europe/Berlin')
by default:
import pytz
import datetime as dtm
from telegram import ParseMode
from telegram.ext import Updater, MessageHandler, Filters, Defaults
def job(context):
chat_id = context.job.context
local_now = dtm.datetime.now(context.bot.defaults.tzinfo)
utc_now = dtm.datetime.utcnow()
text = 'Running job at {} in timezone {}, which equals {} UTC.'.format(
local_now, context.bot.defaults.tzinfo, utc_now
)
context.bot.send_message(chat_id=chat_id, text=text)
def echo(update, context):
# Send with default parse mode
update.message.reply_text('<b>{}</b>'.format(update.message.text))
# Override default parse mode locally
update.message.reply_text('*{}*'.format(update.message.text), parse_mode=ParseMode.MARKDOWN)
update.message.reply_text('*{}*'.format(update.message.text), parse_mode=None)
# Schedule job
context.job_queue.run_once(job, dtm.datetime.now() + dtm.timedelta(seconds=1),
context=update.effective_chat.id)
def main():
"""Instantiate a Defaults object"""
defaults = Defaults(parse_mode=ParseMode.HTML, tzinfo=pytz.timezone('Europe/Berlin'))
"""Start the bot."""
updater = Updater("TOKEN", use_context=True, defaults=defaults)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on non command text message - echo the message on Telegram
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))
# Start the Bot
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
- Wiki of
python-telegram-bot
© Copyright 2015-2025 – Licensed by Creative Commons
- Architecture Overview
- Builder Pattern for
Application
- Types of Handlers
- Working with Files and Media
- Exceptions, Warnings and Logging
- Concurrency in PTB
- Advanced Filters
- Storing data
- Making your bot persistent
- Adding Defaults
- Job Queue
- Arbitrary
callback_data
- Avoiding flood limits
- Webhooks
- Bot API Forward Compatiblity
- Frequently requested design patterns
- Code snippets
- Performance Optimizations
- Telegram Passport
- Bots built with PTB
- Automated Bot Tests