src/VkCommunityCallbackDriver.php
/**
     * Preparing attachments to be sent.
     *
     * @param IncomingMessage $matchingMessage
     * @param $attachment
     * @return array
     * @throws VKDriverException
     * @throws VKDriverException
     */
Preparing attachments to be sent.
Preparing attachments to be sent .
private function prepareAttachments($matchingMessage, $attachment){
        $ret = [];
        $peer_id = $matchingMessage->getRecipient();
        switch(get_class($attachment)){
            case "BotMan\BotMan\Messages\Attachments\Image":
                if(is_string($attachment->getExtras("vk_photo"))){
                    $ret[] = $attachment->getExtras("vk_photo");
                    break;
                }
                $getUploadUrl = $this->api("photos.getMessagesUploadServer", [
                    'peer_id' => $peer_id
                ], true);
                $uploadImg = $this->upload($getUploadUrl["response"]['upload_url'], $attachment->getUrl());
                if($uploadImg["photo"] == "[]")
                    throw new VKDriverException("Can't upload image to VK. Please, be sure photo has correct extension.");
                $saveImg = $this->api('photos.saveMessagesPhoto', [
                    'photo' => $uploadImg['photo'],
                    'server' => $uploadImg['server'],
                    'hash' => $uploadImg['hash']
                ], true);
                $ret[] = "photo".$saveImg["response"][0]['owner_id']."_".$saveImg["response"][0]['id'];
                break;
            case "BotMan\BotMan\Messages\Attachments\Video":
                if(is_string($attachment->getExtras("vk_video"))){
                    $ret[] = $attachment->getExtras("vk_video");
                    break;
                }
                throw new VKDriverException("Uploading videos with community token is not supported by VK API (uploading with user token is under construction)");
                break;
            case "BotMan\BotMan\Messages\Attachments\Audio":
                if(is_string($attachment->getExtras("vk_audio"))){
                    $ret[] = $attachment->getExtras("vk_audio");
                    break;
                }
                if(is_bool($attachment->getExtras("vk_as_voice")) && $attachment->getExtras("vk_as_voice") == true){
                    $this->api("messages.setActivity", [
                        "peer_id" => $matchingMessage->getRecipient(),
                        "type" => "audiomessage"
                    ], true);
                    $getUpload = $this->api("docs.getMessagesUploadServer", [
                        'peer_id' => $peer_id,
                        'type' => "audio_message"
                    ], true);
                    $upload = $this->upload($getUpload["response"]['upload_url'], $attachment->getUrl());
                    if($upload["file"] == "[]" || $upload["file"] == "" || $upload["file"] == null)
                        throw new VKDriverException("Can't upload audio to VK. Please, be sure audo has correct extension (OGG is preferred). Learn more: https://vk.com/dev/upload_files_2");
                    $save = $this->api('docs.save', [
                        'file' => $upload['file']
                    ], true);
                    $ret[] = "audio_message".$save["response"]["audio_message"]['owner_id']."_".$save["response"]["audio_message"]['id'];
                    break;
                }
                throw new VKDriverException("Uploading audio is restricted by VK API");
                break;
            case "BotMan\BotMan\Messages\Attachments\File":
                if(is_string($attachment->getExtras("vk_doc"))){
                    $ret[] = $attachment->getExtras("vk_doc");
                    break;
                }
                $getUpload = $this->api("docs.getMessagesUploadServer", [
                    'peer_id' => $peer_id,
                    'type' => "doc"
                ], true);
                $upload = $this->upload($getUpload["response"]['upload_url'], $attachment->getUrl());
                if(!isset($upload["file"]) || $upload["file"] == "[]" || $upload["file"] == "" || $upload["file"] == null)
                    throw new VKDriverException("Can't upload file to VK. Please, be sure file has correct extension.");
                $_ = [
                    'file' => $upload['file']
                ];
                if($attachment->getExtras("vk_doc_title") != null)
                    $_["title"] = $attachment->getExtras("vk_doc_title");
                if($attachment->getExtras("vk_doc_tags") != null)
                    $_["tags"] = $attachment->getExtras("vk_doc_tags");
                $save = $this->api('docs.save', $_, true);
                $ret[] = "doc".$save["response"]["doc"]['owner_id']."_".$save["response"]["doc"]['id'];
                break;
        }
        return $ret;
    }
private function prepareAttachments ( $ matchingMessage , $ attachment ) { $ ret = [ ] ; $ peer_id = $ matchingMessage -> getRecipient ( ) ; switch ( get_class ( $ attachment ) ) { case " BotMan\BotMan\Messages\Attachments\Image " : if ( is_string ( $ attachment -> getExtras ( " vk_photo " ) ) ) { $ ret [ ] = $ attachment -> getExtras ( " vk_photo " ) ; break ; } $ getUploadUrl = $ this -> api ( " photos.getMessagesUploadServer " , [ 'peer_id' => $ peer_id ] , true ) ; $ uploadImg = $ this -> upload ( $ getUploadUrl [ " response " ] [ 'upload_url' ] , $ attachment -> getUrl ( ) ) ; if ( $ uploadImg [ " photo " ] == " [] " ) throw new VKDriverException ( " Can't upload image to VK. Please, be sure photo has correct extension. " ) ; $ saveImg = $ this -> api ( 'photos.saveMessagesPhoto' , [ 'photo' => $ uploadImg [ 'photo' ] , 'server' => $ uploadImg [ 'server' ] , 'hash' => $ uploadImg [ 'hash' ] ] , true ) ; $ ret [ ] = " photo " . $ saveImg [ " response " ] [ 0 ] [ 'owner_id' ] . " _ " . $ saveImg [ " response " ] [ 0 ] [ 'id' ] ; break ; case " BotMan\BotMan\Messages\Attachments\Video " : if ( is_string ( $ attachment -> getExtras ( " vk_video " ) ) ) { $ ret [ ] = $ attachment -> getExtras ( " vk_video " ) ; break ; } throw new VKDriverException ( " Uploading videos with community token is not supported by VK API (uploading with user token is under construction) " ) ; break ; case " BotMan\BotMan\Messages\Attachments\Audio " : if ( is_string ( $ attachment -> getExtras ( " vk_audio " ) ) ) { $ ret [ ] = $ attachment -> getExtras ( " vk_audio " ) ; break ; } if ( is_bool ( $ attachment -> getExtras ( " vk_as_voice " ) ) && $ attachment -> getExtras ( " vk_as_voice " ) == true ) { $ this -> api ( " messages.setActivity " , [ " peer_id " => $ matchingMessage -> getRecipient ( ) , " type " => " audiomessage " ] , true ) ; $ getUpload = $ this -> api ( " docs.getMessagesUploadServer " , [ 'peer_id' => $ peer_id , 'type' => " audio_message " ] , true ) ; $ upload = $ this -> upload ( $ getUpload [ " response " ] [ 'upload_url' ] , $ attachment -> getUrl ( ) ) ; if ( $ upload [ " file " ] == " [] " || $ upload [ " file " ] == " " || $ upload [ " file " ] == null ) throw new VKDriverException ( " Can't upload audio to VK. Please, be sure audo has correct extension (OGG is preferred). Learn more: https://vk.com/dev/upload_files_2 " ) ; $ save = $ this -> api ( 'docs.save' , [ 'file' => $ upload [ 'file' ] ] , true ) ; $ ret [ ] = " audio_message " . $ save [ " response " ] [ " audio_message " ] [ 'owner_id' ] . " _ " . $ save [ " response " ] [ " audio_message " ] [ 'id' ] ; break ; } throw new VKDriverException ( " Uploading audio is restricted by VK API " ) ; break ; case " BotMan\BotMan\Messages\Attachments\File " : if ( is_string ( $ attachment -> getExtras ( " vk_doc " ) ) ) { $ ret [ ] = $ attachment -> getExtras ( " vk_doc " ) ; break ; } $ getUpload = $ this -> api ( " docs.getMessagesUploadServer " , [ 'peer_id' => $ peer_id , 'type' => " doc " ] , true ) ; $ upload = $ this -> upload ( $ getUpload [ " response " ] [ 'upload_url' ] , $ attachment -> getUrl ( ) ) ; if ( ! isset ( $ upload [ " file " ] ) || $ upload [ " file " ] == " [] " || $ upload [ " file " ] == " " || $ upload [ " file " ] == null ) throw new VKDriverException ( " Can't upload file to VK. Please, be sure file has correct extension. " ) ; $ _ = [ 'file' => $ upload [ 'file' ] ] ; if ( $ attachment -> getExtras ( " vk_doc_title " ) != null ) $ _ [ " title " ] = $ attachment -> getExtras ( " vk_doc_title " ) ; if ( $ attachment -> getExtras ( " vk_doc_tags " ) != null ) $ _ [ " tags " ] = $ attachment -> getExtras ( " vk_doc_tags " ) ; $ save = $ this -> api ( 'docs.save' , $ _ , true ) ; $ ret [ ] = " doc " . $ save [ " response " ] [ " doc " ] [ 'owner_id' ] . " _ " . $ save [ " response " ] [ " doc " ] [ 'id' ] ; break ; } return $ ret ; }
Preparing attachments to be sent.
Preparing attachments to be sent .
/** @var $attachment Image */ // Just return already uploaded photo // Otherwise, upload image to VK // TODO: throw exceptions if error // If error /** @var $attachment Video */ // Just return already uploaded video // TODO: upload video with user token feature /** @var $attachment Audio */ // Just return already uploaded audio // Send audio as voice message // Show "*bot* is recording audiomessage" caption // If error /** @var $attachment File */ // Just return already uploaded document // If error