Criação de documentos no Google drive com base em eventos em sistemas CRM: amoCRM e Bitrix24

Caso prático de como desenvolver seu próprio cenário de integração

Os serviços do Google para trabalhar com documentos de texto (Google Docs) e planilhas (Google Sheets), que surgiram em 2006, foram complementados 6 anos depois com a capacidade de trabalhar com um disco virtual (Google Drive) e conquistaram o amor generalizado entre os usuários, privando Microsoft de seu monopólio de décadas de trabalho com software de escritório.





, , , Google-, , . Google-.





Google- CRM-. , « » , CRM- , Google Drive , , . , Google- .





CRM- Google 24 .





, 24 amoCRM . , ?





, - Google – : REST API Google Script. , REST API Google , , create . , , , API .





, , . - , , , CRM-.





1.

, , ID .





script .





, , Javascript, Google.





2. Google Cloud Platform

, «» . Google Cloud Platform. Enable Google Script API : https://developers.google.com/apps-script/api/quickstart/php.





API, credentials.json, . .





3.

, .





, PHP-.





4. PHP-

API Google , : composer require google/apiclient:^2.0  





:





function getClient()
{
    $client = new Google_Client();
    $client->setApplicationName('Google Apps Script API PHP Quickstart');
    //      
    $client->setScopes("https://www.googleapis.com/auth/documents https://www.googleapis.com/auth/drive");
    //  credential.json,   Google
$client->setAuthConfig('credentials.json');
    $client->setAccessType('offline');
    $client->setPrompt('select_account consent');

    //   token.json
    $tokenPath = 'token.json';
    if (file_exists($tokenPath)) {
        $accessToken = json_decode(file_get_contents($tokenPath), true);
        $client->setAccessToken($accessToken);
    }

    //    
    if ($client->isAccessTokenExpired()) {
        if ($client->getRefreshToken()) {
            $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        } else {
            $authUrl = $client->createAuthUrl();
            printf("Open the following link in your browser:\n%s\n", $authUrl);
            print 'Enter verification code: ';
            $authCode = trim(fgets(STDIN));

            $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
            print_r($accessToken);
            $client->setAccessToken($accessToken);
            
            if (array_key_exists('error', $accessToken)) {
                throw new Exception(join(', ', $accessToken));
            }
        }

        //    
        if (!file_exists(dirname($tokenPath))) {
            mkdir(dirname($tokenPath), 0700, true);
        }
        file_put_contents($tokenPath, json_encode($client->getAccessToken()));
    }
    return $client;
}
      
      



ssh – “php ( )”.





url, verification code url , 1, . (token.json), PHP-. ,  .





, , . 





//        
$client = getClient();
$service = new Google_Service_Script($client);

//   
$scriptId = '***********************wKhTTdKL7ChremS5AkvzwlPJARnxqisW7TzDB ';

$request = new Google_Service_Script_ExecutionRequest();
//       
$request->setFunction('FillTemplate');
$request->setParameters([$_REQUEST['customer'],$_REQUEST['link'],$_REQUEST['formsv'],$_REQUEST['socnet'],$_REQUEST['whatsi'],   $_REQUEST['ats'],$_REQUEST['pipeline'],$_REQUEST['outscope'],$_REQUEST['editor'],$_REQUEST['viewer']]);

try {
    // Make the API request.
    $response = $service->scripts->run($scriptId, $request);
    if ($response->getError()) {
        //  

        $error = $response->getError()['details'][0];
        printf("Script error message: %s\n", $error['errorMessage']);

        if (array_key_exists('scriptStackTraceElements', $error)) {
            print "Script error stacktrace:\n";
            foreach($error['scriptStackTraceElements'] as $trace) {
                printf("\t%s: %d\n", $trace['function'], $trace['lineNumber']);
            }
        }
    }     
} catch (Exception $e) {
    //  
    echo 'Caught exception: ', $e->getMessage(), "\n";
}
      
      



5. PHP- CRM

, CRM.





CRM , . c :





  • amoCRM;





  • 24.





, 24 -, Get- , -.





Google Drive:





URL, Get-. , 4 ( 2048 ).





Portanto, se o texto transferido para o documento for longo o suficiente, recomendamos que os usuários do Bitrix24 escrevam sua própria ativação de processo de negócios. Informações sobre abordagens para seu desenvolvimento podem ser obtidas  neste artigo








All Articles