mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2026-09-13 04:05:58 +00:00
Create a custom.Button class and support send_message(buttons=...)
This commit is contained in:
@@ -13,10 +13,11 @@ from .telegrambaseclient import TelegramBaseClient
|
||||
from .users import UserMethods # Required for everything
|
||||
from .messageparse import MessageParseMethods # Required for messages
|
||||
from .uploads import UploadMethods # Required for messages to send files
|
||||
from .updates import UpdateMethods # Required for buttons (register callbacks)
|
||||
from .buttons import ButtonMethods # Required for messages to use buttons
|
||||
from .messages import MessageMethods
|
||||
from .chats import ChatMethods
|
||||
from .dialogs import DialogMethods
|
||||
from .downloads import DownloadMethods
|
||||
from .auth import AuthMethods
|
||||
from .updates import UpdateMethods
|
||||
from .telegramclient import TelegramClient
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
from .updates import UpdateMethods
|
||||
from ..tl import types, custom
|
||||
from .. import utils
|
||||
|
||||
|
||||
class ButtonMethods(UpdateMethods):
|
||||
def _build_reply_markup(self, buttons):
|
||||
if buttons is None:
|
||||
return None
|
||||
|
||||
try:
|
||||
if buttons.SUBCLASS_OF_ID == 0xe2e10ef2:
|
||||
return buttons # crc32(b'ReplyMarkup'):
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
if not utils.is_list_like(buttons):
|
||||
buttons = [[buttons]]
|
||||
elif not utils.is_list_like(buttons[0]):
|
||||
buttons = [buttons]
|
||||
|
||||
is_inline = False
|
||||
is_normal = False
|
||||
|
||||
rows = []
|
||||
for row in buttons:
|
||||
current = []
|
||||
for button in row:
|
||||
inline = custom.Button._is_inline(button)
|
||||
is_inline |= inline
|
||||
is_normal |= not inline
|
||||
if isinstance(button, custom.Button):
|
||||
# TODO actually register callbacks
|
||||
button = button.button
|
||||
|
||||
if button.SUBCLASS_OF_ID == 0xbad74a3:
|
||||
# 0xbad74a3 == crc32(b'KeyboardButton')
|
||||
current.append(button)
|
||||
|
||||
if current:
|
||||
rows.append(types.KeyboardButtonRow(current))
|
||||
|
||||
if is_inline == is_normal and is_normal:
|
||||
raise ValueError('You cannot mix inline with normal buttons')
|
||||
elif is_inline:
|
||||
return types.ReplyInlineMarkup(rows)
|
||||
elif is_normal:
|
||||
return types.ReplyKeyboardMarkup(rows)
|
||||
@@ -8,13 +8,14 @@ from async_generator import async_generator, yield_
|
||||
|
||||
from .messageparse import MessageParseMethods
|
||||
from .uploads import UploadMethods
|
||||
from .buttons import ButtonMethods
|
||||
from .. import utils
|
||||
from ..tl import types, functions, custom
|
||||
|
||||
__log__ = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MessageMethods(UploadMethods, MessageParseMethods):
|
||||
class MessageMethods(ButtonMethods, UploadMethods, MessageParseMethods):
|
||||
|
||||
# region Public methods
|
||||
|
||||
@@ -333,7 +334,7 @@ class MessageMethods(UploadMethods, MessageParseMethods):
|
||||
async def send_message(
|
||||
self, entity, message='', *, reply_to=None,
|
||||
parse_mode=utils.Default, link_preview=True, file=None,
|
||||
force_document=False, clear_draft=False):
|
||||
force_document=False, clear_draft=False, buttons=None):
|
||||
"""
|
||||
Sends the given message to the specified entity (user/chat/channel).
|
||||
|
||||
@@ -382,8 +383,15 @@ class MessageMethods(UploadMethods, MessageParseMethods):
|
||||
Whether the existing draft should be cleared or not.
|
||||
Has no effect when sending a file.
|
||||
|
||||
buttons (`list`, `custom.Button <telethon.tl.custom.button.Button>`,
|
||||
:tl:`KeyboardButton`):
|
||||
The matrix (list of lists), row list or button to be shown
|
||||
after sending the message. This parameter will only work if
|
||||
you have signed in as a bot. You can also pass your own
|
||||
:tl:`ReplyMarkup` here.
|
||||
|
||||
Returns:
|
||||
The sent `telethon.tl.custom.message.Message`.
|
||||
The sent `custom.Message <telethon.tl.custom.message.Message>`.
|
||||
"""
|
||||
if file is not None:
|
||||
return await self.send_file(
|
||||
@@ -417,12 +425,17 @@ class MessageMethods(UploadMethods, MessageParseMethods):
|
||||
else:
|
||||
reply_id = None
|
||||
|
||||
if buttons is None:
|
||||
markup = message.reply_markup
|
||||
else:
|
||||
markup = self._build_reply_markup(buttons)
|
||||
|
||||
request = functions.messages.SendMessageRequest(
|
||||
peer=entity,
|
||||
message=message.message or '',
|
||||
silent=message.silent,
|
||||
reply_to_msg_id=reply_id,
|
||||
reply_markup=message.reply_markup,
|
||||
reply_markup=markup,
|
||||
entities=message.entities,
|
||||
clear_draft=clear_draft,
|
||||
no_webpage=not isinstance(
|
||||
@@ -438,7 +451,8 @@ class MessageMethods(UploadMethods, MessageParseMethods):
|
||||
entities=msg_ent,
|
||||
no_webpage=not link_preview,
|
||||
reply_to_msg_id=utils.get_message_id(reply_to),
|
||||
clear_draft=clear_draft
|
||||
clear_draft=clear_draft,
|
||||
reply_markup=self._build_reply_markup(buttons)
|
||||
)
|
||||
|
||||
result = await self(request)
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
from . import (
|
||||
UpdateMethods, AuthMethods, DownloadMethods, DialogMethods,
|
||||
ChatMethods, MessageMethods, UploadMethods, MessageParseMethods,
|
||||
UserMethods
|
||||
AuthMethods, DownloadMethods, DialogMethods, ChatMethods,
|
||||
MessageMethods, ButtonMethods, UpdateMethods, UploadMethods,
|
||||
MessageParseMethods, UserMethods
|
||||
)
|
||||
|
||||
|
||||
class TelegramClient(
|
||||
UpdateMethods, AuthMethods, DownloadMethods, DialogMethods,
|
||||
ChatMethods, MessageMethods, UploadMethods, MessageParseMethods,
|
||||
UserMethods
|
||||
AuthMethods, DownloadMethods, DialogMethods, ChatMethods,
|
||||
MessageMethods, ButtonMethods, UpdateMethods, UploadMethods,
|
||||
MessageParseMethods, UserMethods
|
||||
):
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user