|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-2020 Dan <https://github.com/delivrance> |
| 3 | +# |
| 4 | +# This file is part of Pyrogram. |
| 5 | +# |
| 6 | +# Pyrogram is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published |
| 8 | +# by the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Pyrogram is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +# |
| 19 | +# This file is part of Pyrogram. |
| 20 | +# |
| 21 | +# Pyrogram is free software: you can redistribute it and/or modify |
| 22 | +# it under the terms of the GNU Lesser General Public License as published |
| 23 | +# by the Free Software Foundation, either version 3 of the License, or |
| 24 | +# (at your option) any later version. |
| 25 | +# |
| 26 | +# Pyrogram is distributed in the hope that it will be useful, |
| 27 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 28 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 29 | +# GNU Lesser General Public License for more details. |
| 30 | +# |
| 31 | +# You should have received a copy of the GNU Lesser General Public License |
| 32 | +# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
| 33 | + |
| 34 | +from base64 import b64encode |
| 35 | +from struct import pack |
| 36 | +from typing import Union |
| 37 | + |
| 38 | +import pyrogram |
| 39 | +from pyrogram.api import types |
| 40 | +from pyrogram.client.types.object import Object |
| 41 | +from pyrogram.client.types.update import Update |
| 42 | +from pyrogram.client.types.user_and_chats import User |
| 43 | +from pyrogram.client.types.messages_and_media import Location |
| 44 | +from pyrogram.client.ext import utils |
| 45 | + |
| 46 | + |
| 47 | +class ChosenInlineResult(Object, Update): |
| 48 | + """A :doc:`result <InlineQueryResult>` of an inline query chosen by the user and sent to their chat partner. |
| 49 | +
|
| 50 | + Parameters: |
| 51 | + result_id (``str``): |
| 52 | + The unique identifier for the result that was chosen. |
| 53 | +
|
| 54 | + from_user (:obj:`User`): |
| 55 | + The user that chose the result. |
| 56 | +
|
| 57 | + query (``str``): |
| 58 | + The query that was used to obtain the result. |
| 59 | +
|
| 60 | + location (:obj:`Location`, *optional*): |
| 61 | + Sender location, only for bots that require user location. |
| 62 | +
|
| 63 | + inline_message_id (``str``, *optional*): |
| 64 | + Identifier of the sent inline message. |
| 65 | + Available only if there is an :doc:`inline keyboard <InlineKeyboardMarkup>` attached to the message. |
| 66 | + Will be also received in :doc:`callback queries <CallbackQuery>` and can be used to edit the message. |
| 67 | +
|
| 68 | + .. note:: |
| 69 | +
|
| 70 | + It is necessary to enable inline feedback via `@Botfather <https://t.me/botfather>`_ in order to receive these |
| 71 | + objects in updates. |
| 72 | + """ |
| 73 | + |
| 74 | + def __init__( |
| 75 | + self, |
| 76 | + *, |
| 77 | + client: "pyrogram.BaseClient" = None, |
| 78 | + result_id: str, |
| 79 | + from_user: User, |
| 80 | + query: str, |
| 81 | + location: "pyrogram.Location" = None, |
| 82 | + inline_message_id: str = None, |
| 83 | + ): |
| 84 | + super().__init__(client) |
| 85 | + |
| 86 | + self.result_id = result_id |
| 87 | + self.from_user = from_user |
| 88 | + self.query = query |
| 89 | + self.location = location |
| 90 | + self.inline_message_id = inline_message_id |
| 91 | + |
| 92 | + @staticmethod |
| 93 | + def _parse(client, chosen_inline_result: types.UpdateBotInlineSend, users) -> "ChosenInlineResult": |
| 94 | + inline_message_id = None |
| 95 | + |
| 96 | + if isinstance(chosen_inline_result.msg_id, types.InputBotInlineMessageID): |
| 97 | + inline_message_id = b64encode( |
| 98 | + pack( |
| 99 | + "<iqq", |
| 100 | + chosen_inline_result.msg_id.dc_id, |
| 101 | + chosen_inline_result.msg_id.id, |
| 102 | + chosen_inline_result.msg_id.access_hash |
| 103 | + ), |
| 104 | + b"-_" |
| 105 | + ).decode().rstrip("=") |
| 106 | + |
| 107 | + return ChosenInlineResult( |
| 108 | + result_id=str(chosen_inline_result.id), |
| 109 | + from_user=User._parse(client, users[chosen_inline_result.user_id]), |
| 110 | + query=chosen_inline_result.query, |
| 111 | + location=Location( |
| 112 | + longitude=chosen_inline_result.geo.long, |
| 113 | + latitude=chosen_inline_result.geo.lat, |
| 114 | + client=client |
| 115 | + ) if chosen_inline_result.geo else None, |
| 116 | + inline_message_id=inline_message_id |
| 117 | + ) |
0 commit comments