PHP telegram bot - adding share button to messages

I’m working on a telegram bot using PHP and I need help with creating a button that lets users share messages. I want to attach a button to my bot’s message so when people click it, they can send that message to other chats or groups.

Right now I have this basic code but I’m not sure how to make the sharing part work:

$buttons = [
    'inline_keyboard' => [
        [['text' => 'Share with friends']]
    ]
];

sendTelegramMessage([
    "chat_id" => $chatData["message"]["chat"]["id"],
    "text" => "Check out this cool message",
    "reply_markup" => json_encode($buttons)
]);

What do I need to add to make the button actually work for sharing? Any help would be great!

There’s another approach that works great depending on what you need. Skip switch_inline_query and use the url parameter instead - you get way more control:

$shareUrl = 'https://t.me/share/url?url=' . urlencode('https://yourbot.com') . '&text=' . urlencode('Check out this cool message');

$buttons = [
    'inline_keyboard' => [
        [['text' => 'Share with friends', 'url' => $shareUrl]]
    ]
];

This opens Telegram’s share dialog so users can pick any chat or contact. Works even without inline mode enabled. I’ve found it more reliable for basic sharing, though inline queries are better if you want everything happening inside your bot.

You’re missing the callback_data or url parameter in your button setup. For sharing, use Telegram’s switch_inline_query parameter instead. Here’s what works:

$buttons = [
    'inline_keyboard' => [
        [['text' => 'Share with friends', 'switch_inline_query' => 'Check out this cool message']]
    ]
];

This opens the chat picker when clicked and pre-fills the message. Want users to share in the current chat? Use switch_inline_query_current_chat instead. I’ve used this for months - it handles sharing automatically without extra webhook handling for button callbacks.