PHP的电报answercallbackquery sendmessage [英] php telegram answercallbackquery sendmessage

查看:208
本文介绍了PHP的电报answercallbackquery sendmessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用PHP开发Telegram Bot,但是当他按下嵌入式按钮时,我的机器人未能回答用户. 有人可以在调用answerCallback方法后帮助我发送消息(sendMessage方法)吗?

I'm trying to develop a Telegram Bot in PHP, but i failed to make my bot answer the user when he press an inline button. Can someone help me sending a message (sendMessage method) after calling the answerCallback method?

这是我最后的试用代码:

Here's my last trial code:

if ($call_back_query != null) {

    $response = $call_back_query;
    $botUrl = "https://api.telegram.org/bot" . BOT_TOKEN . "/answerCallbackQuery";
    $postFields = array('callback_query_id' =>  $call_back_id, 'text' => $response);
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
    curl_setopt($ch, CURLOPT_URL, $botUrl); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    $output = curl_exec($ch);

        //send Text
    $response = "help me if you can, i'm feeling down";
    header("Content-Type: application/json");
    $parameters = array('chat_id' => $chatId, "text" => $response);
    $parameters["method"] = "sendMessage";
    echo json_encode($parameters);


  }

谢谢

推荐答案

好的,我已经理解了这个问题:

Ok, i've understood the problem:

在sendText中,我必须使用$call_back_from而不是$chatId,因为他正在回答回调查询,而不是简单的消息(我想是...)

In sendText i have to use $call_back_from instead of $chatId, because he's answering to the callback query and not a simple message (i think so...)

同时

$chatId = isset($message['chat']['id']) ? $message['chat']['id'] : "";

$call_back_from = isset ($update['callback_query']['from']['id']) ? $update['callback_query']['from']['id'] : "";

代码将是

if ($call_back_query != null) {

    $response = $call_back_query;
    $botUrl = "https://api.telegram.org/bot" . BOT_TOKEN . "/answerCallbackQuery";
    $postFields = array('callback_query_id' =>  $call_back_id, 'text' => $response);
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
    curl_setopt($ch, CURLOPT_URL, $botUrl); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    $output = curl_exec($ch);

        //send Text
    $response = "help me if you can, i'm feeling down";
    header("Content-Type: application/json");
    $parameters = array('chat_id' => $call_back_from, "text" => $response);
    $parameters["method"] = "sendMessage";
    echo json_encode($parameters);


  }

希望这会有所帮助! 尝试一下!

Hope this could be helpful! Have a nice try!

这篇关于PHP的电报answercallbackquery sendmessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆