2021-09-22 16:49:10 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2022-01-03 09:09:03 +01:00
|
|
|
# Copyright (C) 2015-2022
|
2021-09-22 16:49:10 +02:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
|
|
|
"""This module contains convenience helper functions.
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionchanged:: 20.0
|
2021-09-22 16:49:10 +02:00
|
|
|
Previously, the contents of this module were available through the (no longer existing)
|
|
|
|
module ``telegram.utils.helpers``.
|
|
|
|
"""
|
|
|
|
|
2021-12-05 09:42:14 +01:00
|
|
|
__all__ = (
|
2022-05-05 17:40:22 +02:00
|
|
|
"create_deep_linked_url",
|
|
|
|
"effective_message_type",
|
|
|
|
"escape_markdown",
|
|
|
|
"mention_html",
|
|
|
|
"mention_markdown",
|
2021-12-05 09:42:14 +01:00
|
|
|
)
|
|
|
|
|
2021-09-22 16:49:10 +02:00
|
|
|
import re
|
|
|
|
from html import escape
|
2022-05-05 09:27:54 +02:00
|
|
|
from typing import TYPE_CHECKING, Optional, Union
|
2021-09-22 16:49:10 +02:00
|
|
|
|
2021-10-19 18:28:19 +02:00
|
|
|
from telegram.constants import MessageType
|
|
|
|
|
2021-09-22 16:49:10 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from telegram import Message, Update
|
|
|
|
|
|
|
|
|
|
|
|
def escape_markdown(text: str, version: int = 1, entity_type: str = None) -> str:
|
|
|
|
"""Helper function to escape telegram markup symbols.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
text (:obj:`str`): The text.
|
|
|
|
version (:obj:`int` | :obj:`str`): Use to specify the version of telegrams Markdown.
|
|
|
|
Either ``1`` or ``2``. Defaults to ``1``.
|
2022-04-24 12:38:09 +02:00
|
|
|
entity_type (:obj:`str`, optional): For the entity types
|
|
|
|
:tg-const:`telegram.MessageEntity.PRE`, :tg-const:`telegram.MessageEntity.CODE` and
|
|
|
|
the link part of :tg-const:`telegram.MessageEntity.TEXT_LINK`, only certain characters
|
|
|
|
need to be escaped in :tg-const:`telegram.constants.ParseMode.MARKDOWN_V2`.
|
2021-09-22 16:49:10 +02:00
|
|
|
See the official API documentation for details. Only valid in combination with
|
|
|
|
``version=2``, will be ignored else.
|
|
|
|
"""
|
|
|
|
if int(version) == 1:
|
2022-05-05 17:40:22 +02:00
|
|
|
escape_chars = r"_*`["
|
2021-09-22 16:49:10 +02:00
|
|
|
elif int(version) == 2:
|
2022-05-05 17:40:22 +02:00
|
|
|
if entity_type in ["pre", "code"]:
|
|
|
|
escape_chars = r"\`"
|
|
|
|
elif entity_type == "text_link":
|
|
|
|
escape_chars = r"\)"
|
2021-09-22 16:49:10 +02:00
|
|
|
else:
|
2022-05-05 17:40:22 +02:00
|
|
|
escape_chars = r"_*[]()~`>#+-=|{}.!"
|
2021-09-22 16:49:10 +02:00
|
|
|
else:
|
2022-05-05 17:40:22 +02:00
|
|
|
raise ValueError("Markdown version must be either 1 or 2!")
|
2021-09-22 16:49:10 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
return re.sub(f"([{re.escape(escape_chars)}])", r"\\\1", text)
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def mention_html(user_id: Union[int, str], name: str) -> str:
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
user_id (:obj:`int`): The user's id which you want to mention.
|
|
|
|
name (:obj:`str`): The name the mention is showing.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`str`: The inline mention for the user as HTML.
|
|
|
|
"""
|
|
|
|
return f'<a href="tg://user?id={user_id}">{escape(name)}</a>'
|
|
|
|
|
|
|
|
|
|
|
|
def mention_markdown(user_id: Union[int, str], name: str, version: int = 1) -> str:
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
user_id (:obj:`int`): The user's id which you want to mention.
|
|
|
|
name (:obj:`str`): The name the mention is showing.
|
|
|
|
version (:obj:`int` | :obj:`str`): Use to specify the version of Telegram's Markdown.
|
|
|
|
Either ``1`` or ``2``. Defaults to ``1``.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`str`: The inline mention for the user as Markdown.
|
|
|
|
"""
|
2022-05-05 17:40:22 +02:00
|
|
|
return f"[{escape_markdown(name, version=version)}](tg://user?id={user_id})"
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
def effective_message_type(entity: Union["Message", "Update"]) -> Optional[str]:
|
2021-09-22 16:49:10 +02:00
|
|
|
"""
|
|
|
|
Extracts the type of message as a string identifier from a :class:`telegram.Message` or a
|
|
|
|
:class:`telegram.Update`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
entity (:class:`telegram.Update` | :class:`telegram.Message`): The ``update`` or
|
|
|
|
``message`` to extract from.
|
|
|
|
|
|
|
|
Returns:
|
2021-10-19 18:28:19 +02:00
|
|
|
:obj:`str` | :obj:`None`: One of :class:`telegram.constants.MessageType` if the entity
|
|
|
|
contains a message that matches one of those types. :obj:`None` otherwise.
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
# Importing on file-level yields cyclic Import Errors
|
2021-10-08 08:17:00 +02:00
|
|
|
from telegram import Message, Update # pylint: disable=import-outside-toplevel
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
if isinstance(entity, Message):
|
|
|
|
message = entity
|
|
|
|
elif isinstance(entity, Update):
|
2021-10-19 18:28:19 +02:00
|
|
|
if not entity.effective_message:
|
|
|
|
return None
|
|
|
|
message = entity.effective_message
|
2021-09-22 16:49:10 +02:00
|
|
|
else:
|
2021-10-19 18:28:19 +02:00
|
|
|
raise TypeError(f"The entity is neither Message nor Update (got: {type(entity)})")
|
2021-09-22 16:49:10 +02:00
|
|
|
|
2021-10-19 18:28:19 +02:00
|
|
|
for message_type in MessageType:
|
|
|
|
if message[message_type]:
|
|
|
|
return message_type
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def create_deep_linked_url(bot_username: str, payload: str = None, group: bool = False) -> str:
|
|
|
|
"""
|
2022-04-24 12:38:09 +02:00
|
|
|
Creates a deep-linked URL for this :paramref:`bot_username` with the specified
|
|
|
|
:paramref:`payload`. See https://core.telegram.org/bots#deep-linking to learn more.
|
2021-09-22 16:49:10 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
The :paramref:`payload` may consist of the following characters: ``A-Z, a-z, 0-9, _, -``
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
Note:
|
|
|
|
Works well in conjunction with
|
2022-04-24 12:38:09 +02:00
|
|
|
``CommandHandler("start", callback, filters=filters.Regex('payload'))``
|
2021-09-22 16:49:10 +02:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
``create_deep_linked_url(bot.get_me().username, "some-params")``
|
|
|
|
|
|
|
|
Args:
|
|
|
|
bot_username (:obj:`str`): The username to link to
|
|
|
|
payload (:obj:`str`, optional): Parameters to encode in the created URL
|
|
|
|
group (:obj:`bool`, optional): If :obj:`True` the user is prompted to select a group to
|
|
|
|
add the bot to. If :obj:`False`, opens a one-on-one conversation with the bot.
|
|
|
|
Defaults to :obj:`False`.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`str`: An URL to start the bot with specific parameters
|
|
|
|
"""
|
|
|
|
if bot_username is None or len(bot_username) <= 3:
|
|
|
|
raise ValueError("You must provide a valid bot_username.")
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
base_url = f"https://t.me/{bot_username}"
|
2021-09-22 16:49:10 +02:00
|
|
|
if not payload:
|
|
|
|
return base_url
|
|
|
|
|
|
|
|
if len(payload) > 64:
|
|
|
|
raise ValueError("The deep-linking payload must not exceed 64 characters.")
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
if not re.match(r"^[A-Za-z0-9_-]+$", payload):
|
2021-09-22 16:49:10 +02:00
|
|
|
raise ValueError(
|
|
|
|
"Only the following characters are allowed for deep-linked "
|
|
|
|
"URLs: A-Z, a-z, 0-9, _ and -"
|
|
|
|
)
|
|
|
|
|
|
|
|
if group:
|
2022-05-05 17:40:22 +02:00
|
|
|
key = "startgroup"
|
2021-09-22 16:49:10 +02:00
|
|
|
else:
|
2022-05-05 17:40:22 +02:00
|
|
|
key = "start"
|
2021-09-22 16:49:10 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
return f"{base_url}?{key}={payload}"
|