diff --git a/pyrogram/client/message_parser.py b/pyrogram/client/message_parser.py index c285c704..c0ca13a7 100644 --- a/pyrogram/client/message_parser.py +++ b/pyrogram/client/message_parser.py @@ -447,6 +447,20 @@ def parse_message( else: media = None + reply_markup = message.reply_markup + + if reply_markup: + if isinstance(reply_markup, types.ReplyKeyboardForceReply): + reply_markup = pyrogram_types.ForceReply.read(reply_markup) + elif isinstance(reply_markup, types.ReplyKeyboardMarkup): + reply_markup = pyrogram_types.ReplyKeyboardMarkup.read(reply_markup) + elif isinstance(reply_markup, types.ReplyInlineMarkup): + reply_markup = pyrogram_types.InlineKeyboardMarkup.read(reply_markup) + elif isinstance(reply_markup, types.ReplyKeyboardHide): + reply_markup = pyrogram_types.ReplyKeyboardRemove.read(reply_markup) + else: + reply_markup = None + m = pyrogram_types.Message( message_id=message.id, date=message.date, @@ -477,7 +491,8 @@ def parse_message( views=message.views, via_bot=parse_user(users.get(message.via_bot_id, None)), outgoing=message.out, - client=client + client=client, + reply_markup=reply_markup ) if message.reply_to_msg_id and replies: diff --git a/pyrogram/client/types/__init__.py b/pyrogram/client/types/__init__.py index 7b864fd4..d16fdbbb 100644 --- a/pyrogram/client/types/__init__.py +++ b/pyrogram/client/types/__init__.py @@ -29,6 +29,10 @@ from .location import Location from .message import Message from .message_entity import MessageEntity from .photo_size import PhotoSize +from .reply_markup import ( + ForceReply, InlineKeyboardButton, InlineKeyboardMarkup, + KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove +) from .sticker import Sticker from .update import Update from .user import User diff --git a/pyrogram/client/types/message.py b/pyrogram/client/types/message.py index dff5a6a9..6703a280 100644 --- a/pyrogram/client/types/message.py +++ b/pyrogram/client/types/message.py @@ -245,7 +245,8 @@ class Message(Object): via_bot=None, outgoing: bool = None, matches: list = None, - command: list = None + command: list = None, + reply_markup=None, ): self.message_id = message_id # int self.client = client @@ -294,4 +295,5 @@ class Message(Object): self.via_bot = via_bot # flags.40?User self.outgoing = outgoing self.matches = matches - self.command = command + self.command = command, + self.reply_markup = reply_markup diff --git a/pyrogram/client/types/reply_markup/force_reply.py b/pyrogram/client/types/reply_markup/force_reply.py index 0aa4c134..58312495 100644 --- a/pyrogram/client/types/reply_markup/force_reply.py +++ b/pyrogram/client/types/reply_markup/force_reply.py @@ -41,6 +41,12 @@ class ForceReply(Object): def __init__(self, selective: bool = None): self.selective = selective + @staticmethod + def read(o, *args): + return ForceReply( + selective=o.selective + ) + def write(self): return ReplyKeyboardForceReply( single_use=True, diff --git a/pyrogram/client/types/reply_markup/inline_keyboard_button.py b/pyrogram/client/types/reply_markup/inline_keyboard_button.py index 7eb851db..72f14eef 100644 --- a/pyrogram/client/types/reply_markup/inline_keyboard_button.py +++ b/pyrogram/client/types/reply_markup/inline_keyboard_button.py @@ -83,6 +83,32 @@ class InlineKeyboardButton(Object): self.callback_game = callback_game self.pay = pay + @staticmethod + def read(b, *args): + if isinstance(b, KeyboardButtonUrl): + return InlineKeyboardButton( + text=b.text, + url=b.url + ) + + if isinstance(b, KeyboardButtonCallback): + return InlineKeyboardButton( + text=b.text, + callback_data=b.data.decode() + ) + + if isinstance(b, KeyboardButtonSwitchInline): + if b.same_peer: + return InlineKeyboardButton( + text=b.text, + switch_inline_query_current_chat=b.query + ) + else: + return InlineKeyboardButton( + text=b.text, + switch_inline_query=b.query + ) + def write(self): if self.url: return KeyboardButtonUrl(self.text, self.url) diff --git a/pyrogram/client/types/reply_markup/inline_keyboard_markup.py b/pyrogram/client/types/reply_markup/inline_keyboard_markup.py index 79bc0c37..74bc837e 100644 --- a/pyrogram/client/types/reply_markup/inline_keyboard_markup.py +++ b/pyrogram/client/types/reply_markup/inline_keyboard_markup.py @@ -19,6 +19,7 @@ from pyrogram.api.core import Object from pyrogram.api.types import ReplyInlineMarkup, KeyboardButtonRow +from . import InlineKeyboardButton class InlineKeyboardMarkup(Object): @@ -37,6 +38,22 @@ class InlineKeyboardMarkup(Object): def __init__(self, inline_keyboard: list): self.inline_keyboard = inline_keyboard + @staticmethod + def read(kb, *args): + inline_keyboard = [] + + for i in kb.rows: + row = [] + + for j in i.buttons: + row.append(InlineKeyboardButton.read(j)) + + inline_keyboard.append(row) + + return InlineKeyboardMarkup( + inline_keyboard=inline_keyboard + ) + def write(self): return ReplyInlineMarkup( [KeyboardButtonRow( diff --git a/pyrogram/client/types/reply_markup/keyboard_button.py b/pyrogram/client/types/reply_markup/keyboard_button.py index e4b12de5..149bd6e5 100644 --- a/pyrogram/client/types/reply_markup/keyboard_button.py +++ b/pyrogram/client/types/reply_markup/keyboard_button.py @@ -51,6 +51,23 @@ class KeyboardButton(Object): self.request_contact = request_contact self.request_location = request_location + @staticmethod + def read(b, *args): + if isinstance(b, RawKeyboardButton): + return b.text + + if isinstance(b, KeyboardButtonRequestPhone): + return KeyboardButton( + text=b.text, + request_contact=True + ) + + if isinstance(b, KeyboardButtonRequestGeoLocation): + return KeyboardButton( + text=b.text, + request_location=True + ) + def write(self): # TODO: Enforce optional args mutual exclusiveness diff --git a/pyrogram/client/types/reply_markup/reply_keyboard_markup.py b/pyrogram/client/types/reply_markup/reply_keyboard_markup.py index c4a5be25..d24eca39 100644 --- a/pyrogram/client/types/reply_markup/reply_keyboard_markup.py +++ b/pyrogram/client/types/reply_markup/reply_keyboard_markup.py @@ -18,9 +18,11 @@ from pyrogram.api.core import Object -from pyrogram.api.types import KeyboardButtonRow, KeyboardButton +from pyrogram.api.types import KeyboardButtonRow from pyrogram.api.types import ReplyKeyboardMarkup as RawReplyKeyboardMarkup +from . import KeyboardButton + class ReplyKeyboardMarkup(Object): """This object represents a custom keyboard with reply options (see Introduction to bots for details and examples). @@ -64,6 +66,26 @@ class ReplyKeyboardMarkup(Object): self.one_time_keyboard = one_time_keyboard self.selective = selective + @staticmethod + def read(kb, *args): + print(kb) + keyboard = [] + + for i in kb.rows: + row = [] + + for j in i.buttons: + row.append(KeyboardButton.read(j)) + + keyboard.append(row) + + return ReplyKeyboardMarkup( + keyboard=keyboard, + resize_keyboard=kb.resize, + one_time_keyboard=kb.single_use, + selective=kb.selective + ) + def write(self): return RawReplyKeyboardMarkup( rows=[KeyboardButtonRow( diff --git a/pyrogram/client/types/reply_markup/reply_keyboard_remove.py b/pyrogram/client/types/reply_markup/reply_keyboard_remove.py index dc4f8c60..dbfce50a 100644 --- a/pyrogram/client/types/reply_markup/reply_keyboard_remove.py +++ b/pyrogram/client/types/reply_markup/reply_keyboard_remove.py @@ -44,6 +44,12 @@ class ReplyKeyboardRemove(Object): def __init__(self, selective: bool = None): self.selective = selective + @staticmethod + def read(o, *args): + return ReplyKeyboardRemove( + selective=o.selective + ) + def write(self): return ReplyKeyboardHide( selective=self.selective or None