Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Sender.php
1<?php
2
4
14
16{
17 public function __construct(
18 Providers\OptionManager $optionManager,
19 Providers\SupportChecker $supportChecker,
20 Providers\Edna\EdnaRu $utils,
21 EmojiConverter $emoji
22 )
23 {
24 parent::__construct($optionManager, $supportChecker, $utils, $emoji);
25
26 $this->emoji = $emoji;
27 }
28
29 public function sendMessage(array $messageFields): SendMessage
30 {
31 if (!$this->supportChecker->canUse())
32 {
33 $result = new SendMessage();
34 $result->addError(new Error('Service is unavailable'));
35
36 return $result;
37 }
38
39 $requestParams = $this->getSendMessageParams($messageFields)->getData();
40 $method = $this->getSendMessageMethod($messageFields);
41
42 if ($method === 'imOutHSM')
43 {
44 $this->sendHSMtoChat($messageFields);
45 }
46
47 $result = new SendMessage();
48
51 $this->optionManager->getOption(InternalOption::API_KEY),
52 Constants::API_ENDPOINT
53 )
54 ;
55
56 $requestResult = $externalSender->callExternalMethod($method, $requestParams);
57 if (!$requestResult->isSuccess())
58 {
59 $result->addErrors($requestResult->getErrors());
60
61 return $result;
62 }
63
64 $apiData = $requestResult->getData();
65 $result->setExternalId($apiData['id']);
66 $result->setAccepted();
67
68 return $result;
69 }
70
71 public function getMessageStatus(array $messageFields): MessageStatus
72 {
73 $result = new MessageStatus();
74 $result->setId($messageFields['ID']);
75 $result->setExternalId($messageFields['ID']);
76
77 if (!$this->supportChecker->canUse())
78 {
79 $result->addError(new Error(Loc::getMessage('MESSAGESERVICE_SENDER_SMS_EDNARU_USE_ERROR')));
80 return $result;
81 }
82
85 $this->optionManager->getOption(InternalOption::API_KEY),
86 Constants::API_ENDPOINT
87 )
88 ;
89
90 $apiResult = $externalSender->callExternalMethod("imOutMessage/{$messageFields['ID']}");
91 if (!$apiResult->isSuccess())
92 {
93 $result->addErrors($apiResult->getErrors());
94 }
95 else
96 {
97 $apiData = $apiResult->getData();
98
99 $result->setStatusText($apiData['dlvStatus']);
100 $result->setStatusCode((new StatusResolver())->resolveStatus($apiData['dlvStatus']));
101 }
102
103 return $result;
104 }
112 public function prepareMessageBodyForSave(string $text): string
113 {
114 return $this->emoji->convertEmoji($text, Providers\Constants\InternalOption::EMOJI_ENCODE);
115 }
116
123 protected function getSendMessageParams(array $messageFields): Result
124 {
125 $messageFields['MESSAGE_BODY'] = $this->emoji->convertEmoji($messageFields['MESSAGE_BODY'], Providers\Constants\InternalOption::EMOJI_DECODE);
126 $params = [
127 'id' => uniqid('', true),
128 'subject' => $messageFields['MESSAGE_FROM'],
129 'address' => str_replace('+', '', $messageFields['MESSAGE_TO']),
130 'contentType' => 'text',
131 'text' => $messageFields['MESSAGE_BODY'],
132 ];
133
134 if ($this->isTemplateMessage($messageFields))
135 {
136 $params['imType'] = 'whatsapp';
137 $params['text'] = $messageFields['MESSAGE_HEADERS']['template']['text'];
138
139 $templateFields = ['header', 'footer', 'keyboard'];
140
141 foreach ($templateFields as $templateField)
142 {
143 if (
144 isset($messageFields['MESSAGE_HEADERS']['template'][$templateField])
145 && count($messageFields['MESSAGE_HEADERS']['template'][$templateField]) > 0
146 )
147 {
148 $params[$templateField] = $messageFields['MESSAGE_HEADERS']['template'][$templateField];
149 }
150 }
151
152 $params = $this->emoji->convertEmojiInTemplate($params, InternalOption::EMOJI_DECODE);
153 }
154
155 return (new Result)->setData($params);
156 }
157
165 protected function getSendMessageMethod(array $messageFields): string
166 {
167 $method = 'imOutMessage';
168 if ($this->isTemplateMessage($messageFields))
169 {
170 $method = 'imOutHSM';
171 }
172
173 return $method;
174 }
175
176
177}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
setData(array $data)
Definition result.php:113
Providers ExternalSender $externalSender
Definition Sender.php:18
__construct(Providers\OptionManager $optionManager, Providers\SupportChecker $supportChecker, Providers\Edna\EdnaRu $utils, EmojiConverter $emoji)
Definition Sender.php:17