如何使用电报内联机器人发送消息

时间:2016-07-06 11:47:59

标签: php telegram telegram-bot

我已设置了我的webhook并且它为我提供了更新,但我不知道如何使用InlineQueryResult发送php

我想发送这样的文字:

aaaaaaaaaaabbbbbbbbbcccccc

如何使用php curl发送它?这就是我目前的尝试:

$token = 'bot###';
$chat_id = '###';

$keyboardl = ['inline_keyboard' => [[['text' =>  "one", 'callback_data' => "1"],['text' =>  "two", 'callback_data' => "2"]]]];

$data = array("chat_id" => $chat_id,"results" => "??????","reply_markup" => $keyboardl);

$data_string = json_encode($data);                                                                                   
$ch = curl_init('https://api.telegram.org/'.$token.'/answerinlinequery');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   
$result = curl_exec($ch);
echo $result;

} 

我不知道我必须使用哪个InlineQueryResult才能发送文字。

1 个答案:

答案 0 :(得分:8)

要使用InlineQuery发送文本,您需要使用InlineQueryResultArticle (doc),因此请将类型设置为article
您不需要设置chat_id,因为数据会自动在当前活动聊天中发送。它会通过与inline_query_id中收到的id对应的InlineQuery进行识别。

$results = array();

$entry = array(
    "type" => "article", 
    "id" => "1", 
    "title" => "Title", 
    "description" => "Description", 
    "input_message_content" => array("message_text" => "Text to be sent")
);

array_push($results, $entry);

$post = array(
    "inline_query_id" => $queryId, 
    "results" => json_encode($results)
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.telegram.org/bot" . $token . "/answerInlineQuery");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$content = curl_exec ($ch);
curl_close ($ch);