@mdest1

Ошибка в Telegram PHP Bot?

Код
$data = json_decode(file_get_contents('php://input'));

if (isset($data)) {
    $message = Array(
        'chat_id' => $data->{'message'}->{'chat'}->{'id'},
        'message_id' => $data->{'message'}->{'message_id'},
        'text' => $data->{'message'}->{'text'},
        'from' => $data->{'message'}->{'from'}->{'id'},
        'username' => $data->{'message'}->{'from'}->{'username'},
        'firstname' => $data->{'message'}->{'from'}->{'first_name'},
        'lastname' => $data->{'message'}->{'from'}->{'last_name'},
        'new_chat_member' => $data->{'message'}->{'new_chat_member'},
        'left_chat_member' => $data->{'message'}->{'left_chat_member'}
    );

    $callback = Array(
        'id' => $data->{'callback_query'}->{'id'},
        'type' => $data->{'callback_query'}->{'data'},
        'chat_id' => $data->{'callback_query'}->{'message'}->{'chat'}->{'id'},
        'from' => $data->{'callback_query'}->{'from'}->{'id'},
        'username' => $data->{'callback_query'}->{'from'}->{'username'},
        'firstname' => $data->{'callback_query'}->{'from'}->{'first_name'},
        'lastname' => $data->{'callback_query'}->{'from'}->{'last_name'},
        'message_id' => $data->{'callback_query'}->{'message'}->{'message_id'},
        'message_text' => $data->{'callback_query'}->{'message'}->{'text'}
    );
}


Ошибки

[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Undefined property: stdClass::$last_name in F:\OpenServer\domains\localhost\config.php on line 137
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Undefined property: stdClass::$new_chat_member in F:\OpenServer\domains\localhost\config.php on line 138
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Undefined property: stdClass::$left_chat_member in F:\OpenServer\domains\localhost\config.php on line 139
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Undefined property: stdClass::$callback_query in F:\OpenServer\domains\localhost\config.php on line 143
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'id' of non-object in F:\OpenServer\domains\localhost\config.php on line 143
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Undefined property: stdClass::$callback_query in F:\OpenServer\domains\localhost\config.php on line 144
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'data' of non-object in F:\OpenServer\domains\localhost\config.php on line 144
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Undefined property: stdClass::$callback_query in F:\OpenServer\domains\localhost\config.php on line 145
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'message' of non-object in F:\OpenServer\domains\localhost\config.php on line 145
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'chat' of non-object in F:\OpenServer\domains\localhost\config.php on line 145
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'id' of non-object in F:\OpenServer\domains\localhost\config.php on line 145
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Undefined property: stdClass::$callback_query in F:\OpenServer\domains\localhost\config.php on line 146
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'from' of non-object in F:\OpenServer\domains\localhost\config.php on line 146
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'id' of non-object in F:\OpenServer\domains\localhost\config.php on line 146
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Undefined property: stdClass::$callback_query in F:\OpenServer\domains\localhost\config.php on line 147
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'from' of non-object in F:\OpenServer\domains\localhost\config.php on line 147
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'username' of non-object in F:\OpenServer\domains\localhost\config.php on line 147
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Undefined property: stdClass::$callback_query in F:\OpenServer\domains\localhost\config.php on line 148
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'from' of non-object in F:\OpenServer\domains\localhost\config.php on line 148
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'first_name' of non-object in F:\OpenServer\domains\localhost\config.php on line 148
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Undefined property: stdClass::$callback_query in F:\OpenServer\domains\localhost\config.php on line 149
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'from' of non-object in F:\OpenServer\domains\localhost\config.php on line 149
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'last_name' of non-object in F:\OpenServer\domains\localhost\config.php on line 149
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Undefined property: stdClass::$callback_query in F:\OpenServer\domains\localhost\config.php on line 150
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'message' of non-object in F:\OpenServer\domains\localhost\config.php on line 150
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'message_id' of non-object in F:\OpenServer\domains\localhost\config.php on line 150
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Undefined property: stdClass::$callback_query in F:\OpenServer\domains\localhost\config.php on line 151
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'message' of non-object in F:\OpenServer\domains\localhost\config.php on line 151
[11-Aug-2021 23:01:31 Europe/Moscow] PHP Notice: Trying to get property 'text' of non-object in F:\OpenServer\domains\localhost\config.php on line 151

В чем проблема? Помогите решить
  • Вопрос задан
  • 480 просмотров
Пригласить эксперта
Ответы на вопрос 4
@kot999
Backend software engineer
в чем проблема - говорит интерпретатор. обращение к несуществующим свойствам объекта. в json эти поля не пришли.
Ответ написан
nastya97core
@nastya97core
Начинающий программист
сделай:
echo '<pre>';
print_r($data);
echo '</pre>';
Ответ написан
@notlive
100% рабочий вариант...

$callbackData = file_get_contents("php://input");
$ac = json_decode($callbackData, true);

if (isset($ac["message"]))
{
//Информация об отправителе комманды
$idMessage = $ac['message']['message_id'];
$fromGroup = (empty($ac['message']['chat']['id'])) ? 0 : $ac['message']['chat']['id'];
$fromUser = $ac['message']['from']['id'];
$from = ($fromGroup != 0) ? $fromGroup : $fromUser;
$user_info = $ac['message']['from'];
$command = $ac['message']['text'];
} else if (isset($ac["callback_query"])) {
//Информация об отправителе комманды
$idMessage = $ac['callback_query']['message']['message_id'];
$fromGroup = (empty($ac['callback_query']['message']['chat']['id'])) ? 0 : $ac['callback_query']['message']['chat']['id'];
$fromUser = $ac['callback_query']['from']['id'];
$from = ($fromGroup != 0) ? $fromGroup : $fromUser;
$user_info = $ac['callback_query']['from'];
$command = $ac['callback_query']['data'];
$editMessage = true;
}
Ответ написан
Rsa97
@Rsa97
Для правильного вопроса надо знать половину ответа
Читайте документацию на Telegram Bot API. Там указано, какие поля являются опциональными. Добавляйте обработку таких полей, например через null-коалесценцию:
'lastname' => $data->message->from->last_name ?? null,
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы