Search code examples
phpbotstelegramtelegram-botphp-telegram-bot

send inline keyboard like t.me/username but for userid in telegram php


I want to send an inline keyboard in which get the user into somebodey's chat whit his chat_id . I know that i can set a Url for inline keyboard like: t.me/username but something like that doesn't work with

chat_id : t.me/123456

How to do that ?


Solution

  • Just use an https:// prefix at the start of your InlineKeyboardButton's url.

    So reply_markup parameter of the sendMessage method should look like this:

    "reply_markup": {"inline_keyboard": [[{"text": "button text...", "url": "https://t.me/<chat_id or name of another user>"}]]}.

    Full example with curl:

    curl -v -H "Content-Type: application/json" -d '{"chat_id": 123456, "text": "here is your chat with <USERNAME>", "reply_markup": {"inline_keyboard": [[{"text": "here is your link to another user", "url": "https://t.me/<USERNAME>"}]]}}' https://api.telegram.org/bot<BOT_ID>:<BOT_TOKEN>/sendMessage

    This line sends a Message to user with id = 123456. Message contains inline keyboard with one InlineKeyboardButton. This button has a link to chat with another user with id <USERNAME> and respective page URL https://t.me/<USERNAME>.

    update

    Unfortunately, if user you're looking for does not have a username, you cannot make a t.me link, so you cannot contact him:

    You can write to people who are in your phone contacts and have Telegram. Another way of contacting people is to type their Telegam username into the search field.
    ...
    You can set up a public username on Telegram. It then becomes possible for other users to find you by that username — you will appear in contacts search under ‘global results’. Please note that people who find you will be able to send you messages, even if they don't know your number. If you are not comfortable with this, we advise against setting up a username in Telegram.

    So looks like the only solution here is to ensure that every needed contact has a username.

    More information can be found here and here.