Skip to content

Commit

Permalink
Merge pull request #6 from alhoqbani/dev
Browse files Browse the repository at this point in the history
Add artisan command to make new MobilyWs notifications
  • Loading branch information
alhoqbani committed Aug 15, 2017
2 parents c675f1b + f0d4ecb commit 96be6f2
Show file tree
Hide file tree
Showing 10 changed files with 719 additions and 59 deletions.
137 changes: 109 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
[![Latest Version on Packagist](https://img.shields.io/packagist/v/alhoqbani/laravel-mobily-ws-notification.svg?style=flat-square)](https://packagist.org/packages/alhoqbani/laravel-mobily-ws-notification)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://img.shields.io/travis/alhoqbani/laravel-mobily-ws-notification/master.svg?style=flat-square)](https://travis-ci.org/alhoqbani/laravel-mobily-ws-notification)
[![StyleCI](https://styleci.io/repos/100258454/shield)](https://styleci.io/repos/100258454)
[![SensioLabsInsight](https://img.shields.io/sensiolabs/i/:sensio_labs_id.svg?style=flat-square)](https://insight.sensiolabs.com/projects/:sensio_labs_id)
[![Quality Score](https://img.shields.io/scrutinizer/g/alhoqbani/laravel-mobily-ws-notification.svg?style=flat-square)](https://scrutinizer-ci.com/g/alhoqbani/laravel-mobily-ws-notification)
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/alhoqbani/laravel-mobily-ws-notification/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/alhoqbani/laravel-mobily-ws-notification/?branch=master)
[![Total Downloads](https://img.shields.io/packagist/dt/alhoqbani/laravel-mobily-ws-notification.svg?style=flat-square)](https://packagist.org/packages/alhoqbani/laravel-mobily-ws-notification)

Expand All @@ -15,8 +12,14 @@ This package makes it easy to send notifications using [MobilyWs](https://www.mo
## Contents

- [Installation](#installation)
- [Setting up the MobilyWs service](#setting-up-the-MobilyWs-service)
- [Package Installation](#package-installation)
- [Set up mobily.ws account](#set-up-mobily.ws-account)
- [Usage](#usage)
- [Credentials](#credentials)
- [Create Notification](#create-notification)
- [Routing SMS Notifications](#routing-sms-notifications)
- [Sending SMS](#sending-sms)
- [Scheduled SMS](#scheduled-sms)
- [Available Message methods](#available-message-methods)
- [TODO](#todo)
- [Changelog](#changelog)
Expand All @@ -27,49 +30,66 @@ This package makes it easy to send notifications using [MobilyWs](https://www.mo


## Installation
Install using composer:

### Package Installation

Install the package using composer:
```bash
composer require alhoqbani/laravel-mobily-ws-notification
```
Add service provider to your array of providers in `config/app.php`
Add service provider to your array of providers in `config/app.php`
> You don't need to do this step for laravel 5.5+
```php
NotificationChannels\MobilyWs\MobilyWsServiceProvider::class,
```
Publish the configuration file:
```bash
php artisan vendor:publish --provider="NotificationChannels\MobilyWs\MobilyWsServiceProvider"
```
### Setting up the Mobily.ws account

### Set up mobily.ws account
You must have an account with [MobilyWs](https://www.mobily.ws) to be able to use this package.

> This package has no affiliation with mobily.ws whatsoever.
## Usage
### Add your mobily.ws credentials to your `.env` file.
```php
MOBILY_WS_MOBILE=
#### Credentials.
You must add mobily.ws credentials to your `.env` file.

```
// Mobile number and password used for log in.
MOBILY_WS_MOBILE=
MOBILY_WS_PASSWORD=
// Name/Number of Sender must be approved by mobily.ws for GCC
// name/number of the sender which must be approved by mobily.ws for GCC
MOBILY_WS_SENDER=
```

### Make a new notification class using laravel artisan:
## Usage

### Create new notification:
Make a new notification class using laravel artisan
```bash
php artisan make:notification SmsNewUser
php artisan make:notification UserRegistered
```
### Configure the notification class to use MobilyWsChannel:
and configure the notification class to use MobilyWsChannel.

Or you could use our custom artisan command:
```bash
php artisan mobilyws:notification UserRegistered
```

The `toMobilyWs` method should return a string of the text message to be sent or an instance of `MobilyWsMessage`.

The `toMobilyWs` method should return a string of the text message to be sent.
See [Available Message methods](#available-message-methods) for more details.
```php
<?php

namespace App\Notifications;

use NotificationChannels\MobilyWs\MobilyWsChannel;
use Illuminate\Notifications\Notification;
use NotificationChannels\MobilyWs\MobilyWsChannel;
use NotificationChannels\MobilyWs\MobilyWsMessage;

class SmsNewUser extends Notification
class UserRegistered extends Notification
{
/**
* Get the notification's delivery channels.
Expand All @@ -83,14 +103,16 @@ class SmsNewUser extends Notification
}

/**
* Get the text message of the SMS.
* Get the text message representation of the notification
*
* @param mixed $notifiable
* @return string
* @param mixed $notifiable
* @param \NotificationChannels\MobilyWs\MobilyWsMessage $msg
*
* @return \NotificationChannels\MobilyWs\MobilyWsMessage|string
*/
public function toMobilyWs($notifiable)
public function toMobilyWs($notifiable, MobilyWsMessage $msg)
{
return "Dear $notifiable->name , Thank for your business with us";
return "Dear $notifiable->name, welcome to our website";
}
}
```
Expand Down Expand Up @@ -123,26 +145,85 @@ If you would like to customize the phone number the notification is delivered to
}
}
```
`routeNotificationForMobilyWs` should return a mobile number to which the SMS message will be sent.

Please note that the mobile number must start with the country code without leading zeros.

For example, `9665xxxxxxxx`

### Sending SMS:
```php
use App\Notifications\SmsNewUser;
use App\Notifications\UserRegistered;

$user = App\User::first();

$user->notify(new UserRegistered());
```

### Scheduled SMS
[MobilyWs](https://www.mobily.ws) Api allows for sending scheduled message which will be sent on the defined date/time.

> Please note that if you define time in the past, the message will be sent immediately by mobily.ws.
This library will not check if the defined time is in the future.

You can define the time on which the message should be sent by mobily.ws by calling `time` method on the MobilyWsMessage instance.
```php
public function toMobilyWs($notifiable)
{
return (new MobilyWsMessage)
->text("Message text")
->time(Carbon::parse("+1 week);
}
```
The `time` method accepts either a DateTime object or a timestamp.

### Available Message methods
In your notification, you must define a method `toMobilyWs` which will receive the notifiable entity (e.g User model) and an instance of `MobilyWsMessage`.

This method should return the text of the message to be sent as an SMS to mobily.ws or an instance of `MobilyWsMessage`.

```php
<?php

use NotificationChannels\MobilyWs\MobilyWsMessage;
//
/**
* Get the text message of the SMS.
*
* @param mixed $notifiable
* @return \NotificationChannels\MobilyWs\MobilyWsMessage|string
*/
public function toMobilyWs($notifiable)
{
return MobilyWsMessage::create("Text message");
}
```
You can also pass the message to `MobilyWsMessage` constructor:

`return new MobilyWsMessage("Text message");`

$user->notify(new SmsNewUser());
or set the text message using the `msg()` method:
```php
public function toMobilyWs($notifiable, MobilyWsMessage $msg)
{
return $msg->text($this->message);
}
```
Method `toMobilyWs` will receive an instance of `MobilyWsMessage` as the 2nd argument.
#### list of available methods :
`text()` To add the content of the text message
'time()' To set time of the scheduled sms.

## TODO
- [ ] Validate mobile numbers
- [ ] Validate text messages type and length
- [ ] Verify method `toMobilyWs` existence and config file.
- [ ] Add the option to send Scheduled SMS
- [ ] Add the the reset of params (MsgID, msgKey, deleteKey, timeSend, dateSend)
- [ ] Validate given time is in the future.
- [x] Verify method `toMobilyWs` existence and config file.
- [x] Add the option to send Scheduled SMS
- [ ] Add the the rest of params (MsgID, msgKey, deleteKey, ~~timeSend~~, ~~dateSend~~)
- [ ] Translate mobily.ws error messages
- [ ] Create artisan command to made mobily.ws notifications
- [ ] Add list of fired event to the documentation.

## Changelog

Expand Down
86 changes: 86 additions & 0 deletions src/Console/MobilyWsNotificationMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace NotificationChannels\MobilyWs\Console;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;

class MobilyWsNotificationMakeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'mobilyws:notification';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new Mobily.ws SMS notification class';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Notification';

/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
if (parent::fire() === false && ! $this->option('force')) {
return;
}
}

/**
* Build the class with the given name.
*
* @param string $name
* @return string
*/
protected function buildClass($name)
{
return parent::buildClass($name);
}

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/stubs/notification.stub';
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Notifications';
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the notification already exists.'],
];
}
}
49 changes: 49 additions & 0 deletions src/Console/stubs/notification.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace DummyNamespace;

use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use NotificationChannels\MobilyWs\MobilyWsChannel;
use NotificationChannels\MobilyWs\MobilyWsMessage;

class DummyClass extends Notification
{
use Queueable;

/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [MobilyWsChannel::class];
}

/**
* Get the text message representation of the notification
*
* @param mixed $notifiable
* @param \NotificationChannels\MobilyWs\MobilyWsMessage $msg
*
* @return \NotificationChannels\MobilyWs\MobilyWsMessage|string
*/
public function toMobilyWs($notifiable, MobilyWsMessage $msg)
{
return $msg->text('some text')
->time(Carbon::parse("+1 day"));
}
}
Loading

0 comments on commit 96be6f2

Please sign in to comment.