发送内联查询结果错误(电报)

时间:2016-04-08 10:58:50

标签: php telegram telegram-bot

我想制作一个内联机器人!当我这样做时:

function sendResponse($url, $data){
    $ch = curl_init();
    //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('inline_query_id' => $data['inline_query_id'], 'results' => json_encode($data['results'])));
    $output = curl_exec($ch);
    return $output;
}

它不起作用,错误(有或没有标题):{"ok":false,"error_code":400,"description":"[Error]: Bad request: Field \"message_text\" must be of type String"}

但当我这样做的时候:

function sendResponse($url, $data){
    $ch = curl_init();
    //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
    curl_setopt($ch, CURLOPT_URL, $url.'?inline_query_id='.rawurlencode($data['inline_query_id']).'&results='.rawurlencode(json_encode($data['results']))); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    //curl_setopt($ch, CURLOPT_POST, 1);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $q);
    $output = curl_exec($ch);
    return $output;
}

有效!问题是第二个方法请求URI太大所以我不能用它!

我可以发送这些数据的任何方式都可以!谢谢!

和制作$ data的代码在这里:

$result = connectWebsite(SITE_SEARCH_URL, urlencode($update['inline_query']['query']));
$result = json_decode($result);
$output = array();
$output['inline_query_id'] = $update['inline_query']['id'];
$i = 0;
foreach($result as $post){
    $data = array();
    $data['type'] = 'article';
    $data['id'] = strval($post->ID);
    $data['title'] = '('.$post->atypes.') '.$post->title;
    if(strlen($post->content) > 2100)
        $tmp = substr($post->content, 0, 2096).'...';
    $data['message_text'] = '<b>'.$post->title.'</b>'.ucwords($post->genre, ',').$tmp;
    $data['parse_mode'] = 'HTML';
    if(strlen($post->content) > 200)
        $tmp = substr($post->content, 0, 196).'...';
    //$data['description'] = ucwords($post->genre, ',').'  |  '.$tmp;
    $output['results'][$i] = $data;
    $i++;
    if($i == MAX_RESULTS)
        break;
}   
sendResponse(API_URL.'answerInlineQuery', $output);

1 个答案:

答案 0 :(得分:1)

它可能对某人有帮助,所以我会自己回答。

问题是UTF-8编码

我将substr替换为mb_substr

除了在第一行我添加了这个:mb_internal_encoding("UTF-8")

并且......问题解决了。现在我可以在没有URL长度问题的情况下发送我的内联查询结果(或任何其他命令)

感谢大家的帮助