mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-02-16 18:31:45 +01:00
Updated Adding defaults to your bot (markdown)
parent
ce03d49fb4
commit
fe3b8e869b
1 changed files with 32 additions and 10 deletions
|
@ -2,19 +2,34 @@
|
||||||
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](https://python-telegram-bot.readthedocs.io/en/stable/telegram.ext.defaults.html) 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_.
|
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](https://python-telegram-bot.readthedocs.io/en/stable/telegram.ext.defaults.html) 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
|
## What can be set to a default
|
||||||
* parse_mode
|
* `parse_mode`
|
||||||
* disable_notification
|
* `disable_notification`
|
||||||
* disable_web_page_preview
|
* `disable_web_page_preview`
|
||||||
* timeout
|
* `timeout`
|
||||||
* quote
|
* `quote`
|
||||||
|
* `tzinfo`
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
Here is a show case for setting `parse_mode` to `ParseMode.HTML` by default:
|
Here is a show case for setting `parse_mode` to `ParseMode.HTML` and `tzinfo` to `pytz.timezone('Europe/Berlin')` by default:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
import pytz
|
||||||
|
import datetime as dtm
|
||||||
|
|
||||||
from telegram import ParseMode
|
from telegram import ParseMode
|
||||||
from telegram.ext import Updater, MessageHandler, Filters, Defaults
|
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):
|
def echo(update, context):
|
||||||
# Send with default parse mode
|
# Send with default parse mode
|
||||||
update.message.reply_text('<b>{}</b>'.format(update.message.text))
|
update.message.reply_text('<b>{}</b>'.format(update.message.text))
|
||||||
|
@ -22,9 +37,14 @@ def echo(update, context):
|
||||||
update.message.reply_text('*{}*'.format(update.message.text), parse_mode=ParseMode.MARKDOWN)
|
update.message.reply_text('*{}*'.format(update.message.text), parse_mode=ParseMode.MARKDOWN)
|
||||||
update.message.reply_text('*{}*'.format(update.message.text), parse_mode=None)
|
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():
|
def main():
|
||||||
"""Instanciate a Defaults object"""
|
"""Instantiate a Defaults object"""
|
||||||
defaults = Defaults(parse_mode=ParseMode.HTML)
|
defaults = Defaults(parse_mode=ParseMode.HTML, tzinfo=pytz.timezone('Europe/Berlin'))
|
||||||
|
|
||||||
"""Start the bot."""
|
"""Start the bot."""
|
||||||
updater = Updater("TOKEN", use_context=True, defaults=defaults)
|
updater = Updater("TOKEN", use_context=True, defaults=defaults)
|
||||||
|
@ -32,13 +52,15 @@ def main():
|
||||||
# Get the dispatcher to register handlers
|
# Get the dispatcher to register handlers
|
||||||
dp = updater.dispatcher
|
dp = updater.dispatcher
|
||||||
|
|
||||||
# on noncommand i.e message - echo the message on Telegram
|
# on non command text message - echo the message on Telegram
|
||||||
dp.add_handler(MessageHandler(Filters.text & ~Filters.command(only_start=true), echo))
|
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))
|
||||||
|
|
||||||
# Start the Bot
|
# Start the Bot
|
||||||
updater.start_polling()
|
updater.start_polling()
|
||||||
updater.idle()
|
updater.idle()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
||||||
```
|
```
|
Loading…
Add table
Reference in a new issue