skip to Main Content
$data = [
    'text' => 'your message here',
    'chat_id' => '@channelname'
];

file_get_contents("https://api.telegram.org/bot$token/sendMessage?" . http_build_query($data));

I can send messages this way, but I want to dynamically put a button(link) in it. How can I do?

2

Answers


  1. Chosen as BEST ANSWER
    $token = "Your Token";
    $chatID = "Your Chat iD";
    $reply = "Message";
    
    
    $keyboard = array(
        "inline_keyboard" => array(array(array("text" => "Your Buton Name", "url" => "Your Url")))
    );
    
    $keyboard = json_encode($keyboard, true);
    
    $sendto = "https://api.telegram.org/bot$token/sendmessage?chat_id=".$chatID."&text=".$reply."&parse_mode=HTML&reply_markup=".$keyboard;
    
    file_get_contents($sendto);
    

  2. <?php
    
        $token = '11345342......';
        $keyboard = json_encode([
            "inline_keyboard" => [
                [
                    [
                        "text" => "Button 1",
                        "callback_data" => "1"
                    ],
                    [
                        "text" => "Button 2",
                        "callback_data" => "2"
                    ]
                ]
            ]
        ]);
    
        $data = http_build_query([
            'text' => 'your message here',
            'chat_id' => '@channelname'
        ]);
    
        $url = "https://api.telegram.org/bot$token/sendMessage?{$data}&reply_markup={$keyboard}";
        $res = @file_get_contents($url);
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search