如何使用URL创建答案内联查询

时间:2017-10-22 21:16:35

标签: php bots telegram

如何使用URL创建AnswerInlineQuery?我找不到该查询的更多细节,所有示例代码都是关于Telegram SDK,我需要这样编码(URL post,not class)

我试过了:

$data = array();
$data['inline_query_id']= $inline_query_id;//sample : 1515027006022511114
$data['results']='[
  {
    "type": "article",
    "id": "001",
    "title": "UC Browser",
    "message_text": "Text of the first message",
    "parse_mode": "Markdown",
    "disable_web_page_preview": true,
    "url": "telegram.com",
    "hide_url": true,
    "description": "Optional. Short description of the result",
    "thumb_url": "http://icons.iconarchive.com/icons/martz90/circle/64/uc-browser-icon.png",
    "thumb_width": 64,
    "thumb_height": 64
  },
  {
    "type": "article",
    "id": "002",
    "title": "Bitcoin",
    "message_text": "*Text of the second message*",
    "parse_mode": "Markdown",
    "disable_web_page_preview": true,
    "url": "bitcoin.org",
    "hide_url": true,
    "description": "Short description of the result",
    "thumb_url": "http://www.coinwarz.com/content/images/bitcoin-64x64.png",
    "thumb_width": 64,
    "thumb_height": 64
  }
]';

$ch = curl_init();
$curlConfig = array(
    CURLOPT_URL => 'https://api.telegram.org/bot' . TOKEN . '/answerInlineQuery',
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SAFE_UPLOAD => true,
    CURLOPT_POSTFIELDS => 'params=' . json_encode($data),
    CURLOPT_VERBOSE => true,
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);

但结果是:

Result : {"ok":false,"error_code":400,"description":"Bad Request: QUERY_ID_INVALID"}

我也尝试了这个:

 $mResult = '[
      {
        "type": "article",
        "id": "001",
        "title": "UC Browser",
        "message_text": "Text of the first message",
        "parse_mode": "Markdown",
        "disable_web_page_preview": true,
        "url": "telegram.com",
        "hide_url": true,
        "description": "Optional. Short description of the result",
        "thumb_url": "http://icons.iconarchive.com/icons/martz90/circle/64/uc-browser-icon.png",
        "thumb_width": 64,
        "thumb_height": 64
      },
      {
        "type": "article",
        "id": "002",
        "title": "Bitcoin",
        "message_text": "*Text of the second message*",
        "parse_mode": "Markdown",
        "disable_web_page_preview": true,
        "url": "bitcoin.org",
        "hide_url": true,
        "description": "Short description of the result",
        "thumb_url": "http://www.coinwarz.com/content/images/bitcoin-64x64.png",
        "thumb_width": 64,
        "thumb_height": 64
      }
    ]';
$url= "https://api.telegram.org/bot" . TOKEN . "/answerInlineQuery?inline_query_id=" . $inline_query_id . "&results=" . json_encode($mResult);
file_get_contents($url);

然后返回:

Result : {"ok":false,"error_code":400,"description":"Bad Request: QUERY_ID_INVALID"}

2 个答案:

答案 0 :(得分:1)

试试这个:

$mResult = [
    [
        "type" => "article",
        "id" => "first",
        "title" => "Hey",
        "input_message_content" => ["message_text" => "Hello World"],
        "parse_mode" => "Markdown",
    ]
];
$url= "https://api.telegram.org/bot" . TOKEN . "/answerInlineQuery?inline_query_id={$inline_query_id}&results=" . json_encode($mResult);
file_get_contents($url);

答案 1 :(得分:0)

只是对以上给出的答案进行了更新,docs中的内容有所变化,没有名为message_text的字段,现在称为input_message_content

所以你可以尝试

$inlineResult = [
[
    "type" => "article",
    "id" => rand(1, 100), //this has to be unique for each response
    "title" => "TITLE",
    "input_message_content" => [
      "message_text" => "*Hello World*",
      "parse_mode" => "Markdown",
     ],
] ];


$url= "https://api.telegram.org/bot" . TOKEN . "/answerInlineQuery?inline_query_id={$inline_query_id}&results=" . json_encode($inlineResult);
file_get_contents($url);