如何在 Telegram 中的 answercallbackquery 之后发送消息? [英] How to sendmessage after answercallbackquery in Telegram?

查看:87
本文介绍了如何在 Telegram 中的 answercallbackquery 之后发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 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!

这篇关于如何在 Telegram 中的 answercallbackquery 之后发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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