Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
Sender.php
1
<?php
2
3
namespace
Bitrix\MessageService\Providers\Edna\WhatsApp\Old
;
4
5
use
Bitrix\Main\Error
;
6
use
Bitrix\Main\Result
;
7
use
Bitrix\Main\Localization\Loc
;
8
use
Bitrix\MessageService\Providers
;
9
use
Bitrix\MessageService\Providers\Constants\InternalOption
;
10
use
Bitrix\MessageService\Providers\Edna\WhatsApp\ExternalSender
;
11
use
Bitrix\MessageService\Providers\Edna\WhatsApp\StatusResolver
;
12
use
Bitrix\MessageService\Sender\Result\MessageStatus
;
13
use
Bitrix\MessageService\Sender\Result\SendMessage
;
14
15
class
Sender
extends
Providers\Edna\WhatsApp\Sender
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
49
$externalSender
=
50
new
ExternalSender
(
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
83
$externalSender
=
84
new
ExternalSender
(
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
}
Bitrix\Bizproc\Error
Definition
error.php:8
Bitrix\Bizproc\Result
Definition
result.php:6
Bitrix\Main\Error
Definition
error.php:14
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\ORM\Data\Result
Definition
result.php:16
Bitrix\Main\Result\setData
setData(array $data)
Definition
result.php:113
Bitrix\MessageService\MessageStatus
Definition
messagestatus.php:9
Bitrix\MessageService\Providers\Constants\InternalOption
Definition
InternalOption.php:6
Bitrix\MessageService\Providers\Edna\ExternalSender
Definition
ExternalSender.php:9
Bitrix\MessageService\Providers\Edna\Sender
Definition
Sender.php:13
Bitrix\MessageService\Providers\Edna\Sender\sendHSMtoChat
sendHSMtoChat(array $messageFields)
Bitrix\MessageService\Providers\Edna\Sender\isTemplateMessage
isTemplateMessage(array $messageFields)
Bitrix\MessageService\Providers\Edna\Sender\$externalSender
Providers ExternalSender $externalSender
Definition
Sender.php:18
Bitrix\MessageService\Providers\Edna\WhatsApp\Constants
Definition
Constants.php:6
Bitrix\MessageService\Providers\Edna\WhatsApp\EmojiConverter
Definition
EmojiConverter.php:8
Bitrix\MessageService\Providers\Edna\WhatsApp\Old\Sender\getSendMessageParams
getSendMessageParams(array $messageFields)
Definition
Sender.php:123
Bitrix\MessageService\Providers\Edna\WhatsApp\Old\Sender\__construct
__construct(Providers\OptionManager $optionManager, Providers\SupportChecker $supportChecker, Providers\Edna\EdnaRu $utils, EmojiConverter $emoji)
Definition
Sender.php:17
Bitrix\MessageService\Providers\Edna\WhatsApp\Old\Sender\prepareMessageBodyForSave
prepareMessageBodyForSave(string $text)
Definition
Sender.php:112
Bitrix\MessageService\Providers\Edna\WhatsApp\Old\Sender\getSendMessageMethod
getSendMessageMethod(array $messageFields)
Definition
Sender.php:165
Bitrix\MessageService\Providers\Edna\WhatsApp\Old\Sender\sendMessage
sendMessage(array $messageFields)
Definition
Sender.php:29
Bitrix\MessageService\Providers\Edna\WhatsApp\Old\Sender\getMessageStatus
getMessageStatus(array $messageFields)
Definition
Sender.php:71
Bitrix\MessageService\Providers\Edna\WhatsApp\Sender
Definition
Sender.php:15
Bitrix\MessageService\Providers\Edna\WhatsApp\StatusResolver
Definition
StatusResolver.php:8
Bitrix\MessageService\Sender\Result\SendMessage
Definition
sendmessage.php:9
Bitrix\MessageService\Providers\Edna\EdnaRu
Definition
EdnaRu.php:8
Bitrix\MessageService\Providers\OptionManager
Definition
OptionManager.php:5
Bitrix\MessageService\Providers\SupportChecker
Definition
SupportChecker.php:6
Bitrix\MessageService\Providers\Edna\WhatsApp\Old
Definition
Constants.php:3
Bitrix\MessageService\Providers
modules
messageservice
lib
Providers
Edna
WhatsApp
Old
Sender.php
Создано системой
1.10.0