diff --git a/aiogram/bot.py b/aiogram/bot.py index 97c5e90b..6fa46a62 100644 --- a/aiogram/bot.py +++ b/aiogram/bot.py @@ -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)) diff --git a/aiogram/types/force_reply.py b/aiogram/types/force_reply.py new file mode 100644 index 00000000..182137b7 --- /dev/null +++ b/aiogram/types/force_reply.py @@ -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 diff --git a/aiogram/types/inline_keyboard.py b/aiogram/types/inline_keyboard.py new file mode 100644 index 00000000..b5dc1f95 --- /dev/null +++ b/aiogram/types/inline_keyboard.py @@ -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} diff --git a/aiogram/types/reply_keyboard.py b/aiogram/types/reply_keyboard.py new file mode 100644 index 00000000..4f717436 --- /dev/null +++ b/aiogram/types/reply_keyboard.py @@ -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