2016-10-03 20:09:57 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2023-01-01 21:31:29 +01:00
|
|
|
# Copyright (C) 2015-2023
|
2016-10-03 20:09:57 +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/].
|
2016-10-17 00:22:40 +02:00
|
|
|
"""This module contains an object that represents a Telegram Game."""
|
2016-10-03 20:09:57 +02:00
|
|
|
import sys
|
2022-12-15 15:00:36 +01:00
|
|
|
from typing import TYPE_CHECKING, Dict, List, Optional, Sequence
|
2016-10-03 20:09:57 +02:00
|
|
|
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram._files.animation import Animation
|
|
|
|
from telegram._files.photosize import PhotoSize
|
|
|
|
from telegram._messageentity import MessageEntity
|
|
|
|
from telegram._telegramobject import TelegramObject
|
2022-12-15 15:00:36 +01:00
|
|
|
from telegram._utils.argumentparsing import parse_sequence_arg
|
2021-10-10 15:10:21 +02:00
|
|
|
from telegram._utils.types import JSONDict
|
2020-10-09 17:22:07 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from telegram import Bot
|
2016-10-03 20:09:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Game(TelegramObject):
|
2017-07-23 22:33:08 +02:00
|
|
|
"""
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
This object represents a game. Use `BotFather <https://t.me/BotFather>`_ to create and edit
|
|
|
|
games, their short names will act as unique identifiers.
|
2016-10-03 20:09:57 +02:00
|
|
|
|
2020-07-14 21:33:56 +02:00
|
|
|
Objects of this class are comparable in terms of equality. Two objects of this class are
|
|
|
|
considered equal, if their :attr:`title`, :attr:`description` and :attr:`photo` are equal.
|
|
|
|
|
2020-12-30 15:59:50 +01:00
|
|
|
Args:
|
2017-07-23 22:33:08 +02:00
|
|
|
title (:obj:`str`): Title of the game.
|
|
|
|
description (:obj:`str`): Description of the game.
|
2022-12-15 15:00:36 +01:00
|
|
|
photo (Sequence[:class:`telegram.PhotoSize`]): Photo that will be displayed in the game
|
|
|
|
message in chats.
|
|
|
|
|
|
|
|
.. versionchanged:: 20.0
|
|
|
|
|sequenceclassargs|
|
|
|
|
|
2020-12-30 15:59:50 +01:00
|
|
|
text (:obj:`str`, optional): Brief description of the game or high scores included in the
|
2017-07-23 22:33:08 +02:00
|
|
|
game message. Can be automatically edited to include current high scores for the game
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
when the bot calls :meth:`telegram.Bot.set_game_score`, or manually edited
|
|
|
|
using :meth:`telegram.Bot.edit_message_text`.
|
2022-11-22 11:07:42 +01:00
|
|
|
0-:tg-const:`telegram.constants.MessageLimit.MAX_TEXT_LENGTH` characters.
|
2022-12-15 15:00:36 +01:00
|
|
|
text_entities (Sequence[:class:`telegram.MessageEntity`], optional): Special entities that
|
2016-10-04 01:13:24 +02:00
|
|
|
appear in text, such as usernames, URLs, bot commands, etc.
|
2022-12-15 15:00:36 +01:00
|
|
|
|
|
|
|
.. versionchanged:: 20.0
|
|
|
|
|sequenceclassargs|
|
|
|
|
|
2020-12-30 15:59:50 +01:00
|
|
|
animation (:class:`telegram.Animation`, optional): Animation that will be displayed in the
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
game message in chats. Upload via `BotFather <https://t.me/BotFather>`_.
|
2016-10-04 01:13:24 +02:00
|
|
|
|
2020-12-30 15:59:50 +01:00
|
|
|
Attributes:
|
2017-07-23 22:33:08 +02:00
|
|
|
title (:obj:`str`): Title of the game.
|
|
|
|
description (:obj:`str`): Description of the game.
|
2022-12-15 15:00:36 +01:00
|
|
|
photo (Tuple[:class:`telegram.PhotoSize`]): Photo that will be displayed in the game
|
|
|
|
message in chats.
|
|
|
|
|
|
|
|
.. versionchanged:: 20.0
|
|
|
|
|tupleclassattrs|
|
|
|
|
|
2020-12-30 15:59:50 +01:00
|
|
|
text (:obj:`str`): Optional. Brief description of the game or high scores included in the
|
2017-07-23 22:33:08 +02:00
|
|
|
game message. Can be automatically edited to include current high scores for the game
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
when the bot calls :meth:`telegram.Bot.set_game_score`, or manually edited
|
|
|
|
using :meth:`telegram.Bot.edit_message_text`.
|
2023-01-01 16:24:00 +01:00
|
|
|
0-:tg-const:`telegram.constants.MessageLimit.MAX_TEXT_LENGTH` characters.
|
2022-12-15 15:00:36 +01:00
|
|
|
text_entities (Tuple[:class:`telegram.MessageEntity`]): Optional. Special entities that
|
2017-07-23 22:33:08 +02:00
|
|
|
appear in text, such as usernames, URLs, bot commands, etc.
|
2023-01-01 14:24:30 +01:00
|
|
|
This tuple is empty if the message does not contain text entities.
|
2022-12-15 15:00:36 +01:00
|
|
|
|
|
|
|
.. versionchanged:: 20.0
|
|
|
|
|tupleclassattrs|
|
|
|
|
|
2020-12-30 15:59:50 +01:00
|
|
|
animation (:class:`telegram.Animation`): Optional. Animation that will be displayed in the
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
game message in chats. Upload via `BotFather <https://t.me/BotFather>`_.
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2016-10-04 01:13:24 +02:00
|
|
|
"""
|
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = (
|
|
|
|
"title",
|
|
|
|
"photo",
|
|
|
|
"description",
|
|
|
|
"text_entities",
|
|
|
|
"text",
|
|
|
|
"animation",
|
|
|
|
)
|
|
|
|
|
2016-10-04 01:13:24 +02:00
|
|
|
def __init__(
|
2020-11-05 18:12:01 +01:00
|
|
|
self,
|
2020-10-06 19:28:40 +02:00
|
|
|
title: str,
|
|
|
|
description: str,
|
2022-12-15 15:00:36 +01:00
|
|
|
photo: Sequence[PhotoSize],
|
2020-10-06 19:28:40 +02:00
|
|
|
text: str = None,
|
2022-12-15 15:00:36 +01:00
|
|
|
text_entities: Sequence[MessageEntity] = None,
|
2020-10-06 19:28:40 +02:00
|
|
|
animation: Animation = None,
|
2022-10-07 11:51:53 +02:00
|
|
|
*,
|
|
|
|
api_kwargs: JSONDict = None,
|
2020-10-06 19:28:40 +02:00
|
|
|
):
|
2022-10-07 11:51:53 +02:00
|
|
|
super().__init__(api_kwargs=api_kwargs)
|
2020-07-14 21:33:56 +02:00
|
|
|
# Required
|
2016-10-03 20:09:57 +02:00
|
|
|
self.title = title
|
|
|
|
self.description = description
|
2022-12-15 15:00:36 +01:00
|
|
|
self.photo = parse_sequence_arg(photo)
|
2020-07-14 21:33:56 +02:00
|
|
|
# Optionals
|
2016-10-04 01:13:24 +02:00
|
|
|
self.text = text
|
2022-12-15 15:00:36 +01:00
|
|
|
self.text_entities = parse_sequence_arg(text_entities)
|
2016-10-04 01:13:24 +02:00
|
|
|
self.animation = animation
|
2016-10-03 20:09:57 +02:00
|
|
|
|
2020-07-14 21:33:56 +02:00
|
|
|
self._id_attrs = (self.title, self.description, self.photo)
|
|
|
|
|
2022-12-15 15:00:36 +01:00
|
|
|
self._freeze()
|
|
|
|
|
2017-07-23 21:14:38 +02:00
|
|
|
@classmethod
|
2020-10-06 19:28:40 +02:00
|
|
|
def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["Game"]:
|
2021-05-27 09:38:17 +02:00
|
|
|
"""See :meth:`telegram.TelegramObject.de_json`."""
|
|
|
|
data = cls._parse_data(data)
|
2020-10-06 19:28:40 +02:00
|
|
|
|
2016-10-03 20:09:57 +02:00
|
|
|
if not data:
|
|
|
|
return None
|
|
|
|
|
|
|
|
data["photo"] = PhotoSize.de_list(data.get("photo"), bot)
|
|
|
|
data["text_entities"] = MessageEntity.de_list(data.get("text_entities"), bot)
|
|
|
|
data["animation"] = Animation.de_json(data.get("animation"), bot)
|
|
|
|
|
2022-10-07 11:51:53 +02:00
|
|
|
return super().de_json(data=data, bot=bot)
|
2016-10-03 20:09:57 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def parse_text_entity(self, entity: MessageEntity) -> str:
|
2017-09-01 08:43:08 +02:00
|
|
|
"""Returns the text from a given :class:`telegram.MessageEntity`.
|
2016-10-03 20:09:57 +02:00
|
|
|
|
|
|
|
Note:
|
|
|
|
This method is present because Telegram calculates the offset and length in
|
|
|
|
UTF-16 codepoint pairs, which some versions of Python don't handle automatically.
|
|
|
|
(That is, you can't just slice ``Message.text`` with the offset and length.)
|
|
|
|
|
|
|
|
Args:
|
2017-07-23 22:33:08 +02:00
|
|
|
entity (:class:`telegram.MessageEntity`): The entity to extract the text from. It must
|
|
|
|
be an entity that belongs to this message.
|
2016-10-03 20:09:57 +02:00
|
|
|
|
|
|
|
Returns:
|
2017-07-23 22:33:08 +02:00
|
|
|
:obj:`str`: The text of the given entity.
|
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
Raises:
|
|
|
|
RuntimeError: If this game has no text.
|
|
|
|
|
2017-09-01 08:43:08 +02:00
|
|
|
"""
|
2020-10-06 19:28:40 +02:00
|
|
|
if not self.text:
|
|
|
|
raise RuntimeError("This Game has no 'text'.")
|
|
|
|
|
2016-10-03 20:09:57 +02:00
|
|
|
# Is it a narrow build, if so we don't need to convert
|
|
|
|
if sys.maxunicode == 0xFFFF:
|
|
|
|
return self.text[entity.offset : entity.offset + entity.length]
|
2020-10-31 16:33:34 +01:00
|
|
|
entity_text = self.text.encode("utf-16-le")
|
|
|
|
entity_text = entity_text[entity.offset * 2 : (entity.offset + entity.length) * 2]
|
2016-10-03 20:09:57 +02:00
|
|
|
|
|
|
|
return entity_text.decode("utf-16-le")
|
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def parse_text_entities(self, types: List[str] = None) -> Dict[MessageEntity, str]:
|
2016-10-03 20:09:57 +02:00
|
|
|
"""
|
2017-07-23 22:33:08 +02:00
|
|
|
Returns a :obj:`dict` that maps :class:`telegram.MessageEntity` to :obj:`str`.
|
2022-04-24 12:38:09 +02:00
|
|
|
It contains entities from this message filtered by their
|
|
|
|
:attr:`~telegram.MessageEntity.type` attribute as the key, and the text that each entity
|
|
|
|
belongs to as the value of the :obj:`dict`.
|
2016-10-03 20:09:57 +02:00
|
|
|
|
|
|
|
Note:
|
2017-07-23 22:33:08 +02:00
|
|
|
This method should always be used instead of the :attr:`text_entities` attribute, since
|
|
|
|
it calculates the correct substring from the message text based on UTF-16 codepoints.
|
|
|
|
See :attr:`parse_text_entity` for more info.
|
2016-10-03 20:09:57 +02:00
|
|
|
|
|
|
|
Args:
|
2022-04-24 12:38:09 +02:00
|
|
|
types (List[:obj:`str`], optional): List of :class:`telegram.MessageEntity` types as
|
|
|
|
strings. If the :attr:`~telegram.MessageEntity.type` attribute of an entity is
|
|
|
|
contained in this list, it will be returned. Defaults to
|
|
|
|
:attr:`telegram.MessageEntity.ALL_TYPES`.
|
2016-10-03 20:09:57 +02:00
|
|
|
|
|
|
|
Returns:
|
2017-07-23 22:33:08 +02:00
|
|
|
Dict[:class:`telegram.MessageEntity`, :obj:`str`]: A dictionary of entities mapped to
|
|
|
|
the text that belongs to them, calculated based on UTF-16 codepoints.
|
|
|
|
|
2017-09-01 08:43:08 +02:00
|
|
|
"""
|
2016-10-03 20:09:57 +02:00
|
|
|
if types is None:
|
|
|
|
types = MessageEntity.ALL_TYPES
|
|
|
|
|
2016-10-12 22:56:57 +02:00
|
|
|
return {
|
|
|
|
entity: self.parse_text_entity(entity)
|
2022-05-26 19:15:54 +02:00
|
|
|
for entity in self.text_entities
|
2020-10-06 19:28:40 +02:00
|
|
|
if entity.type in types
|
2016-10-12 22:56:57 +02:00
|
|
|
}
|