-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #420 from amocrm/feature/api_templates
Feature/api templates
- Loading branch information
Showing
27 changed files
with
1,016 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use AmoCRM\Collections\Chats\Templates\Buttons\ButtonsCollection; | ||
use AmoCRM\Exceptions\AmoCRMApiException; | ||
use AmoCRM\Models\Chats\Templates\Buttons\TextButtonModel; | ||
use AmoCRM\Models\Chats\Templates\TemplateModel; | ||
use League\OAuth2\Client\Token\AccessTokenInterface; | ||
|
||
include_once __DIR__ . '/bootstrap.php'; | ||
|
||
$accessToken = getToken(); | ||
|
||
$apiClient->setAccessToken($accessToken) | ||
->setAccountBaseDomain($accessToken->getValues()['baseDomain']) | ||
->onAccessTokenRefresh( | ||
function (AccessTokenInterface $accessToken, string $baseDomain) { | ||
saveToken( | ||
[ | ||
'accessToken' => $accessToken->getToken(), | ||
'refreshToken' => $accessToken->getRefreshToken(), | ||
'expires' => $accessToken->getExpires(), | ||
'baseDomain' => $baseDomain, | ||
] | ||
); | ||
} | ||
); | ||
|
||
|
||
$chatTemplatesService = $apiClient->chatTemplates(); | ||
|
||
// Создадим редактируемый шаблон | ||
$chatTemplate = new TemplateModel(); | ||
$chatTemplate | ||
->setName('Название шаблона') | ||
->setContent('Название сделки - {{lead.name}}') | ||
->setExternalId('qwedsgfsdg-dsgsdg') //Идентификатор шаблона на стороне интеграции | ||
->setIsEditable(true); | ||
|
||
try { | ||
$chatTemplate = $chatTemplatesService->addOne($chatTemplate); | ||
} catch (AmoCRMApiException $e) { | ||
printError($e); | ||
die; | ||
} | ||
echo 'Добавленный шаблон: '; | ||
var_dump($chatTemplate->toArray()); | ||
echo PHP_EOL; | ||
|
||
|
||
// Обновим шаблон и добавим в него кнопки. Кнопок разного типа быть не может | ||
$buttonsCollection = new ButtonsCollection(); | ||
$buttonsCollection | ||
->add( | ||
(new TextButtonModel())->setText('Текст кнопки') | ||
) | ||
->add( | ||
(new TextButtonModel())->setText('Текст кнопки2') | ||
); | ||
$chatTemplate->setButtons($buttonsCollection); | ||
|
||
|
||
try { | ||
$chatTemplatesService->updateOne($chatTemplate); | ||
} catch (AmoCRMApiException $e) { | ||
printError($e); | ||
die; | ||
} | ||
|
||
|
||
// Получим шаблоны | ||
try { | ||
$chatTemplatesCollection = $chatTemplatesService->get(); | ||
} catch (AmoCRMApiException $e) { | ||
printError($e); | ||
die; | ||
} | ||
var_dump($chatTemplatesCollection->toArray()); | ||
|
||
|
||
// Получим шаблоны по ExternalId | ||
$templatesFilter = new \AmoCRM\Filters\Chats\TemplatesFilter(); | ||
$templatesFilter->setExternalIds(['qwedsgfsdg-dsgsdg']); | ||
try { | ||
$chatTemplate = $chatTemplatesService->get($templatesFilter)->first(); | ||
} catch (AmoCRMApiException $e) { | ||
printError($e); | ||
die; | ||
} | ||
var_dump($chatTemplate->toArray()); | ||
|
||
// Удалим первый шаблон | ||
try { | ||
$chatTemplatesService->deleteOne($chatTemplate); | ||
} catch (AmoCRMApiException $e) { | ||
printError($e); | ||
die; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/AmoCRM/Collections/Chats/Templates/Buttons/ButtonsCollection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AmoCRM\Collections\Chats\Templates\Buttons; | ||
|
||
use AmoCRM\Collections\BaseApiCollection; | ||
use AmoCRM\Exceptions\InvalidArgumentException; | ||
use AmoCRM\Models\Chats\Templates\Buttons\AbstractButtonModel; | ||
|
||
/** | ||
* Class ButtonsCollection | ||
* | ||
* @package AmoCRM\Collections\Chats\Templates\Buttons | ||
* | ||
* @method null|AbstractButtonModel current() | ||
* @method null|AbstractButtonModel last() | ||
* @method null|AbstractButtonModel first() | ||
* @method null|AbstractButtonModel offsetGet($offset) | ||
* @method ButtonsCollection offsetSet($offset, AbstractButtonModel $value) | ||
* @method ButtonsCollection prepend(AbstractButtonModel $value) | ||
* @method ButtonsCollection add(AbstractButtonModel $value) | ||
* @method null|AbstractButtonModel getBy($key, $value) | ||
*/ | ||
class ButtonsCollection extends BaseApiCollection | ||
{ | ||
public const ITEM_CLASS = AbstractButtonModel::class; | ||
|
||
/** | ||
* @param array $array | ||
* | ||
* @return ButtonsCollection | ||
*/ | ||
public static function fromArray(array $array): BaseApiCollection | ||
{ | ||
$items = array_map( | ||
static function (array $item) { | ||
try { | ||
return AbstractButtonModel::fromArray($item); | ||
} catch (InvalidArgumentException $e) { | ||
return null; | ||
} | ||
}, | ||
$array | ||
); | ||
|
||
return self::make($items); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/AmoCRM/Collections/Chats/Templates/TemplatesCollection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AmoCRM\Collections\Chats\Templates; | ||
|
||
use AmoCRM\Collections\BaseApiCollection; | ||
use AmoCRM\Collections\Interfaces\HasPagesInterface; | ||
use AmoCRM\Collections\Traits\PagesTrait; | ||
use AmoCRM\Models\Chats\Templates\TemplateModel; | ||
|
||
/** | ||
* Class TemplatesCollection | ||
* | ||
* @package AmoCRM\Collections\Chats\Templates | ||
* | ||
* @method null|TemplateModel current() | ||
* @method null|TemplateModel last() | ||
* @method null|TemplateModel first() | ||
* @method null|TemplateModel offsetGet($offset) | ||
* @method TemplatesCollection offsetSet($offset, TemplateModel $value) | ||
* @method TemplatesCollection prepend(TemplateModel $value) | ||
* @method TemplatesCollection add(TemplateModel $value) | ||
* @method null|TemplateModel getBy($key, $value) | ||
*/ | ||
class TemplatesCollection extends BaseApiCollection implements HasPagesInterface | ||
{ | ||
use PagesTrait; | ||
|
||
public const ITEM_CLASS = TemplateModel::class; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AmoCRM\Collections; | ||
|
||
use AmoCRM\Collections\Traits\EntityApiTrait; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.