Answer the question
In order to leave comments, you need to log in
How to make a PHP bot create a dialog with a user who has joined a group?
The essence of the question is described in the title. There is a bot that responds perfectly and if it has communicated with it before, it notifies you with a message about leaving and entering the group.
But if a person is new, has not communicated with this community in any way, then silence. does not create a new dialog.
case 'group_join':
$userId = $data->object->user_id;
$userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$use..."));
$user_name = $userInfo->response[0]->first_name;
$request_params = array(
'message' => "Добро пожаловать в наше официальное сообщество {$user_name}",
'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);
//Возвращаем "ok" серверу Callback API
echo('ok');
break;
Answer the question
In order to leave comments, you need to log in
The privacy policy of VKontakte prohibits communities from writing to the first users, even if he joined the group.
There are several options after which the community will be able to write to the user:
1. alarming - it's 'v' => '5.0'.
working 100%.
private $token = 'хххххххххххххххххххххххххххххххххххххххххххххххх';
private $_ok = 'OK';
public function messages_send ($paramm = []){
if (!$paramm) {return false;}
else {
$url = trim('https://api.vk.com/method/messages.send?'.http_build_query($paramm));
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
// curl_setopt($curl, CURLOPT_ENCODING ,"");
$_id_message = json_decode($data)->response;
if (isset ($_id_message)) {
if (is_int($_id_message)){
return $this->_ok;
}
} else {
return $data;
}
}
}
case 'group_leave':
$request_params = array (
'message' => 'Ливнул',
'user_id' =>$callback_object->object->user_id,
'access_token' => $this->token,
'read_state' => 1,
);
return $this-> messages_send ($request_params);
break;
case 'group_join':
$request_params = array (
'message' => 'Добро пожаловать!',
'user_id' =>$callback_object->object->user_id,
'access_token' => $this->token,
'read_state' => 1,
);
return $this-> messages_send ($request_params);
break;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question