Package that integrates SemySMS API into your Laravel 5 app.
composer require allanvb/laravel-semysmsIf you're using Laravel 5.5 or above, the package will automatically register provider and facade.
Add Allanvb\LaravelSemysms\SemySmsServiceProvider to the providers array in your config/app.php:
'providers' => [
// Other service providers...
Allanvb\LaravelSemysms\SemySmsServiceProvider::class,
],Add an alias in your config/app.php:
'aliases' => [
...
'SemySMS' => Allanvb\LaravelSemysms\Facades\SemySMS::class,
],Or you can use the facade class when needed:
use Allanvb\Semysms\Facades\SemySMS;Look at one of the following topics to learn more about SemySMS package.
You can use php artisan vendor:publish to copy the configuration file to your app's config directory:
$ php artisan vendor:publish --provider="Allanvb\LaravelSemysms\SemySmsServiceProvider" --tag="config"Then update config/semy-sms.php with your credentials. Also you can update your .env file with the following:
SEMYSMS_TOKEN=your_access_token
SEMYSMS_DEVICE_ID=default_device_idAll methods uses Validator to validate passed data.
By default, methods will try to use for all requests the device you specified in your SEMYSMS_DEVICE_ID variable.
To use the SemySMS Library you can access the facade, or request the instance from the service container:
SemySMS::sendOne([
'to' => '+1234567890',
'text' => 'My first message.'
]);Or
app('semy-sms')->sendOne([
'to' => '+1234567890',
'text' => 'My first message.'
]);- All numbers must have international format.
All methods and events will return a Collection, so you can use all available methods to manipulate response.
For example: SemySMS::getInbox()->sortByDesc('date');
SemySMS::sendOne([
'to' => '+1234567890',
'text' => 'Test message'
]);Available parameters:
to- (string) Phone number in international format (required).text- (string) SMS Text, max 255 symbols (required).device_id- (string) Device ID or active.
device_id parameter can also take the active value which means that the service will distribute new SMS between all your active devices.
SemySMS::sendMultiple([
'to' => ['+1234567890','+1567890234','+1902345678'],
'text' => 'Test message'
]);Available parameters:
to- (array) List of phones in international format (required).text- (string) SMS Text, max 255 symbols (required).
In case you want to have more control on sending multiple messages you can use chaining methods.
$messages = SemySMS::multiple();
$messages->addRecipient([
'to' => '+1234567890',
'text' => 'Test message',
]);
$messages->addRecipient([
'to' => '+1567890234',
'text' => 'Test message 2',
]);
$messages->send();Available parameters:
to- (string) Phone number in international format (required).text- (string) SMS Text, max 255 symbols (required).device_id- (int) Device ID.my_id- (string) SMS code from your accounting system
- This feature works only on Android 8.0+
SemySMS::ussd([
'to' => '*123#'
]);You can use 'device_id' parameter to perform USSD request from specific device.
SemySMS::getOutbox();Optional, you can specify a interval of time you want to get by using Interval helper like this.
SemySMS::getOutbox([
'interval' => Interval::days(3)
]);SemySMS::getInbox();SemySMS::deleteOutbox();SemySMS::deleteInbox();deleteOutbox() and deleteInbox() methods will return deleted messages.
Optional, you can use filter for getOutbox(), getInbox(), deleteOutbox() and deleteInbox() methods.
Available parameters:
interval- (Interval) Interval of time.device_id- (int) Device ID.start_id- (int) Start ID of list filter.end_id- (int) End ID of list filter.list_id- (array) List of SMS codes.phone- (string) Phone number.
SemySMS::getDevices();By default this method will return list of all devices connected to account.
Available parameters:
status- (string) active|archived.list_id- (array) List of devices.
SemySMS::cancelSMS();This method will cancel all SMS which was not sent to your default device.
You can request canceling of all SMS for specific device by passing device_id, or specific message by passing sms_id to array.
The package have events built in. There are three events available for you to listen for.
| Event | Fired | Parameter |
|---|---|---|
| semy-sms.sent | When message sent. | 'To' and 'text' parameters of SMS that was sent. |
| semy-sms.sent-multiple | When multiple message sent. | List of phones, devices and message that was sent. |
| semy-sms.received | When new message income. | DeviceID, Sender and Text of income message. |
| semy-sms.ussd-response | When ussd response income. | DeviceID, Sender and Text of income message. |
This package also provide notification channel for SemySMS.
To use notification channel you must use SemySmsChannel::class in via method of your notification class.
After that you will be able to use toSemySms() method for sending messages.
See example below.
use Illuminate\Notifications\Notification;
use Allanvb\LaravelSemysms\Channels\SemySmsChannel;
use Allanvb\LaravelSemysms\Channels\SemySmsMessage;
class MyNotification extends Notification
{
public function via($notifiable)
{
return [SemySmsChannel::class];
}
public function toSemySms($notifiable)
{
return (new SemySmsMessage)
->text('My first notification message.');
}
}You can add recipient in two ways.
- First is by using
routeNotificationForSemySMS()in your notifiable model as below.
// User model
public function routeNotificationForSemySMS()
{
return $this->phone;
}- Second way is to use
to()method inside your notification.
public function toSemySms($notifiable)
{
return (new SemySmsMessage)
->to('+1234567890')
->text('My second notification message.');
}If you'll use both, then to() method will be used as primary.
The package can throw the following exceptions:
| Exception | Reason |
|---|---|
| SemySmsValidationException | When method params don't pass validation. |
| RequestException | When HTTP response will be different than 200. |
| SmsNotSentException | When something went wrong with the request to SemySMS servers. |
| InvalidIntervalException | When you pass invalid Interval |
If you want to get incoming messages from your devices, you can use
https://yourdomain.com/semy-sms/receive route in your SemySMS control panel.
To get this route working you need make some actions:
- Change
catch_incomingtotruein your config file. - Add
semy-sms/receiveroute to your$exceptvariable inVerifyCsrfTokenmiddleware.
After that, you will be able to listen for semy-sms.received Event.
If you performed an ussd request, you can listen for semy-sms.ussd-response Event to process USSD response.
In case that you get USSD response as SMS, you can add sender name to ussd_senders in your config file.
You can get more information about Events in Laravel official documentation
Interval class offers the following methods: hours(), days(), weeks(), months() and years().
If you want to have more control on Interval you can pass a startDate and an endDate to the object.
$startDate = Carbon::yesterday()->subDays(1);
$endDate = Carbon::yesterday();
Interval::create($startDate, $endDate);The MIT License (MIT). Please see License File for more information.