如何使用PHP中的callback_data或方法解决错误 [英] How to solve a error with callback_data or method in PHP

查看:234
本文介绍了如何使用PHP中的callback_data或方法解决错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个想法是要有一些按钮来做某事,我得到了以下代码,并且方法(sendMessage)或callback_data出了点问题,因为在$ message所在的所有行中,我都收到了未定义的索引错误,如果我使用url代替了回调数据,效果很好

The idea is to have some buttons to do something, I got the following code, and something is wrong with the method (sendMessage) or callback_data because I received a undefined index error in all the rows where is the $message, if I use a url instead of a call back data works fine

我使用了一个Webhook

I use a webhook

<?php

$botToken = "TOKEN";
$website = "https://api.telegram.org/bot".$botToken;

$update = file_get_contents('php://input');
$update = json_decode($update, TRUE);

$chatId = $update["message"]["chat"]["id"];

$message = $update["message"]["text"];

$response ="testing";
$keyboard = [
    'inline_keyboard' => [
        [
            ['text' => 'This is a test', 'callback_data' => 'testcompleted']
        ]
    ]
];

$parameters = 
    array(
        'chat_id' => $chatId, 
        'text' => $response, 
        'reply_markup' => json_encode($keyboard)
    );

send($parameters);

function send($data)
{
    $url = "https://api.telegram.org/botTOKEN/sendMessage";

    if (!$curld = curl_init()) {
        exit;
    }
    curl_setopt($curld, CURLOPT_POST, true);
    curl_setopt($curld, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curld, CURLOPT_URL, $url);
    curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($curld);
    curl_close($curld);
    return $output;
}
?>

推荐答案

如果请求是回调,则应获取如下数据:

In case of the request being a callback you should get the data like this:

$chatId = $update['callback_query']['message']['chat']['id'];

$message = $update['callback_query']["message"]["text"];

您可以检查isset($update['callback_query'])并根据结果获取数据.

you can check isset($update['callback_query']) and get the data based on the result.

这篇关于如何使用PHP中的callback_data或方法解决错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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