mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-16 20:23:32 +00:00
Add ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton, ForceReply and parse reply_markup
This commit is contained in:
parent
5874291b15
commit
7003b088fc
4 changed files with 126 additions and 5 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import asyncio
|
||||
import json
|
||||
|
||||
import aiohttp
|
||||
|
||||
|
|
@ -81,12 +82,13 @@ class AIOGramBot:
|
|||
:return:
|
||||
"""
|
||||
|
||||
if reply_markup and hasattr(reply_markup, 'to_json'):
|
||||
reply_markup = json.dumps(reply_markup.to_json())
|
||||
|
||||
if hasattr(reply_to_message_id, 'message_id'):
|
||||
reply_to_message_id = reply_to_message_id.message_id
|
||||
|
||||
payload = generate_payload(**locals())
|
||||
|
||||
if reply_markup:
|
||||
# TODO: convert markup
|
||||
pass
|
||||
|
||||
message = await self.request(ApiMethods.SEND_MESSAGE, payload)
|
||||
return self.prepare_object(Message.de_json(message))
|
||||
|
||||
|
|
|
|||
12
aiogram/types/force_reply.py
Normal file
12
aiogram/types/force_reply.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
from aiogram.types import Serializable
|
||||
|
||||
|
||||
class ForceReply(Serializable):
|
||||
def __init__(self, selective=None):
|
||||
self.selective = selective
|
||||
|
||||
def to_json(self):
|
||||
data = {'force_reply': True}
|
||||
if self.selective:
|
||||
data['selective'] = True
|
||||
return data
|
||||
45
aiogram/types/inline_keyboard.py
Normal file
45
aiogram/types/inline_keyboard.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
from . import Serializable
|
||||
|
||||
|
||||
class InlineKeyboardMarkup(Serializable):
|
||||
def __init__(self, row_width=3):
|
||||
self.row_width = row_width
|
||||
|
||||
self.keyboard = []
|
||||
|
||||
def add(self, *args):
|
||||
i = 1
|
||||
row = []
|
||||
for button in args:
|
||||
row.append(button.to_json())
|
||||
if i % self.row_width == 0:
|
||||
self.keyboard.append(row)
|
||||
row = []
|
||||
i += 1
|
||||
if len(row) > 0:
|
||||
self.keyboard.append(row)
|
||||
|
||||
def row(self, *args):
|
||||
btn_array = []
|
||||
for button in args:
|
||||
btn_array.append(button.to_json())
|
||||
self.keyboard.append(btn_array)
|
||||
return self
|
||||
|
||||
def to_json(self):
|
||||
return {'inline_keyboard': self.keyboard}
|
||||
|
||||
|
||||
class InlineKeyboardButton(Serializable):
|
||||
def __init__(self, text, url=None, callback_data=None, switch_inline_query=None,
|
||||
switch_inline_query_current_chat=None, callback_game=None, pay=None):
|
||||
self.text = text
|
||||
self.url = url
|
||||
self.callback_data = callback_data
|
||||
self.switch_inline_query = switch_inline_query
|
||||
self.switch_inline_query_current_chat = switch_inline_query_current_chat
|
||||
self.callback_game = callback_game
|
||||
self.pay = pay
|
||||
|
||||
def to_json(self):
|
||||
return {key: value for key, value in self.__dict__.items() if value}
|
||||
62
aiogram/types/reply_keyboard.py
Normal file
62
aiogram/types/reply_keyboard.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
from . import Serializable
|
||||
|
||||
|
||||
class ReplyKeyboardMarkup(Serializable):
|
||||
def __init__(self, resize_keyboard=None, one_time_keyboard=None, selective=None, row_width=3):
|
||||
self.resize_keyboard = resize_keyboard
|
||||
self.one_time_keyboard = one_time_keyboard
|
||||
self.selective = selective
|
||||
self.row_width = row_width
|
||||
|
||||
self.keyboard = []
|
||||
|
||||
def add(self, *args):
|
||||
i = 1
|
||||
row = []
|
||||
for button in args:
|
||||
if isinstance(button, str):
|
||||
row.append({'text': button})
|
||||
elif isinstance(button, bytes):
|
||||
row.append({'text': button.decode('utf-8')})
|
||||
else:
|
||||
row.append(button.to_json())
|
||||
if i % self.row_width == 0:
|
||||
self.keyboard.append(row)
|
||||
row = []
|
||||
i += 1
|
||||
if len(row) > 0:
|
||||
self.keyboard.append(row)
|
||||
|
||||
def row(self, *args):
|
||||
btn_array = []
|
||||
for button in args:
|
||||
if isinstance(button, str):
|
||||
btn_array.append({'text': button})
|
||||
else:
|
||||
btn_array.append(button.to_json())
|
||||
self.keyboard.append(btn_array)
|
||||
return self
|
||||
|
||||
def to_json(self):
|
||||
return {key: value for key, value in self.__dict__.items() if value and key != 'row_width'}
|
||||
|
||||
|
||||
class KeyboardButton(Serializable):
|
||||
def __init__(self, text, request_contact=None, request_location=None):
|
||||
self.text = text
|
||||
self.request_contact = request_contact
|
||||
self.request_location = request_location
|
||||
|
||||
def to_json(self):
|
||||
return self.__dict__
|
||||
|
||||
|
||||
class ReplyKeyboardRemove(Serializable):
|
||||
def __init__(self, selective=None):
|
||||
self.selective = selective
|
||||
|
||||
def to_json(self):
|
||||
json_dict = {'remove_keyboard': True}
|
||||
if self.selective:
|
||||
json_dict['selective'] = True
|
||||
return json_dict
|
||||
Loading…
Add table
Add a link
Reference in a new issue