如何在同一时间使用Switch进行内联键盘和回复键盘#PHP

时间:2017-10-25 03:29:20

标签: php switch-statement telegram telegram-bot php-telegram-bot

这是一个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以及一些命令,但是我遇到了问题,请参阅下文。我认为它需要正确的卷曲功能,你的想法是什么。

你怎么能帮助我?

        $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);
            }

提前致谢

1 个答案:

答案 0 :(得分:0)

这是一个完整的代码

function cleanData(data);
% data = rand(19,1000,134);
f = figure('Units','Normalized','Position',[0.25 0.25 0.5 0.5]);
a =   axes('Units','Normalized','Position',[0.05 0.15, 0.75 0.75]);
s =   uicontrol(f, 'Style','Slider', 'Units','Normalized','Position',[0.05 0.025, 0.75 0.05],...
                   'Min',1,'Max',size(data,3),'Value',1, 'Callback',{@sliderChange,a});
l =   uicontrol(f, 'Style','listbox','Units','Normalized','Position',[0.85 0.15, 0.1, 0.75],...
                   'String',cellstr(num2str([1:size(data,1)]')),'Callback',{@changeChannel,a,s,data});
c = uicontrol(f, 'Style','pushbutton','Units','Normalized','String','Not clean',...
                   'Position',[0.85 0.025 0.1 0.05],'Callback',{@notClean,s,a});

stepSize = 1/(s.Max - s.Min);
s.SliderStep = [stepSize 2*stepSize];               
changeChannel(l,[],a,s,data)

function changeChannel(l,evtData,a,s,data)
cla(a);
chanNum = str2double(l.String{l.Value});
sR = 500;  %500Hz
tempData = reshape(data(chanNum,:,:),[],size(data,3)); %Reshape each epoch into a column
tempTime = [0:1/sR:(size(data,2)-1)/sR]' + (0:1:size(data,3)-1)*2; %Build time array
plot(a,tempTime,tempData) %plot all the lines
s.Value = 1; %Rest Slider Position

function sliderChange(s,evtData,a)
global clean
viewI = round(s.Value);
if clean(viewI) == 0
    s.Value = s.Value+1;
    sliderChange(s,[],a)
else
    disp(['Current Epoch: ' num2str(viewI)]) %console print
    xlim(a,[(viewI-1)*2 viewI*2] + [-.1 .1])  
end


function notClean(c,evtData,s,a)
global clean %Call the global
num = round(s.Value);
clean(num) = 0;
disp(['Epoch ' num2str(num) ' not clean']) %console print
%Advance to the next slider position.
s.Value = num+1;
sliderChange(s,[],a)

由于