Skip to content

Commit

Permalink
calls fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bessudnov committed May 11, 2023
1 parent 52f8c22 commit 6e31f3f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 131 deletions.
3 changes: 2 additions & 1 deletion examples/calls.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ function (AccessTokenInterface $accessToken, string $baseDomain) {
//Добавим входящий звонок
$call = new CallModel();
$call
->setPhone('+7912312321')
->setPhone('+7912312321') // кто звонил
->setCallStatus(CallInterface::CALL_STATUS_SUCCESS_CONVERSATION)
->setCallResult('Разговор состоялся')
->setDuration(148)
->setUniq(Uuid::uuid4())
->setSource('integration name')
->setDirection(CallInterface::CALL_DIRECTION_IN)
->setCallResponsible('+79161234567') // кому звонили, можно id пользователя, или строку
->setLink('https://example.test/test.mp3');

try {
Expand Down
13 changes: 12 additions & 1 deletion src/AmoCRM/Models/CallModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ public function fromArray(array $call): CallModel
$this->setDirection($call['direction']);
}


if (isset($call['call_responsible'])) {
$this->setCallResponsible($call['call_responsible']);
}

return $model;
}
Expand All @@ -132,6 +134,7 @@ public function toArray(): array
'phone' => $this->getPhone(),
'call_result' => $this->getCallResult(),
'call_status' => $this->getCallStatus(),
'call_responsible' => $this->getCallResponsible(),
'direction' => $this->getDirection(),
'entity_id' => $this->getEntityId(),
'entity_type' => $this->getEntityType(),
Expand Down Expand Up @@ -166,10 +169,18 @@ public function toApi(?string $requestId = "0"): array
'request_id' => $this->getRequestId(),
];

// Кто может работать с событием, например, удалять
if ($responsibleUserId = $this->getResponsibleUserId()) {
$call['responsible_user_id'] = $responsibleUserId;
}

// Кто отображается
// Для входящего - кто получил звонок (для отображения в интерфейсе)
// Для исходящего - кто совершил звонок (для отображения в интерфейсе)
if ($callResponsible = $this->getCallResponsible()) {
$call['call_responsible'] = $callResponsible;
}

if ($createdBy = $this->getCreatedBy()) {
$call['created_by'] = $createdBy;
}
Expand Down
36 changes: 7 additions & 29 deletions src/AmoCRM/Models/NoteType/CallNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,14 @@ abstract class CallNote extends NoteModel
{
use CallTrait;

/** @deprecated */
public const CALL_STATUS_LEAVE_MESSAGE = 1;
/** @deprecated */
public const CALL_STATUS_SUCCESS_RECALL = 2;
/** @deprecated */
public const CALL_STATUS_SUCCESS_NOT_IN_STOCK = 3;
/** @deprecated */
public const CALL_STATUS_SUCCESS_CONVERSATION = 4;
/** @deprecated */
public const CALL_STATUS_FAIL_WRONG_NUMBER = 5;
/** @deprecated */
public const CALL_STATUS_FAIL_NOT_PHONED = 6;
/** @deprecated */
public const CALL_STATUS_FAIL_BUSY = 7;
/** @deprecated */
public const CALL_STATUS_UNDEFINED = 8;

/** @deprecated */
protected const AVAILABLE_CALL_STATUSES = [
self::CALL_STATUS_LEAVE_MESSAGE,
self::CALL_STATUS_SUCCESS_RECALL,
self::CALL_STATUS_SUCCESS_NOT_IN_STOCK,
self::CALL_STATUS_SUCCESS_CONVERSATION,
self::CALL_STATUS_FAIL_WRONG_NUMBER,
self::CALL_STATUS_FAIL_NOT_PHONED,
self::CALL_STATUS_FAIL_BUSY,
self::CALL_STATUS_UNDEFINED,
];

/**
* @param array $note
*
* @return self
*/
public function fromArray(array $note): NoteModel
{
/** @var CallNote $model */
$model = parent::fromArray($note);

if (isset($note['params']['uniq']) && is_string($note['params']['uniq'])) {
Expand Down Expand Up @@ -77,6 +49,10 @@ public function fromArray(array $note): NoteModel
$model->setCallStatus($note['params']['call_status']);
}

if (isset($note['params']['call_responsible'])) {
$model->setCallResponsible($note['params']['call_responsible']);
}

return $model;
}

Expand All @@ -95,6 +71,7 @@ public function toArray(): array
'phone' => $this->getPhone(),
'call_result' => $this->getCallResult(),
'call_status' => $this->getCallStatus(),
'call_responsible' => $this->getCallResponsible(),
];

return $result;
Expand All @@ -116,6 +93,7 @@ public function toApi(?string $requestId = "0"): array
'phone' => $this->getPhone(),
'call_result' => $this->getCallResult(),
'call_status' => $this->getCallStatus(),
'call_responsible' => $this->getCallResponsible(),
];

return $result;
Expand Down
30 changes: 30 additions & 0 deletions src/AmoCRM/Models/Traits/CallTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ trait CallTrait
*/
protected $callStatus;

/**
* Для входящего - кто получил звонок (для отображения в интерфейсе)
* Для исходящего - кто совершил звонок (для отображения в интерфейсе)
*
* Можно передать id пользователя или имя (будет зафиксировано)
*
* @var string|int|null
*/
protected $callResponsible;

/**
* @return string|null
*/
Expand Down Expand Up @@ -177,4 +187,24 @@ public function setCallStatus(?int $callStatus): self

return $this;
}

/**
* @return int|string|null
*/
public function getCallResponsible()
{
return $this->callResponsible;
}

/**
* @param int|string|null $callResponsible
*
* @return self
*/
public function setCallResponsible($callResponsible): self
{
$this->callResponsible = $callResponsible;

return $this;
}
}
116 changes: 16 additions & 100 deletions src/AmoCRM/Models/Unsorted/SipMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,31 @@

namespace AmoCRM\Models\Unsorted;

use AmoCRM\Models\Traits\CallTrait;
use AmoCRM\Models\Unsorted\Interfaces\UnsortedMetadataInterface;
use AmoCRM\Models\BaseApiModel;
use AmoCRM\Contracts\Support\Arrayable;

use function is_scalar;

class SipMetadata extends BaseApiModel implements Arrayable, UnsortedMetadataInterface
{
use CallTrait;

/**
* @deprecated
* @var int|string|null
*/
protected $from;

/**
* @var string|null
*/
protected $phone;

/**
* @var int|null
*/
protected $calledAt;

/**
* @var int|null
*/
protected $duration;

/**
* @var string|null
*/
protected $link;

/**
* @var string|null
*/
protected $serviceCode;

/**
* @var string|null
*/
protected $uniq;

/**
* @var bool|null
*/
Expand All @@ -66,6 +48,7 @@ public static function fromArray(array $metadata): self
$model->setLink($metadata['link'] ?? null);
$model->setServiceCode($metadata['service_code'] ?? null);
$model->setUniq($metadata['uniq'] ?? null);
$model->setCallResponsible($metadata['call_responsible'] ?? null);

return $model;
}
Expand All @@ -83,10 +66,12 @@ public function toArray(): array
'link' => $this->getLink(),
'service_code' => $this->getServiceCode(),
'uniq' => $this->getUniq(),
'call_responsible' => $this->getCallResponsible(),
];
}

/**
* @deprecated
* @return int|string|null
*/
public function getFrom()
Expand All @@ -95,6 +80,7 @@ public function getFrom()
}

/**
* @deprecated
* @param int|null|string $from
* @return SipMetadata
*/
Expand All @@ -105,25 +91,6 @@ public function setFrom($from): SipMetadata
return $this;
}

/**
* @return string|null
*/
public function getPhone(): ?string
{
return $this->phone;
}

/**
* @param string|null $phone
* @return SipMetadata
*/
public function setPhone(?string $phone): SipMetadata
{
$this->phone = $phone;

return $this;
}

/**
* @return int|null
*/
Expand All @@ -143,44 +110,6 @@ public function setCalledAt(?int $calledAt): SipMetadata
return $this;
}

/**
* @return int|null
*/
public function getDuration(): ?int
{
return $this->duration;
}

/**
* @param int|null $duration
* @return SipMetadata
*/
public function setDuration(?int $duration): SipMetadata
{
$this->duration = $duration;

return $this;
}

/**
* @return string|null
*/
public function getLink(): ?string
{
return $this->link;
}

/**
* @param string|null $link
* @return SipMetadata
*/
public function setLink(?string $link): SipMetadata
{
$this->link = $link;

return $this;
}

/**
* @return string|null
*/
Expand All @@ -200,25 +129,6 @@ public function setServiceCode(?string $serviceCode): SipMetadata
return $this;
}

/**
* @return string|null
*/
public function getUniq(): ?string
{
return $this->uniq;
}

/**
* @param string|null $uniq
* @return SipMetadata
*/
public function setUniq(?string $uniq): SipMetadata
{
$this->uniq = $uniq;

return $this;
}

/**
* @return bool|null
*/
Expand All @@ -245,16 +155,22 @@ public function setIsCallEventNeeded(?bool $isCallEventNeeded): SipMetadata
*/
public function toApi(?string $requestId = "0"): array
{
return [
'from' => $this->getFrom(),
$result = [
'phone' => $this->getPhone(),
'called_at' => $this->getCalledAt() ?? time(),
'duration' => $this->getDuration(),
'link' => $this->getLink(),
'service_code' => $this->getServiceCode(),
'uniq' => $this->getUniq(),
'is_call_event_needed' => $this->getIsCallEventNeeded() ?? true,
'call_responsible' => $this->getCallResponsible(),
];

if (!empty($this->getFrom())) {
$result['from'] = $this->getFrom();
}

return $result;
}

/**
Expand Down

0 comments on commit 6e31f3f

Please sign in to comment.