Davide Martelli
Davide Martelli

Reputation: 319

Link in telegram bot so the user can call a bot command

I'm writing a telegram bot but I have a question. For now my bot will search for an image based on user request, but if I the bat found more than one image I want to send to the user a list of image with a link for search that iamge. Eg.

/command mickey mouse .... image 1.... I found more than one image, please be more specific [link to image 2] [link to image 3]

If the user will click the link I need to autosend message with the command and the name of the new image.

Is possible? I tried to add an hyperlink to the telegram api but i will open in the browser and send me a json with call status of the api.

Upvotes: 12

Views: 16734

Answers (3)

The question is old, but nevertheless. I was looking for a solution to the same problem now. And I found a solution. In the Aiogram library, as I believe in others now, there are methods that allow you to format the text of a message in almost any form.

Here, the Aiogram documentation describes how to use it.

If you pay attention to the BotCommand section, you will find quite clear instructions on how to add a telegram bot command to the message body.

I did it the following way:

...
from aiogram.utils.formatting import BotCommand
...


@some_router.callback_query(F.data == 'some_command')
async def some_function(
    callback: CallbackQuery
) -> None:
    await callback.answer(
        'Let`s see what we found'
    )
    entities = BotCommand('/my_profile')
    await callback.message.answer(
        'some text'
        'If you do not want to receive notifications, '
        'disable them in your personal account {my_profile_command}'.format(my_profile_command=entities.as_kwargs()['text'])
    )

Clicking on /my_account executes my command. It works as it should. enter image description here

Upvotes: 0

Yuriy Litvin
Yuriy Litvin

Reputation: 101

We can vote the suggestion for TelegramBotApi: https://bugs.telegram.org/c/3901

Upvotes: 4

Alexander Trakhimenok
Alexander Trakhimenok

Reputation: 6268

For inline mode you can simply return a list of image results that will be displayed as kind of a popup on top of keyboard.

For conversation mode you have options:

1) Return images as inline keyboard attachment to a message with array of buttons each having callback_data parameter or switch_inline_query_current_chat or url parameter. Handle one of this to display the image.

2) Return message text as HTML with list of links in form of: <a href="https://t.me/YOUR_BOT?start=image-123456789">image name</a>

Then you can parse the start command and extract image ID. This has disadvantage that user would need to click the "START" button every time after he clicked the link.

You can use the 2nd approach with inline mode as well.

In my @DebtsTrackerBot I use both callbacks & switch_inline_query_current_chat for similar task.

Upvotes: 6

Related Questions