Задача: отправлять данные в SendGrid (в разные группы контактов)
|
через composer поставила https://packagist.org/packages/sendgrid/sendgrid
и написала класс SendGridHelper
namespace nanocad\dev\sendgrid; use Bitrix\Main\Loader, nanocad\dev\user\orm\UserTable; class SendGridHelper{ protected const API_KEY = "XXXXXXXX"; //ключ API const GROUPS_ID = [ "customer" => "fba1a6d8-2766-4ce8-924d-0b36faf6ca1c", "free" => "03f2b04f-29f3-4c81-87b8-79a82caaac58", "trial" => "5024e64e-e131-471e-843a-9250442442c5", ]; const CUSTOM_FIELDS_ID = [ "client_id"=>'e8_N', "date_free" =>'e11_D', "date_trial" =>'e13_D', "date_customer" => 'e14_D' ]; protected function AddContact($arData){ $sg = new \SendGrid(self::API_KEY); try { $response = $sg->client->marketing()->contacts()->put($arData); } catch (Exception $e) { //echo 'Caught exception: ', $e->getMessage(), "\n"; } } protected function AddContactToGroupByUser($user,$group){ $arData = [ "contacts" => [ [ "email" => $user->getLogin(), "first_name" => $user->getName(), "last_name" => $user->getLastName(), "custom_fields" => [ self::CUSTOM_FIELDS_ID["client_id"]=> $user->getId(), self::CUSTOM_FIELDS_ID["date_".$group]=> date("m/d/Y") ] ] ], "list_ids" => [self::GROUPS_ID[$group]] ]; self::AddContact($arData); } protected function AddContactToGroupByEmail($email,$group){ if($user = UserTable::getBylogin($email)->fetchObject()) { self::AddContactToGroupByUser($user,$group); } } public static function AddToCustomersByUser($user){ self::AddContactToGroupByUser($user,"customer"); } public static function AddToFreeDownloadersByUser($user){ self::AddContactToGroupByUser($user,"free"); } public static function AddToTrialDownloadersByUser($user){ self::AddContactToGroupByUser($user,"trial"); } public static function AddToCustomersByEmail($email){ if($user = UserTable::getBylogin($email)->fetchObject()) { self::AddToCustomersByUser($user); } } public static function AddToFreeDownloadersByEmail($email){ if($user = UserTable::getBylogin($email)->fetchObject()) { self::AddToFreeDownloadersByUser($user); } } public static function AddToTrialDownloadersByEmail($email){ if($user = UserTable::getBylogin($email)->fetchObject()) { self::AddToTrialDownloadersByUser($user); } } public static function AddToCustomersByUserId($userId){ if($user = UserTable::getByPrimary($userId)->fetchObject()) { self::AddToCustomersByUser($user); } } public static function AddToFreeDownloadersByUserId($userId){ if($user = UserTable::getByPrimary($userId)->fetchObject()) { self::AddToFreeDownloadersByUser($user); } } public static function AddToTrialDownloadersByUserId($userId){ if($user = UserTable::getByPrimary($userId)->fetchObject()) { self::AddToTrialDownloadersByUser($user); } } public static function PrintAllFields(){ //выводит все поля $sg = new \SendGrid(self::API_KEY); try { $response = $sg->client->marketing()->field_definitions()->get(); print $response->statusCode() . "\n"; echo "<pre>"; print_r($response->headers()); print_r(json_decode($response->body())); echo "</pre>"; } catch (Exception $ex) { echo 'Caught exception: '. $ex->getMessage(); } } public static function CheckContactBySGId($id){//проверяет результат по job_id $sg = new \SendGrid(self::API_KEY); try { $response = $sg->client->marketing()->contacts()->imports()->_($id)->get(); echo "<pre>"; print $response->statusCode() . "\n"; print_r($response->headers()); print_r(json_decode($response->body())); echo "</pre>"; } catch (Exception $ex) { echo 'Caught exception: '. $ex->getMessage(); } } } |
use nanocad\dev\sendgrid\SendGridHelper; //распечатать все поля SendGridHelper::PrintAllFields(); $email = 'vmalysheva@nanocad.com'; SendGridHelper::AddToFreeDownloadersByEmail($email); SendGridHelper::CheckContactBySGId("a232e63c-f299-4b7a-bb18-9696bed4a821"); |