Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warning to install optional dependencies #3393

Merged
merged 11 commits into from Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/source/examples.rst
Expand Up @@ -40,7 +40,7 @@ up a job to send a message to that user after 30 seconds. The user can
also cancel the timer by sending ``/unset``. To learn more about the
``JobQueue``, read `this wiki
article <https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions-%E2%80%93-JobQueue>`__.
Note: To use JobQueue, you must install ptb via ``pip install python-telegram-bot[job-queue]``
Note: To use ``JobQueue``, you must install PTB via ``pip install python-telegram-bot[job-queue]``

:any:`examples.conversationbot`
-------------------------------
Expand Down Expand Up @@ -116,7 +116,7 @@ Don’t forget to enable and configure payments with
`@BotFather <https://telegram.me/BotFather>`_. Check out this
`guide <https://github.com/python-telegram-bot/python-telegram-bot/wiki/Telegram-Passport>`__
on Telegram passports in PTB.
Note: To use telegram passport, you must install ptb via ``pip install python-telegram-bot[passport]``
Note: To use Telegram Passport, you must install PTB via ``pip install python-telegram-bot[passport]``

:any:`examples.paymentbot`
--------------------------
Expand Down Expand Up @@ -164,7 +164,7 @@ combination with ``telegram.ext.Application``.

This example showcases how PTBs “arbitrary callback data” feature can be
used.
Note: To use arbitrary callback data, you must install ptb via ``pip install python-telegram-bot[callback-data]``
Note: To use arbitrary callback data, you must install PTB via ``pip install python-telegram-bot[callback-data]``

Pure API
--------
Expand Down
3 changes: 2 additions & 1 deletion examples/arbitrarycallbackdatabot.py
Expand Up @@ -8,7 +8,8 @@
https://github.com/python-telegram-bot/python-telegram-bot/wiki/Arbitrary-callback_data

Note:
To use arbitrary callback data, you must install ptb via `pip install python-telegram-bot[callback-data]`
To use arbitrary callback data, you must install PTB via
`pip install python-telegram-bot[callback-data]`
"""
import logging
from typing import List, Tuple, cast
Expand Down
3 changes: 2 additions & 1 deletion examples/passportbot.py
Expand Up @@ -11,7 +11,8 @@
for how to use Telegram Passport properly with python-telegram-bot.

Note:
To use telegram passport, you must install PTB via `pip install python-telegram-bot[passport]`
To use Telegram Passport, you must install PTB via
`pip install python-telegram-bot[passport]`
"""
import logging
from pathlib import Path
Expand Down
3 changes: 2 additions & 1 deletion examples/timerbot.py
Expand Up @@ -18,7 +18,8 @@
bot.

Note:
To use arbitrary callback data you must install ptb via `pip install python-telegram-bot[callback-data]`
To use arbitrary callback data, you must install ptb via
`pip install python-telegram-bot[callback-data]`
"""

import logging
Expand Down
30 changes: 23 additions & 7 deletions telegram/ext/_application.py
Expand Up @@ -64,7 +64,7 @@

if TYPE_CHECKING:
from telegram import Message
from telegram.ext import ConversationHandler
from telegram.ext import ConversationHandler, JobQueue
from telegram.ext._applicationbuilder import InitApplicationBuilder
from telegram.ext._jobqueue import Job

Expand Down Expand Up @@ -218,6 +218,7 @@ class Application(Generic[BT, CCT, UD, CD, BD, JQ], AbstractAsyncContextManager)
"_concurrent_updates_sem",
"_conversation_handler_conversations",
"_initialized",
"_job_queue",
"_running",
"_user_data",
"_user_ids_to_be_deleted_in_persistence",
Expand All @@ -228,7 +229,6 @@ class Application(Generic[BT, CCT, UD, CD, BD, JQ], AbstractAsyncContextManager)
"context_types",
"error_handlers",
"handlers",
"job_queue",
"persistence",
"post_init",
"post_shutdown",
Expand Down Expand Up @@ -264,7 +264,6 @@ def __init__(

self.bot = bot
self.update_queue = update_queue
self.job_queue = job_queue
self.context_types = context_types
self.updater = updater
self.handlers: Dict[int, List[BaseHandler]] = {}
Expand Down Expand Up @@ -306,6 +305,7 @@ def __init__(
# A number of low-level helpers for the internal logic
self._initialized = False
self._running = False
self._job_queue = job_queue
self.__update_fetcher_task: Optional[asyncio.Task] = None
self.__update_persistence_task: Optional[asyncio.Task] = None
self.__update_persistence_event = asyncio.Event()
Expand Down Expand Up @@ -337,6 +337,22 @@ def concurrent_updates(self) -> int:
"""
return self._concurrent_updates

@property
def job_queue(self) -> Optional["JobQueue"]:
"""
:class:`telegram.ext.JobQueue`: The :class:`JobQueue` used by the
clot27 marked this conversation as resolved.
Show resolved Hide resolved
:class:`telegram.ext.Application`.

.. seealso:: `Job Queue <https://github.com/python-telegram-bot/
python-telegram-bot/wiki/Extensions-%E2%80%93-JobQueue>`_
"""
if self._job_queue is None:
warn(
"No `JobQueue` set up. To use `JobQueue`, you must install PTB via "
"`pip install python-telegram-bot[job_queue]`."
)
return self._job_queue

async def initialize(self) -> None:
"""Initializes the Application by initializing:

Expand Down Expand Up @@ -511,8 +527,8 @@ async def start(self) -> None:
)
_logger.debug("Loop for updating persistence started")

if self.job_queue:
await self.job_queue.start() # type: ignore[union-attr]
if self._job_queue:
await self._job_queue.start() # type: ignore[union-attr]
_logger.debug("JobQueue started")

self.__update_fetcher_task = asyncio.create_task(
Expand Down Expand Up @@ -561,9 +577,9 @@ async def stop(self) -> None:
await self.__update_fetcher_task
_logger.debug("Application stopped fetching of updates.")

if self.job_queue:
if self._job_queue:
_logger.debug("Waiting for running jobs to finish")
await self.job_queue.stop(wait=True) # type: ignore[union-attr]
await self._job_queue.stop(wait=True) # type: ignore[union-attr]
_logger.debug("JobQueue stopped")

_logger.debug("Waiting for `create_task` calls to be processed")
Expand Down
4 changes: 2 additions & 2 deletions telegram/ext/_callbackcontext.py
Expand Up @@ -21,8 +21,8 @@

from telegram._callbackquery import CallbackQuery
from telegram._update import Update
from telegram.ext._extbot import ExtBot
from telegram._utils.warnings import warn
from telegram.ext._extbot import ExtBot
from telegram.ext._utils.types import BD, BT, CD, UD

if TYPE_CHECKING:
Expand Down Expand Up @@ -392,7 +392,7 @@ def job_queue(self) -> Optional["JobQueue"]:
"""
if self._application.job_queue is None:
warn(
"No `JobQueue` Set up. To use `JobQueue`, you must install PTB via"
"No `JobQueue` set up. To use `JobQueue`, you must install PTB via "
"`pip install python-telegram-bot[job_queue]`."
)
return self._application.job_queue
Expand Down
15 changes: 9 additions & 6 deletions telegram/ext/_conversationhandler.py
Expand Up @@ -675,12 +675,15 @@ def _schedule_job(

try:
# both job_queue & conversation_timeout are checked before calling _schedule_job
j_queue = application.job_queue
self.timeout_jobs[conversation_key] = j_queue.run_once(
self._trigger_timeout,
self.conversation_timeout, # type: ignore[arg-type]
data=_ConversationTimeoutContext(conversation_key, update, application, context),
)
if application.job_queue:
j_queue = application.job_queue
self.timeout_jobs[conversation_key] = j_queue.run_once(
self._trigger_timeout,
self.conversation_timeout, # type: ignore[arg-type]
data=_ConversationTimeoutContext(
conversation_key, update, application, context
),
)
Bibo-Joshi marked this conversation as resolved.
Show resolved Hide resolved
except Exception as exc:
_logger.exception("Failed to schedule timeout.", exc_info=exc)

Expand Down