首页 > 解决方案 > 如何将几个按钮发送到频道并在用户单击其中一个按钮时立即获取?

问题描述

我必须导入其他东西还是我犯了一个错误?

from telethon import functions, types, events, utils
from clases.button import Button

.
.
await client.send_message(chat_id,
                                      'Pick one from this grid',
                                      buttons=[[Button.inline('Left'),
                                                Button.inline('Right')],
                                               [Button.url('Check this site!', 'https://example.com')]  ])

当我收到消息时,没有显示任何按钮

标签: python-3.xtelegram-bottelethon

解决方案


只有 bot 客户端可以发送buttons. 还有,什么是clases?您要从哪里导入Button

import asyncio 
from telethon import TelegramClient
from telethon import functions, types, events
from telethon.tl.custom import Button

# start the bot client
client = TelegramClient('SESSION_NAME', 'YOUR_API_ID', 'YOUR_API_HASH')
client.start(bot_token='your bot token')

# function that sends the message
async def sendButtons():
    await client.send_message(chat, 'Pick one from this grid', buttons=[[Button.inline('Left'), Button.inline('Right')], [Button.url('Check this site!', 'https://example.com')]])                         

# CallBackQuery event handler that gets triggered every time a user click a Button.inline
@events.register(events.CallbackQuery(chats=[your_chat]))
async def click_handler(event):
    print(event) # event contains the user choice

loop = asyncio.get_event_loop()
loop.run_until_complete(sendButtons())
client.add_event_handler(click_handler)
loop.run_forever()

如果您有任何疑问,请查看Telethon 文档,您会在那里找到答案。


推荐阅读