Users are now able to view keyboards from bots

This commit is contained in:
Dan 2018-04-27 20:11:36 +02:00
parent f6b0e0e0dc
commit e81ef5a757
9 changed files with 119 additions and 4 deletions

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -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)

View File

@ -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(

View File

@ -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

View File

@ -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(

View File

@ -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