src/Service/SmsService.php line 35

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. class SmsService
  18. {
  19. private $app_key;
  20. private $app_token;
  21. private MessageSender $sender;
  22. private ConfigurationRepository $configurationRepository;
  23. private EntityManagerInterface $entityManager;
  24. public function __construct(ConfigurationRepository $configurationRepository, EntityManagerInterface $entityManager)
  25. {
  26. // $this->bearerKey = $configurationRepository->findOneBy(['name' => 'TELIASMS_KEY'])->getValue();
  27. // $this->bearerKey = $configurationRepository->findOneBy(['name' => 'TELIASMS_KEY'])->getValue();
  28. $this->app_key = $configurationRepository->findOneBy(['name' => 'BULKGATE_APPLICATION_ID'])->getValue();
  29. $this->app_token = $configurationRepository->findOneBy(['name' => 'BULKGATE_APPLICATION_TOKEN'])->getValue();
  30. $connection = new ConnectionStream($this->app_key, $this->app_token);
  31. $this->sender = new MessageSender($connection);
  32. $this->configurationRepository = $configurationRepository;
  33. $this->entityManager = $entityManager;
  34. }
  35. /**
  36. * @param string $phone Number to send a message to.
  37. * @param string $content Content for an SMS Message
  38. * @return bool|string Either false on failure, or json response on success
  39. */
  40. public function send($phone, $content, $forceSMS = false)
  41. {
  42. if(!$phone || !is_string($phone))
  43. {
  44. return false;
  45. }
  46. $ltSymbols = [
  47. 'ą' => 'a',
  48. 'č' => 'c',
  49. 'ę' => 'e',
  50. 'ė' => 'e',
  51. 'į' => 'i',
  52. 'š' => 's',
  53. 'ų' => 'u',
  54. 'ū' => 'u',
  55. 'ž' => 'z',
  56. 'Ą' => 'A',
  57. 'Č' => 'C',
  58. 'Ę' => 'E',
  59. 'Ė' => 'E',
  60. 'Į' => 'I',
  61. 'Š' => 'S',
  62. 'Ų' => 'U',
  63. 'Ū' => 'U',
  64. 'Ž' => 'Z',
  65. ];
  66. $content = str_replace(array_keys($ltSymbols), array_values($ltSymbols), $content);
  67. $content = str_replace(" - ", "-", $content);
  68. $pattern = '/^86/i';
  69. $phone = preg_replace($pattern, '3706', $phone);
  70. if(!$forceSMS) {
  71. return $this->sendMultiChannel($phone, $content);
  72. }
  73. //old
  74. $curl = curl_init();
  75. $body = array(
  76. "application_id"=> $this->app_key,
  77. "application_token"=> $this->app_token,
  78. "number"=> $phone,
  79. "text"=> $content,
  80. "unicode"=>true,
  81. "sender_id"=> "gText",
  82. "sender_id_value"=> "COREPETITUS",
  83. "country"=>"lt"
  84. );
  85. curl_setopt_array($curl, array(
  86. CURLOPT_URL => 'https://portal.bulkgate.com/api/1.0/simple/transactional',
  87. CURLOPT_RETURNTRANSFER => true,
  88. CURLOPT_ENCODING => '',
  89. CURLOPT_MAXREDIRS => 10,
  90. CURLOPT_TIMEOUT => 0,
  91. CURLOPT_FOLLOWLOCATION => true,
  92. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  93. CURLOPT_CUSTOMREQUEST => 'POST',
  94. CURLOPT_POSTFIELDS => json_encode($body),
  95. CURLOPT_HTTPHEADER => array(
  96. 'Content-Type: application/json',
  97. 'Cookie: _nss=1'
  98. ),
  99. ));
  100. $response = curl_exec($curl);
  101. curl_close($curl);
  102. return $response;
  103. }
  104. /**
  105. * @param array[
  106. * {recipient: string, text: string},
  107. *
  108. * {recipient: string, text: string},
  109. *
  110. * {recipient: string, text: string}] $messages
  111. * @return bool|string Either false on failure, or json response on success
  112. */
  113. public function sendAll(array $messages)
  114. {
  115. $ltSymbols = [
  116. 'ą' => 'a',
  117. 'č' => 'c',
  118. 'ę' => 'e',
  119. 'ė' => 'e',
  120. 'į' => 'i',
  121. 'š' => 's',
  122. 'ų' => 'u',
  123. 'ū' => 'u',
  124. 'ž' => 'z',
  125. 'Ą' => 'A',
  126. 'Č' => 'C',
  127. 'Ę' => 'E',
  128. 'Ė' => 'E',
  129. 'Į' => 'I',
  130. 'Š' => 'S',
  131. 'Ų' => 'U',
  132. 'Ū' => 'U',
  133. 'Ž' => 'Z',
  134. ];
  135. foreach ($messages as &$message) {
  136. $message['text'] = str_replace(array_keys($ltSymbols), array_values($ltSymbols), $message['text']);
  137. $message['text'] = str_replace(" - ", "-", $message['text']);
  138. }
  139. $connection = new ConnectionStream($this->app_key, $this->app_token);
  140. $sender = new MessageSender($connection);
  141. $sms_configurator = new SmsConfigurator();
  142. $sms_configurator->portalProfile(13361);
  143. $bulkMessages = new Bulk();
  144. foreach($messages as $sms){
  145. $pattern = '/^86/i';
  146. $phone = preg_replace($pattern, '3706', $sms['recipient']);
  147. // $sms = new Sms($phone, $sms['text']);
  148. // $sms_configurator->configure($sms);
  149. // $bulkMessages[]=$sms;
  150. $this->sendMultiChannel($phone,$sms['text']);
  151. }
  152. return true;
  153. // return $sender->send($bulkMessages);
  154. }
  155. public function sendMultiChannel($phone, $content)
  156. {
  157. // $phone_number = new PhoneNumber($phone,"lt");
  158. $smsContent = $content;
  159. $ltSymbols = [
  160. 'ą' => 'a',
  161. 'č' => 'c',
  162. 'ę' => 'e',
  163. 'ė' => 'e',
  164. 'į' => 'i',
  165. 'š' => 's',
  166. 'ų' => 'u',
  167. 'ū' => 'u',
  168. 'ž' => 'z',
  169. 'Ą' => 'A',
  170. 'Č' => 'C',
  171. 'Ę' => 'E',
  172. 'Ė' => 'E',
  173. 'Į' => 'I',
  174. 'Š' => 'S',
  175. 'Ų' => 'U',
  176. 'Ū' => 'U',
  177. 'Ž' => 'Z',
  178. ];
  179. $smsContent = str_replace(array_keys($ltSymbols), array_values($ltSymbols), $smsContent);
  180. $smsContent = str_replace(" - ", "-", $smsContent);
  181. $smstext = new SimpleText($smsContent);
  182. $vibertext = new SimpleText($content);
  183. $timeout = 5;
  184. $message = new MultiChannel($phone);
  185. $message->sms($smstext, 'gText', 'COREPETITUS', false);
  186. $message->viber($vibertext, "Corepetitus korepetitoriai", null, null, $timeout);
  187. $message->setPrimaryChannel(Channel::VIBER);
  188. $smsLog = new SMSLog();
  189. $smsLog->setMessage($vibertext);
  190. $smsLog->setPhoneNumber($phone);
  191. $smsLog->setCreatedAt(new \DateTime());
  192. $this->entityManager->persist($smsLog);
  193. $this->entityManager->flush();
  194. $res = $this->sender->send($message);
  195. return $res;
  196. }
  197. public function sendMultiBulk()
  198. {
  199. $numbers = ['+37065520239','+37063794090','+37066229936'];
  200. $bulkMessages = new Bulk();
  201. foreach($numbers as $number){
  202. $pattern = '/^86/i';
  203. $phone = preg_replace($pattern, '3706', $number);
  204. $text = new SimpleText('Labas as krabas v2');
  205. $timeout = 5;
  206. $message = new MultiChannel($phone);
  207. $message->sms($text, 'gText', 'COREPETITUS', false);
  208. $button = null;
  209. $message->viber($text, "Corepetitus korepetitoriai", $button, null, $timeout);
  210. $message->setPrimaryChannel(Channel::VIBER);
  211. $bulkMessages[]=$message;
  212. }
  213. // $phone_number = new PhoneNumber($phone,"lt");
  214. // $text = new SimpleText($content);
  215. // $timeout = 5;
  216. // $message = new MultiChannel($phone);
  217. // $message->sms($text, 'gText', 'COREPETITUS', false);
  218. // $message->viber($text, "Corepetitus korepetitoriai", null, null, $timeout);
  219. // $message->setPrimaryChannel(Channel::VIBER);
  220. $res = $this->sender->send($bulkMessages);
  221. return $res;
  222. }
  223. }