<?php
namespace App\Service;
use App\Entity\SMSLog;
use App\Repository\ConfigurationRepository;
use BulkGate\Sdk\Configurator\SmsConfigurator;
use BulkGate\Sdk\Connection\ConnectionStream;
use BulkGate\Sdk\Message\Bulk;
use BulkGate\Sdk\Message\Channel;
use BulkGate\Sdk\Message\Component\Button;
use BulkGate\Sdk\Message\Component\SimpleText;
use BulkGate\Sdk\Message\MultiChannel;
use BulkGate\Sdk\Message\Sms;
use BulkGate\Sdk\MessageSender;
use BulkGate\Sms\Message\PhoneNumber;
use BulkGate\Sms\Sender;
use Doctrine\ORM\EntityManagerInterface;
class SmsService
{
private $app_key;
private $app_token;
private MessageSender $sender;
private ConfigurationRepository $configurationRepository;
private EntityManagerInterface $entityManager;
public function __construct(ConfigurationRepository $configurationRepository, EntityManagerInterface $entityManager)
{
// $this->bearerKey = $configurationRepository->findOneBy(['name' => 'TELIASMS_KEY'])->getValue();
// $this->bearerKey = $configurationRepository->findOneBy(['name' => 'TELIASMS_KEY'])->getValue();
$this->app_key = $configurationRepository->findOneBy(['name' => 'BULKGATE_APPLICATION_ID'])->getValue();
$this->app_token = $configurationRepository->findOneBy(['name' => 'BULKGATE_APPLICATION_TOKEN'])->getValue();
$connection = new ConnectionStream($this->app_key, $this->app_token);
$this->sender = new MessageSender($connection);
$this->configurationRepository = $configurationRepository;
$this->entityManager = $entityManager;
}
/**
* @param string $phone Number to send a message to.
* @param string $content Content for an SMS Message
* @return bool|string Either false on failure, or json response on success
*/
public function send($phone, $content, $forceSMS = false)
{
if(!$phone || !is_string($phone))
{
return false;
}
$ltSymbols = [
'ą' => 'a',
'č' => 'c',
'ę' => 'e',
'ė' => 'e',
'į' => 'i',
'š' => 's',
'ų' => 'u',
'ū' => 'u',
'ž' => 'z',
'Ą' => 'A',
'Č' => 'C',
'Ę' => 'E',
'Ė' => 'E',
'Į' => 'I',
'Š' => 'S',
'Ų' => 'U',
'Ū' => 'U',
'Ž' => 'Z',
];
$content = str_replace(array_keys($ltSymbols), array_values($ltSymbols), $content);
$content = str_replace(" - ", "-", $content);
$pattern = '/^86/i';
$phone = preg_replace($pattern, '3706', $phone);
if(!$forceSMS) {
return $this->sendMultiChannel($phone, $content);
}
//old
$curl = curl_init();
$body = array(
"application_id"=> $this->app_key,
"application_token"=> $this->app_token,
"number"=> $phone,
"text"=> $content,
"unicode"=>true,
"sender_id"=> "gText",
"sender_id_value"=> "COREPETITUS",
"country"=>"lt"
);
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://portal.bulkgate.com/api/1.0/simple/transactional',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Cookie: _nss=1'
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
/**
* @param array[
* {recipient: string, text: string},
*
* {recipient: string, text: string},
*
* {recipient: string, text: string}] $messages
* @return bool|string Either false on failure, or json response on success
*/
public function sendAll(array $messages)
{
$ltSymbols = [
'ą' => 'a',
'č' => 'c',
'ę' => 'e',
'ė' => 'e',
'į' => 'i',
'š' => 's',
'ų' => 'u',
'ū' => 'u',
'ž' => 'z',
'Ą' => 'A',
'Č' => 'C',
'Ę' => 'E',
'Ė' => 'E',
'Į' => 'I',
'Š' => 'S',
'Ų' => 'U',
'Ū' => 'U',
'Ž' => 'Z',
];
foreach ($messages as &$message) {
$message['text'] = str_replace(array_keys($ltSymbols), array_values($ltSymbols), $message['text']);
$message['text'] = str_replace(" - ", "-", $message['text']);
}
$connection = new ConnectionStream($this->app_key, $this->app_token);
$sender = new MessageSender($connection);
$sms_configurator = new SmsConfigurator();
$sms_configurator->portalProfile(13361);
$bulkMessages = new Bulk();
foreach($messages as $sms){
$pattern = '/^86/i';
$phone = preg_replace($pattern, '3706', $sms['recipient']);
// $sms = new Sms($phone, $sms['text']);
// $sms_configurator->configure($sms);
// $bulkMessages[]=$sms;
$this->sendMultiChannel($phone,$sms['text']);
}
return true;
// return $sender->send($bulkMessages);
}
public function sendMultiChannel($phone, $content)
{
// $phone_number = new PhoneNumber($phone,"lt");
$smsContent = $content;
$ltSymbols = [
'ą' => 'a',
'č' => 'c',
'ę' => 'e',
'ė' => 'e',
'į' => 'i',
'š' => 's',
'ų' => 'u',
'ū' => 'u',
'ž' => 'z',
'Ą' => 'A',
'Č' => 'C',
'Ę' => 'E',
'Ė' => 'E',
'Į' => 'I',
'Š' => 'S',
'Ų' => 'U',
'Ū' => 'U',
'Ž' => 'Z',
];
$smsContent = str_replace(array_keys($ltSymbols), array_values($ltSymbols), $smsContent);
$smsContent = str_replace(" - ", "-", $smsContent);
$smstext = new SimpleText($smsContent);
$vibertext = new SimpleText($content);
$timeout = 5;
$message = new MultiChannel($phone);
$message->sms($smstext, 'gText', 'COREPETITUS', false);
$message->viber($vibertext, "Corepetitus korepetitoriai", null, null, $timeout);
$message->setPrimaryChannel(Channel::VIBER);
$smsLog = new SMSLog();
$smsLog->setMessage($vibertext);
$smsLog->setPhoneNumber($phone);
$smsLog->setCreatedAt(new \DateTime());
$this->entityManager->persist($smsLog);
$this->entityManager->flush();
$res = $this->sender->send($message);
return $res;
}
public function sendMultiBulk()
{
$numbers = ['+37065520239','+37063794090','+37066229936'];
$bulkMessages = new Bulk();
foreach($numbers as $number){
$pattern = '/^86/i';
$phone = preg_replace($pattern, '3706', $number);
$text = new SimpleText('Labas as krabas v2');
$timeout = 5;
$message = new MultiChannel($phone);
$message->sms($text, 'gText', 'COREPETITUS', false);
$button = null;
$message->viber($text, "Corepetitus korepetitoriai", $button, null, $timeout);
$message->setPrimaryChannel(Channel::VIBER);
$bulkMessages[]=$message;
}
// $phone_number = new PhoneNumber($phone,"lt");
// $text = new SimpleText($content);
// $timeout = 5;
// $message = new MultiChannel($phone);
// $message->sms($text, 'gText', 'COREPETITUS', false);
// $message->viber($text, "Corepetitus korepetitoriai", null, null, $timeout);
// $message->setPrimaryChannel(Channel::VIBER);
$res = $this->sender->send($bulkMessages);
return $res;
}
}