【发布时间】:2018-04-06 00:23:06
【问题描述】:
这是一个 php 电报 webhook 脚本:
//calling telegram api after setting $access_token with the bot api
$api = 'https://api.telegram.org/bot' . $access_token;
$output = json_decode(file_get_contents('php://input'), TRUE);
//setting variables from telegram api
$chat_id = $output['message']['chat']['id'];
$first_name = $output['message']['chat']['first_name'];
$last_name = $output["message"]["chat"]["last_name"];
$message = $output['message']['text'];
$callback_query = $output['callback_query'];
$data = $callback_query['data'];
$message_id = ['callback_query']['message']['message_id'];
$chat_id_in = $callback_query['message']['chat']['id'];
switch($message) {
case '/start':
$inline_button1 = array("text"=>"Website","url"=>"http://google.com");
$inline_button2 = array("text"=>"test","callback_data"=>'/test');
$inline_keyboard = [[$inline_button1,$inline_button2]];
$keyboard=array("inline_keyboard"=>$inline_keyboard);
$replyMarkup = json_encode($keyboard);
sendMessage($chat_id, "hello", $replyMarkup);
break;
case '/help':
sendMessage($chat_id,"you need help?",$replyMarkup);
break;
default:
sendMessage($chat_id,"thank you",$replyMarkup);
}
switch($data){
case '/test':
sendMessage($chat_id_in, "Coming Soon .....");
break;
}
//the sendMessage function
function sendMessage($chat_id, $message, $replyMarkup) {
file_get_contents($GLOBALS['api'] . '/sendMessage?chat_id=' . $chat_id . '&text=' . urlencode($message) . '&reply_markup=' . $replyMarkup);
}
上面的代码在电报上使用内联键盘对我来说很好,但我喜欢添加replykeyboard 以及一些命令,但我遇到了问题,请参见下文。我认为这是我需要正确处理的 curl 功能,你们的想法是什么。
你能帮我什么忙?
$api = 'https://api.telegram.org/bot' . $access_token;
$output = json_decode(file_get_contents('php://input'), TRUE);
$chat_id = $output['message']['chat']['id'];
$first_name = $output['message']['chat']['first_name'];
$last_name = $output["message"]["chat"]["last_name"];
$message = $output['message']['text'];
$callback_query = $output['callback_query'];
$data = $callback_query['data'];
$message_id = ['callback_query']['message']['message_id'];
$chat_id_in = $callback_query['message']['chat']['id'];
switch($message) {
case '/start':
$inline_button1 = array("text"=>"Website","url"=>"http://google.com");
$inline_button2 = array("text"=>"test","callback_data"=>'/test');
$inline_keyboard = [[$inline_button1,$inline_button2]];
$keyboard=array("inline_keyboard"=>$inline_keyboard);
$replyMarkup = json_encode($keyboard);
sendMessage($chat_id, "Hello", $replyMarkup);
break;
case '/help':
$keyboard1 = array("text"=>"Button 1","text"=>"Button 1","resize_keyboard"=>'true',"one_time_keyboard"=>'true');
$keyboard2 = array("text"=>"Button 2","text"=>"Button 2","resize_keyboard"=>'true',"one_time_keyboard"=>'true');
$keyboardset = [[$keyboard1,$keyboard2]];
$keyboard=array("KeyboardButton"=>$keyboardset)
$replyMarkup = json_encode($keyboard);
sendMessage($chat_id,"$you need help",$replyMarkup);
break;
default:
sendMessage($chat_id,"$Thanks",$replyMarkup);
}
switch($data){
case '/test':
sendMessage($chat_id_in, "Coming Soon .....");
break;
}
//call the curl $ch = curl_init($api . 'sendMessage');
curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch,
CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($data)); curl_setopt($ch,
CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($ch);
curl_close($ch);
function sendMessage($chat_id, $message, $replyMarkup) {
file_get_contents($GLOBALS['api'] . '/sendMessage?chat_id=' . $chat_id . '&text=' . urlencode($message) . '&reply_markup=' .
$replyMarkup);
}
提前致谢
【问题讨论】:
标签: php switch-statement telegram telegram-bot php-telegram-bot