Implement splitting long message into multiple SMS or LMS

This commit is contained in:
Kijin Sung 2016-11-04 11:37:03 +09:00
parent 80fc8a0bd5
commit 06e9a40ef6
2 changed files with 121 additions and 8 deletions

View file

@ -49,6 +49,10 @@ class CoolSMS extends Base implements \Rhymix\Framework\Drivers\SMSInterface
{
try
{
// Initialize the sender.
$sender = new \Nurigo\Api\Message($this->_config['api_key'], $this->_config['api_secret']);
// Get recipients.
$recipients = $message->getRecipientsWithCountry();
foreach ($recipients as $recipient)
{
@ -56,11 +60,12 @@ class CoolSMS extends Base implements \Rhymix\Framework\Drivers\SMSInterface
$options = new \stdClass;
$options->from = $message->getFrom();
$options->to = $recipient->number;
$options->text = $message->getContent();
$options->charset = 'UTF-8';
$content_full = $message->getContent();
// Determine the message type based on the length.
$options->type = $message->checkLength($options->text, $this->_maxlength_sms) ? 'SMS' : 'LMS';
$detected_type = $message->checkLength($content_full, $this->_maxlength_sms) ? 'SMS' : 'LMS';
$options->type = $detected_type;
// If the message has a subject, it must be an LMS.
if ($subject = $message->getSubject())
@ -82,15 +87,49 @@ class CoolSMS extends Base implements \Rhymix\Framework\Drivers\SMSInterface
{
unset($options->subject);
unset($options->image);
$options->text = array_first($message->splitMessage($options->text, $this->_maxlength_sms));
$options->country = $recipient->country;
$options->type = 'SMS';
}
// Split the message if necessary.
if ($options->type === 'SMS' && $detected_type !== 'SMS')
{
$content_split = $message->splitMessage($content_full, $this->_maxlength_sms);
}
elseif ($options->type !== 'SMS' && !$message->checkLength($content_full, $this->_maxlength_lms))
{
$content_split = $message->splitMessage($content_full, $this->_maxlength_lms);
}
else
{
$content_split = array($content_full);
}
// Send the message.
$sender = new \Nurigo\Api\Message($this->_config['api_key'], $this->_config['api_secret']);
$result = $sender->send($options);
return (isset($result->success_count) && $result->success_count > 0) ? true : false;
$sent_once = false;
foreach ($content_split as $i => $content)
{
// If splitting a message, don't send the subject and image more than once.
if ($sent_once)
{
unset($options->subject);
unset($options->image);
}
// Set the content and send.
$options->text = $content;
$result = $sender->send($options);
$sent_once = true;
if (!$result->success_count)
{
$message->errors[] = 'Error while sending message ' . $i . ' of ' . count($content_split) . ' to ' . $options->to;
return false;
}
}
}
return true;
}
catch (\Nurigo\Exceptions\CoolsmsException $e)
{