src/Service/SmsService.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\SMSLog;
  4. use App\Repository\ConfigurationRepository;
  5. use BulkGate\Sdk\Configurator\SmsConfigurator;
  6. use BulkGate\Sdk\Connection\ConnectionStream;
  7. use BulkGate\Sdk\Message\Bulk;
  8. use BulkGate\Sdk\Message\Channel;
  9. use BulkGate\Sdk\Message\Component\Button;
  10. use BulkGate\Sdk\Message\Component\SimpleText;
  11. use BulkGate\Sdk\Message\MultiChannel;
  12. use BulkGate\Sdk\Message\Sms;
  13. use BulkGate\Sdk\MessageSender;
  14. use BulkGate\Sms\Message\PhoneNumber;
  15. use BulkGate\Sms\Sender;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Psr\Log\LoggerInterface;
  18. class SmsService
  19. {
  20. private const HTTP_TIMEOUT_SECONDS = 15;
  21. private const HTTP_CONNECT_TIMEOUT_SECONDS = 5;
  22. private const HTTP_ERROR_STATUS_FROM = 400;
  23. private const VISIBLE_PHONE_DIGITS = 3;
  24. private $app_key;
  25. private $app_token;
  26. private MessageSender $sender;
  27. private ConfigurationRepository $configurationRepository;
  28. private EntityManagerInterface $entityManager;
  29. private LoggerInterface $smsLogger;
  30. public function __construct(ConfigurationRepository $configurationRepository, EntityManagerInterface $entityManager, LoggerInterface $smsLogger)
  31. {
  32. $this->smsLogger = $smsLogger;
  33. // $this->bearerKey = $configurationRepository->findOneBy(['name' => 'TELIASMS_KEY'])->getValue();
  34. // $this->bearerKey = $configurationRepository->findOneBy(['name' => 'TELIASMS_KEY'])->getValue();
  35. $this->app_key = $configurationRepository->findOneBy(['name' => 'BULKGATE_APPLICATION_ID'])->getValue();
  36. $this->app_token = $configurationRepository->findOneBy(['name' => 'BULKGATE_APPLICATION_TOKEN'])->getValue();
  37. $connection = new ConnectionStream($this->app_key, $this->app_token);
  38. $this->sender = new MessageSender($connection);
  39. $this->configurationRepository = $configurationRepository;
  40. $this->entityManager = $entityManager;
  41. }
  42. /**
  43. * @param string $phone Number to send a message to.
  44. * @param string $content Content for an SMS Message
  45. * @return bool|string Either false on failure, or json response on success
  46. */
  47. public function send($phone, $content, $forceSMS = false)
  48. {
  49. if(!$phone || !is_string($phone))
  50. {
  51. return false;
  52. }
  53. $ltSymbols = [
  54. 'ą' => 'a',
  55. 'č' => 'c',
  56. 'ę' => 'e',
  57. 'ė' => 'e',
  58. 'į' => 'i',
  59. 'š' => 's',
  60. 'ų' => 'u',
  61. 'ū' => 'u',
  62. 'ž' => 'z',
  63. 'Ą' => 'A',
  64. 'Č' => 'C',
  65. 'Ę' => 'E',
  66. 'Ė' => 'E',
  67. 'Į' => 'I',
  68. 'Š' => 'S',
  69. 'Ų' => 'U',
  70. 'Ū' => 'U',
  71. 'Ž' => 'Z',
  72. ];
  73. $content = str_replace(array_keys($ltSymbols), array_values($ltSymbols), $content);
  74. $content = str_replace(" - ", "-", $content);
  75. $pattern = '/^86/i';
  76. $phone = preg_replace($pattern, '3706', $phone);
  77. if(!$forceSMS) {
  78. return $this->sendMultiChannel($phone, $content);
  79. }
  80. //old
  81. $curl = curl_init();
  82. $body = array(
  83. "application_id"=> $this->app_key,
  84. "application_token"=> $this->app_token,
  85. "number"=> $phone,
  86. "text"=> $content,
  87. "unicode"=>true,
  88. "sender_id"=> "gText",
  89. "sender_id_value"=> "COREPETITUS",
  90. "country"=>"lt"
  91. );
  92. curl_setopt_array($curl, array(
  93. CURLOPT_URL => 'https://portal.bulkgate.com/api/1.0/simple/transactional',
  94. CURLOPT_RETURNTRANSFER => true,
  95. CURLOPT_ENCODING => '',
  96. CURLOPT_MAXREDIRS => 10,
  97. CURLOPT_TIMEOUT => self::HTTP_TIMEOUT_SECONDS,
  98. CURLOPT_CONNECTTIMEOUT => self::HTTP_CONNECT_TIMEOUT_SECONDS,
  99. CURLOPT_FOLLOWLOCATION => true,
  100. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  101. CURLOPT_CUSTOMREQUEST => 'POST',
  102. CURLOPT_POSTFIELDS => json_encode($body),
  103. CURLOPT_HTTPHEADER => array(
  104. 'Content-Type: application/json',
  105. 'Cookie: _nss=1'
  106. ),
  107. ));
  108. $response = curl_exec($curl);
  109. $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  110. $curlError = curl_error($curl);
  111. curl_close($curl);
  112. if ($response === false || $statusCode >= self::HTTP_ERROR_STATUS_FROM) {
  113. $this->smsLogger->error('BulkGate SMS request failed', [
  114. 'phone' => $this->maskPhone($phone),
  115. 'statusCode' => $statusCode,
  116. 'curlError' => $curlError,
  117. 'response' => $response,
  118. ]);
  119. }
  120. return $response;
  121. }
  122. /**
  123. * @param array[
  124. * {recipient: string, text: string},
  125. *
  126. * {recipient: string, text: string},
  127. *
  128. * {recipient: string, text: string}] $messages
  129. * @return bool|string Either false on failure, or json response on success
  130. */
  131. public function sendAll(array $messages)
  132. {
  133. $ltSymbols = [
  134. 'ą' => 'a',
  135. 'č' => 'c',
  136. 'ę' => 'e',
  137. 'ė' => 'e',
  138. 'į' => 'i',
  139. 'š' => 's',
  140. 'ų' => 'u',
  141. 'ū' => 'u',
  142. 'ž' => 'z',
  143. 'Ą' => 'A',
  144. 'Č' => 'C',
  145. 'Ę' => 'E',
  146. 'Ė' => 'E',
  147. 'Į' => 'I',
  148. 'Š' => 'S',
  149. 'Ų' => 'U',
  150. 'Ū' => 'U',
  151. 'Ž' => 'Z',
  152. ];
  153. foreach ($messages as &$message) {
  154. $message['text'] = str_replace(array_keys($ltSymbols), array_values($ltSymbols), $message['text']);
  155. $message['text'] = str_replace(" - ", "-", $message['text']);
  156. }
  157. $connection = new ConnectionStream($this->app_key, $this->app_token);
  158. $sender = new MessageSender($connection);
  159. $sms_configurator = new SmsConfigurator();
  160. $sms_configurator->portalProfile(13361);
  161. $bulkMessages = new Bulk();
  162. foreach($messages as $sms){
  163. $pattern = '/^86/i';
  164. $phone = preg_replace($pattern, '3706', $sms['recipient']);
  165. // $sms = new Sms($phone, $sms['text']);
  166. // $sms_configurator->configure($sms);
  167. // $bulkMessages[]=$sms;
  168. try {
  169. $this->sendMultiChannel($phone,$sms['text']);
  170. } catch (\Throwable $exception) {
  171. // Already logged in sendMultiChannel(); keep sending to the remaining recipients.
  172. continue;
  173. }
  174. }
  175. return true;
  176. // return $sender->send($bulkMessages);
  177. }
  178. public function sendMultiChannel($phone, $content)
  179. {
  180. // $phone_number = new PhoneNumber($phone,"lt");
  181. $smsContent = $content;
  182. $ltSymbols = [
  183. 'ą' => 'a',
  184. 'č' => 'c',
  185. 'ę' => 'e',
  186. 'ė' => 'e',
  187. 'į' => 'i',
  188. 'š' => 's',
  189. 'ų' => 'u',
  190. 'ū' => 'u',
  191. 'ž' => 'z',
  192. 'Ą' => 'A',
  193. 'Č' => 'C',
  194. 'Ę' => 'E',
  195. 'Ė' => 'E',
  196. 'Į' => 'I',
  197. 'Š' => 'S',
  198. 'Ų' => 'U',
  199. 'Ū' => 'U',
  200. 'Ž' => 'Z',
  201. ];
  202. $smsContent = str_replace(array_keys($ltSymbols), array_values($ltSymbols), $smsContent);
  203. $smsContent = str_replace(" - ", "-", $smsContent);
  204. $smstext = new SimpleText($smsContent);
  205. $vibertext = new SimpleText($content);
  206. $timeout = 5;
  207. $message = new MultiChannel($phone);
  208. $message->sms($smstext, 'gText', 'COREPETITUS', false);
  209. $message->viber($vibertext, "Corepetitus korepetitoriai", null, null, $timeout);
  210. $message->setPrimaryChannel(Channel::VIBER);
  211. $smsLog = new SMSLog();
  212. $smsLog->setMessage($vibertext);
  213. $smsLog->setPhoneNumber($phone);
  214. $smsLog->setCreatedAt(new \DateTime());
  215. $this->entityManager->persist($smsLog);
  216. $this->entityManager->flush();
  217. try {
  218. $res = $this->sender->send($message);
  219. } catch (\Throwable $exception) {
  220. $this->smsLogger->error('BulkGate Viber/SMS send failed', [
  221. 'phone' => $this->maskPhone($phone),
  222. 'exception' => $exception,
  223. ]);
  224. throw $exception;
  225. }
  226. return $res;
  227. }
  228. public function sendMultiBulk()
  229. {
  230. $numbers = ['+37065520239','+37063794090','+37066229936'];
  231. $bulkMessages = new Bulk();
  232. foreach($numbers as $number){
  233. $pattern = '/^86/i';
  234. $phone = preg_replace($pattern, '3706', $number);
  235. $text = new SimpleText('Labas as krabas v2');
  236. $timeout = 5;
  237. $message = new MultiChannel($phone);
  238. $message->sms($text, 'gText', 'COREPETITUS', false);
  239. $button = null;
  240. $message->viber($text, "Corepetitus korepetitoriai", $button, null, $timeout);
  241. $message->setPrimaryChannel(Channel::VIBER);
  242. $bulkMessages[]=$message;
  243. }
  244. // $phone_number = new PhoneNumber($phone,"lt");
  245. // $text = new SimpleText($content);
  246. // $timeout = 5;
  247. // $message = new MultiChannel($phone);
  248. // $message->sms($text, 'gText', 'COREPETITUS', false);
  249. // $message->viber($text, "Corepetitus korepetitoriai", null, null, $timeout);
  250. // $message->setPrimaryChannel(Channel::VIBER);
  251. $res = $this->sender->send($bulkMessages);
  252. return $res;
  253. }
  254. private function maskPhone($phone): string
  255. {
  256. $phone = (string) $phone;
  257. return str_repeat('*', max(0, strlen($phone) - self::VISIBLE_PHONE_DIGITS))
  258. . substr($phone, -self::VISIBLE_PHONE_DIGITS);
  259. }
  260. }