It is necessary to choose from all friends, those who have not been sent a single message. If you do a search for a substring in a message, then everything works, but on the contrary, to select all who this message was not sent will not be received. Below is the working code in which there is all the users to whom a message was sent with the text - "Text for an example". Here is the code:

<?php header('Content-Type: text/html; charset=utf-8'); /** * Example 2. * Get access token via OAuth and usage VK API. * @link http://vk.com/developers.php VK API */ error_reporting(E_ALL); require_once('src/VK/VK.php'); require_once('src/VK/VKException.php'); $vk_config = array( 'app_id' => '******', 'api_secret' => '***************', 'callback_url' => '', 'api_settings' => 'messages,friends', 'token' => '*************************************************' ); try { $vk = new VK\VK($vk_config['app_id'], $vk_config['api_secret'], $vk_config['token']); $access_token = $vk_config['token']."<br>"; echo 'access token: ' . $access_token; $user_friends = $vk->api('friends.get', array( 'uid' => '*********', 'fields' => 'uid,first_name,last_name', 'order' => 'name' )); $user_send_messeges = $vk->api('messages.get', array( 'out' => '1', 'count' => '200', 'time_offset' => '0', 'offset' => '200' )); foreach ($user_send_messeges['response'] as $k => $v) { if (preg_match('!Текст для примера!si',$v['body'])) { foreach ($user_friends['response'] as $key => $value) { if ($value['uid'] == $v['uid']) { echo "<hr>"; echo $value['uid']." - ". $value['first_name']." ".$value['last_name'].": ".$v['body']; } } } } } catch (VK\VKException $error) { echo $error->getMessage(); } 

    1 answer 1

    I want to say that calling messages.get will probably be wrong. After all, for all the time the user may have a cloud of different dialogues, i.e. not only with ordinary users, but also with a group of users. You probably need to call the messages.getHistory method, which returns the message history for the specified dialog. So we can choose it only for friends.

    I can offer a slightly different approach, but which can be altered under the php code, if it pleases.

    Vkontakte itself allows the use of a custom query execute - a method that allows you to run a sequence of other methods, saving and filtering intermediate results. You can use it and create such a query:

     var friends = API.friends.get({fields:"first_name, last_name", count:20, offset:0}).items; var friendsWithNoMessages = []; var i = 0; while (i < friends.length) { var msgCount = API.messages.getHistory({user_id: friends[i]}).count; if (msgCount == 0) { friendsWithNoMessages.push(friends[i]); } i = i + 1; }; return {"friends":friends, "friendsWithNoMessages" : friendsWithNoMessages}; 

    Where to return "friends":friends , in principle, not necessary. There is a friend's id, first and last name. But friendsWithNoMessages will return an array of those with whom there are no messages even once.

    However (!!!) Because Since Vkontakte has a limit on the number of hits per second, you will have to repeat this request several times, falling asleep for a certain time interval, for example, 300 ms and shifting the offset each time by 20 friends.

    However, 2 (!!!) If there was no personal correspondence between friends, but they corresponded in some group chat, then such a request, of course, will not return anything.

    • Is there any option to implement this not in javascript, but in php? - Vadim
    • one
      @ Vadim well, who forbids it?)) And, instead of API.friends.get write $vk->api('friends.get' with input data ......... similarly with messages.getHistory .... ..I therefore wrote that the подход, который можно будет переделать под php ..... in fact, nothing will change. You will also have to call this block of code several times, falling asleep for a certain period of time - Alexey Shimansky
    • and how to deal with a limit of 200 messages, tried to change the offset value through a cycle, did not work, I do something wrong. Here's the code: while ($user_send_messeges['response'][$i]) { echo $user_send_messeges['response'][$i]['uid']."<br>"; ++$i; $user_send_messeges = $vk->api('messages.get', array( 'out' => '1', 'count' => '200', 'time_offset' => '0', 'offset' => $i )); } while ($user_send_messeges['response'][$i]) { echo $user_send_messeges['response'][$i]['uid']."<br>"; ++$i; $user_send_messeges = $vk->api('messages.get', array( 'out' => '1', 'count' => '200', 'time_offset' => '0', 'offset' => $i )); } while ($user_send_messeges['response'][$i]) { echo $user_send_messeges['response'][$i]['uid']."<br>"; ++$i; $user_send_messeges = $vk->api('messages.get', array( 'out' => '1', 'count' => '200', 'time_offset' => '0', 'offset' => $i )); } - Vadim