python-3.x - 如何将几个按钮发送到频道并在用户单击其中一个按钮时立即获取?
问题描述
我必须导入其他东西还是我犯了一个错误?
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')] ])
解决方案
只有 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 文档,您会在那里找到答案。
推荐阅读
- ios - 使用新版本领域的应用程序无法打开使用旧版本领域的应用程序创建的数据库
- ubuntu - WSL Ubuntu(Linux 的 Windows 子系统)上没有互联网连接
- javascript - 视差视频背景而不是图像
- python-3.x - 当我从班级的 var 中打印 `chr(13)` 时,会打印"\r"
- graphql - graphql 模式的命名约定是什么?
- powershell - PowerShell 位置参数和参数集
- excel - 如何根据两个条件返回 Excel 中的单元格值列表?
- python - Python Selenium 找不到要发送 _keys 的字段元素
- c# - 在 HoloLens 1 上,使用默认构造函数创建 TcpClient 对象时会抛出“ArgumentException:值不在预期范围内”
- r - 用指标分隔列的R函数是什么