B
B
Bagens2017-08-03 11:58:27
PHP
Bagens, 2017-08-03 11:58:27

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.

Part of the code
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

2 answer(s)
P
Pavel Gromadchuk, 2017-08-03
@Gromadchuk

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. The user writes to the community
  2. The user will allow access to the community through the API using the messages.allowMessagesFromGroup method
  3. The user will allow access using a special widget for sites

U
userfordownload, 2017-08-04
@userfordownload

1. alarming - it's 'v' => '5.0'.
working 100%.
c33489f624594a51a6e343c52b5e8104.JPG

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;
            }
        }
    }

Use :
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 question

Ask a Question

731 491 924 answers to any question