ADRIAN LEON
ADRIAN LEON

Reputation: 17

How can I send a couple of buttons to a channel and get it as soon as user click on one of them?

Do I have to import something else or i've made a mistake?

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')]  ])

Whe I receive the message, no button is shown

Upvotes: 0

Views: 1924

Answers (1)

TheKill-996
TheKill-996

Reputation: 1069

Only bot clients can send buttons. Also, what is clases? From where are you trying to import 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()

If you have any doubt, have a look at the Telethon documentation, you will find answers there.

Upvotes: 2

Related Questions