I want to write my bot for the group, but it does not send messages.
if (!isset($_REQUEST)) { return; } $confirmationToken = 'XXX'; $token = 'XXX'; $secretKey = 'XXX'; $data = json_decode(file_get_contents('php://input')); if(strcmp($data->secret, $secretKey) !== 0 && strcmp($data->type, 'confirmation') !== 0) return; switch ($data->type) { case 'confirmation': die($confirmationToken); break; case 'message_new': $userId = $data->object->user_id; //эта строка на сервере выдаёт ошибку $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); $user_name = $userInfo->response[0]->first_name;//эта строка на сервере выдаёт ошибку $request_params = array( 'message' => "{$user_name}, ваше сообщение отправлено!<br>". "Мы постараемся ответить в ближайшее время.", 'user_id' => $userId, 'access_token' => $token, 'v' => '5.0' ); $get_params = http_build_query($request_params); file_get_contents('https://api.vk.com/method/messages.send?' . $get_params); echo('ok'); break; case 'group_join': $userId = $data->object->user_id; $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); $user_name = $userInfo->response[0]->first_name; $request_params = array( 'message' => "Добро пожаловать в наше сообщество, {$user_name}!<br>" . "Если у Вас возникнут вопросы, то вы всегда можете обратиться к администраторам сообщества.<br>" . "Их контакты можно найти в соответсвующем разделе группы.<br>", 'user_id' => $userId, 'access_token' => $token, 'v' => '5.0' ); $get_params = http_build_query($request_params); file_get_contents('https://api.vk.com/method/messages.send?' . $get_params); echo('ok'); break; case 'group_leave': $userId = $data->object->user_id; $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); $user_name = $userInfo->response[0]->first_name; $request_params = array( 'message' => "{$user_name}, нам очень жаль прощаться с вами 😔<br>" . "Мы всегда будем ждать Вас в нашей уютной компании.<br>", 'user_id' => $userId, 'access_token' => $token, 'v' => '5.0' ); $get_params = http_build_query($request_params); file_get_contents('https://api.vk.com/method/messages.send?' . $get_params); echo('ok'); break; } echo('ok'); ?> When sending a message to a group, the following ones come to the server in error_log:
[30-Aug-2018 11:50:10 UTC] PHP Notice: Undefined property: stdClass::$user_id in /xxx/index.php on line 34 [30-Aug-2018 11:50:11 UTC] PHP Notice: Undefined property: stdClass::$response in /xxx/index.php on line 39 [30-Aug-2018 11:50:11 UTC] PHP Notice: Trying to get property 'first_name' of non-object in /xxx/index.php on line 39 And when joining the group these:
[30-Aug-2018 11:55:05 UTC] PHP Notice: Undefined property: stdClass::$response in /xxx/index.php on line 68 [30-Aug-2018 11:55:05 UTC] PHP Notice: Trying to get property 'first_name' of non-object in /xxx/index.php on line 68 It cannot get user_id and everything else.