Creation of documents on Google drive based on events in CRM systems: amoCRM and Bitrix24

Practical case on how to develop your own integration scenario

Introduced in 2006, Google Docs and Google Sheets, supplemented 6 years later with Google Drive capabilities, have won widespread love among users, depriving Microsoft of its decades-old monopoly on working with office software.





, , , 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 ).





Therefore, if the text transferred to the document is long enough, then we recommend that Bitrix24 users write their own business process activation. Information on approaches to its development can be taken from  this article








All Articles