0

I got problem with Telegram Keyboard , I want to send a string in my array for markup that has a "#" but with "#" it dosent work and I visited a bot that same mine and that's works good and shows '#' in Keyboard Button

how can I fix this??

$string = "#number 1";//This # 

$markup[] = [
    ["text" =>  $string ]
];

$keyboard = [
    "keyboard" => $markup,
    "resize_keyboard" => true
];

return $r = file_get_contents("https://api.telegram.org/bot" . TOKEN . "/sendMessage?chat_id="
    . $chat_id . "&text=" . urlencode("MENU") . "&reply_markup=" . json_encode($keyboard));
Meti
  • 25
  • 6

1 Answers1

1

# will interfere with the request url.

You should escape it to prevent this.

Easiest solution is to use 's urlencode() function on the text thats containing any special chars:

<?php

    $chat_id = ;
    CONST TOKEN = '';

    $keyboard = [
        "keyboard" => [
            [
                ["text" =>  urlencode('Test -> #') ]
            ]
        ],
        "resize_keyboard" => true
    ];

    return file_get_contents("https://api.telegram.org/bot" . TOKEN . "/sendMessage?chat_id="
        . $chat_id . "&text=" . urlencode("MENU") . "&reply_markup=" . json_encode($keyboard));

Will produce:

enter image description here

0stone0
  • 34,288
  • 4
  • 39
  • 64