N
N
nastyskafomka2018-08-29 18:10:41
PHP
nastyskafomka, 2018-08-29 18:10:41

VK_API how to send random images?

How to send a random image to a user that is in a group album?
That is, a command came, for example, 1. And the bot sends one random image from the group's album to the user.
api version 5.50
Here is the code, it works, but only a message with the id of the pub comes in response and that's it (see 'message') And the id of the photo should also be.

case 'r':
        $user_id        = $data->object->user_id;
        unset($data);
        $photo_list = json_decode(file_get_contents("https://api.vk.com/method/photos.get?owner_id=-".$pub."&album_id=wall&access_token=".$service_token."&v=".$api_version), true);
        print_r($photo_list);
        $photo_id = $photo_list['response']['items']['id'];
        //unset($photo_list);
        $rand = array_rand($photo_id, 1);
        unset($photo_id);
        $photo = 'photo-{$pub}_{$rand}';
        $request_params = [
          'message'       => "photo-{$pub}_{$rand}",
          'user_id'       => $user_id,
          'attachment'    => $photo,
          'access_token'  => $token,
          'v'             => $api_version
        ];
        $get_params = http_build_query($request_params);
        file_get_contents('https://api.vk.com/method/'. $msg_send . $get_params);
        echo ('ok');
        break;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2018-08-29
@sergiks

  1. get a list of images in a group album: photos.get()
  2. choose N random
  3. send them as message attachments: field attachmentin messages.send()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question