PHP Keyboard Button for telegram bot
How to make PHP Keyboard Button for telegram bot?Its very simple. You can make array data convert it to JSON and make CURL request. More information on official telegram web-site. PHP TELEGRAM bot tutorial.
https://core.telegram.org/bots/api#inlinekeyboardbutton
Simple Button with PHP script with callback_data.
$keyboard = array(
"inline_keyboard" => array(array(array(
"text" => "button",
"callback_data" => "button_0"
)))
);
$postfields = array(
'chat_id' => "$chat_id",
'text' => "$reply",
'reply_markup' => json_encode($keyboard)
);
With this sample you will generate this:
Array ( [chat_id] => 2631402392 [text] => Working [reply_markup] => {"inline_keyboard":[[{"text":"button","callback_data":"button_0"}]]} )
And then you can send this data with curl to telegram API link.
FULL PHP EXAMPLE:
$bottoken = "TELEGRAM BOT TOKEN";
$chat_id = "TELEGRAM CHAT_ID";
$reply = "Working";
$url = "https://api.telegram.org/bot$bottoken/sendMessage";
$keyboard = array(
"inline_keyboard" => array(array(array(
"text" => "button",
"callback_data" => "button_0"
)))
);
$postfields = array(
'chat_id' => "$chat_id",
'text' => "$reply",
'reply_markup' => json_encode($keyboard)
);
if (!$curld = curl_init()) {
exit;
}
curl_setopt($curld, CURLOPT_POST, true);
curl_setopt($curld, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curld, CURLOPT_URL,$url);
curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curld);
curl_close ($curld);
For link button you have to change callback_data to URL.
Array ( [chat_id] => 2631402392 [text] => Working [reply_markup] => {"inline_keyboard":[[{"text":"button","url":"http:\/\/www.google.com"}]]} )
$keyboard = array(
"inline_keyboard" => array(array(array(
"text" => "button",
"url" => "http://www.google.com"
)))
);
You can send link button to your telegram bot. You can add other optional fields for your InlineKeyboardButton button.
https://www.facebook.com/chakabiz
https://www.youtube.com/channel/UCYuMRNb_SRZ4FMsZjnHRZUA
Thank you!!!
ReplyDeleteThanks for these great examples!
ReplyDeleteIs it possible to add a vote button that counts user's clicks?
What about reply text?
ReplyDeleteThank you very much
ReplyDelete