Skip to content

Commit 75432fc

Browse files
committed
Work in progress
1 parent 18f8980 commit 75432fc

15 files changed

+322
-140
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Changelog
22

3-
All Notable changes to `:package_name` will be documented in this file
3+
All Notable changes to `twilio` will be documented in this file
44

55
## 1.0.0 - 201X-XX-XX
66

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The MIT License (MIT)
22

3-
Copyright (c) :author_name <:author_email>
3+
Copyright (c) Gregorio Hernández Caso <[email protected]>
44

55
> Permission is hereby granted, free of charge, to any person obtaining a copy
66
> of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,76 @@
1-
Use this repo as a skeleton for your new channel, once you're done please submit a Pull Request on [this repo](https://github.com/laravel-notification-channels/new-channels) with all the files.
1+
# Twilio notifications channel for Laravel 5.3 [WIP]
22

3-
Here's the latest documentation on Laravel 5.3 Notifications System:
3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/laravel-notification-channels/twilio.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/twilio)
4+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
5+
[![Build Status](https://img.shields.io/travis/laravel-notification-channels/twilio/master.svg?style=flat-square)](https://travis-ci.org/laravel-notification-channels/twilio)
6+
[![Quality Score](https://img.shields.io/scrutinizer/g/laravel-notification-channels/twilio.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/twilio)
7+
[![Total Downloads](https://img.shields.io/packagist/dt/laravel-notification-channels/twilio.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/twilio)
48

5-
https://laravel.com/docs/master/notifications
6-
7-
# A Boilerplate repo for contributions
8-
9-
This package makes it easy to send notifications using [xxxxx](link to service) with Laravel 5.3.
10-
11-
**Note:** Replace ```:channel_namespace``` ```:author_name``` ```:author_username``` ```:author_website``` ```:author_email``` ```:package_name``` ```:package_description``` with their correct values in [README.md](README.md), [CHANGELOG.md](CHANGELOG.md), [CONTRIBUTING.md](CONTRIBUTING.md), [LICENSE.md](LICENSE.md) and [composer.json](composer.json) files, then delete this line.
12-
This is where your description should go. Add a little code example so build can understand real quick how the package can be used. Try and limit it to a paragraph or two.
9+
This package makes it easy to send [Twilio notifications](https://documentation.twilio.com/docs) with Laravel 5.3.
1310

11+
## Installation
1412

13+
You can install the package via composer:
1514

16-
## Contents
15+
``` bash
16+
composer require laravel-notification-channels/twilio
17+
```
1718

18-
- [Installation](#installation)
19-
- [Setting up the xxxx service](#setting-up-the-xxxx-service)
20-
- [Usage](#usage)
21-
- [Available Message methods](#available-message-methods)
22-
- [Changelog](#changelog)
23-
- [Testing](#testing)
24-
- [Security](#security)
25-
- [Contributing](#contributing)
26-
- [Credits](#credits)
27-
- [License](#license)
19+
You must install the service provider:
2820

21+
```php
22+
// config/app.php
23+
'providers' => [
24+
...
25+
NotificationChannels\TwilioNotifications\Provider::class,
26+
];
27+
```
2928

30-
## Installation
29+
## Usage
3130

32-
Please also include the steps for any third-party service setup that's required for this package.
31+
Now you can use the channel in your `via()` method inside the notification:
3332

34-
### Setting up the xxxx service
33+
``` php
34+
use NotificationChannels\TwilioNotifications\Channel;
35+
use NotificationChannels\TwilioNotifications\SmsMessage;
36+
use Illuminate\Notifications\Notification;
3537

36-
Optionally include a few steps how users can set up the service.
38+
class AccountApproved extends Notification
39+
{
40+
public function via($notifiable)
41+
{
42+
return [Channel::class];
43+
}
3744

38-
## Usage
45+
public function toTwilio($notifiable)
46+
{
47+
return (new SmsMessage())
48+
->content("Your {$notifiable->service} account was approved!");
49+
}
50+
}
51+
```
3952

40-
Some code examples, make it clear how to use the package
53+
## Changelog
4154

42-
### Available methods
55+
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
4356

44-
A list of all available options
57+
## Testing
58+
59+
``` bash
60+
$ composer test
61+
```
4562

4663
## Contributing
4764

4865
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
4966

67+
## Security
68+
69+
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
70+
5071
## Credits
5172

52-
- [:author_name](https://github.com/:author_username)
73+
- [Gregorio Hernández Caso](https://github.com/gregoriohc)
5374
- [All Contributors](../../contributors)
5475

5576
## License

composer.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,31 @@
55
"license": "MIT",
66
"authors": [
77
{
8-
"name": ":author_name",
9-
"email": ":author_email",
10-
"homepage": ":author_url",
8+
"name": "Gregorio Hernández Caso",
9+
"email": "[email protected]",
10+
"homepage": "https://github.com/gregoriohc",
1111
"role": "Developer"
1212
}
1313
],
1414
"require": {
1515
"php": ">=5.6.4",
16+
"illuminate/events": "^5.3@dev",
1617
"illuminate/notifications": "^5.3@dev",
1718
"illuminate/support": "^5.3@dev",
18-
"illuminate/events": "^5.3@dev"
19+
"twilio/sdk": "^4.11"
1920
},
2021
"require-dev": {
2122
"mockery/mockery": "^0.9.5",
2223
"phpunit/phpunit": "4.*"
2324
},
2425
"autoload": {
2526
"psr-4": {
26-
"NotificationChannels\\:channel_namespace\\": "src"
27+
"NotificationChannels\\Twilio\\": "src"
2728
}
2829
},
2930
"autoload-dev": {
3031
"psr-4": {
31-
"NotificationChannels\\:channel_namespace\\Test\\": "tests"
32+
"NotificationChannels\\Twilio\\Test\\": "tests"
3233
}
3334
},
3435
"config": {

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
processIsolation="false"
1111
stopOnFailure="false">
1212
<testsuites>
13-
<testsuite name="Spatie Test Suite">
13+
<testsuite name="Test Suite">
1414
<directory>tests</directory>
1515
</testsuite>
1616
</testsuites>

src/CallMessage.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace NotificationChannels\Twilio;
4+
5+
class CallMessage
6+
{
7+
/**
8+
* The message TwiML url.
9+
*
10+
* @var string
11+
*/
12+
public $url;
13+
14+
/**
15+
* The phone number the message should be sent from.
16+
*
17+
* @var string
18+
*/
19+
public $from;
20+
21+
/**
22+
* Create a new message instance.
23+
*
24+
* @param string $url
25+
* @return void
26+
*/
27+
public function __construct($url = '')
28+
{
29+
$this->url = $url;
30+
}
31+
32+
/**
33+
* Set the message content.
34+
*
35+
* @param string $url
36+
* @return $this
37+
*/
38+
public function url($url)
39+
{
40+
$this->url = $url;
41+
42+
return $this;
43+
}
44+
45+
/**
46+
* Set the phone number the message should be sent from.
47+
*
48+
* @param string $from
49+
* @return $this
50+
*/
51+
public function from($from)
52+
{
53+
$this->from = $from;
54+
55+
return $this;
56+
}
57+
}

src/Channel.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace NotificationChannels\Twilio;
4+
5+
use Illuminate\Notifications\Notification;
6+
use NotificationChannels\Twilio\Events\MessageWasSent;
7+
use NotificationChannels\Twilio\Events\SendingMessage;
8+
use NotificationChannels\Twilio\Exceptions\CouldNotSendNotification;
9+
use Services_Twilio;
10+
11+
class Channel
12+
{
13+
/**
14+
* @var \Services_Twilio
15+
*/
16+
protected $twilio;
17+
18+
public function __construct(Services_Twilio $twilio)
19+
{
20+
$this->twilio = $twilio;
21+
}
22+
23+
/**
24+
* Send the given notification.
25+
*
26+
* @param mixed $notifiable
27+
* @param \Illuminate\Notifications\Notification $notification
28+
* @return void
29+
* @throws CouldNotSendNotification
30+
*/
31+
public function send($notifiable, Notification $notification)
32+
{
33+
if (!$to = $notifiable->routeNotificationFor('twilio')) {
34+
return;
35+
}
36+
37+
$config = config('services.twilio');
38+
39+
$message = $notification->toTwilio($notifiable);
40+
41+
if (is_string($message)) {
42+
$message = new SmsMessage($message);
43+
}
44+
45+
if (!$message instanceof SmsMessage::class &&
46+
!$message instanceof CallMessage::class
47+
) {
48+
$class = get_class($message) ?: 'Unknown';
49+
50+
throw CouldNotSendNotification::invalidMessageObject($class);
51+
}
52+
53+
$shouldSendMessage = event(new SendingMessage($notifiable, $notification, $message), [], true) !== false;
54+
55+
if (!$shouldSendMessage) {
56+
return;
57+
}
58+
59+
$response = null;
60+
61+
/** @var SmsMessage|CallMessage $message */
62+
try {
63+
if ($message instanceof SmsMessage::class) {
64+
$response = $this->twilio->account->messages->sendMessage(
65+
$message->from ?: $config['from'],
66+
$to,
67+
trim($message->content)
68+
);
69+
} elseif ($message instanceof CallMessage::class) {
70+
$response = $this->twilio->account->calls->create(
71+
$message->from ?: $config['from'],
72+
$to,
73+
trim($message->url)
74+
);
75+
}
76+
} catch (\Exception $exception) {
77+
throw CouldNotSendNotification::serviceRespondedWithAnException($exception);
78+
}
79+
80+
event(new MessageWasSent($notifiable, $notification, $response));
81+
}
82+
}

src/Events/MessageWasSent.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace NotificationChannels\:channel_namespace\Events;
3+
namespace NotificationChannels\Twilio\Events;
44

55
use Illuminate\Notifications\Notification;
66

@@ -11,14 +11,17 @@ class MessageWasSent
1111
/** @var \Illuminate\Notifications\Notification */
1212
protected $notification;
1313

14+
protected $response;
15+
1416
/**
1517
* @param $notifiable
1618
* @param \Illuminate\Notifications\Notification $notification
19+
* @param mixed|null $response
1720
*/
18-
public function __construct($notifiable, Notification $notification)
21+
public function __construct($notifiable, Notification $notification, $response = null)
1922
{
2023
$this->notifiable = $notifiable;
21-
2224
$this->notification = $notification;
25+
$this->response = $response;
2326
}
2427
}

src/Events/SendingMessage.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
<?php
22

3-
namespace NotificationChannels\:channel_namespace\Events;
3+
namespace NotificationChannels\Twilio\Events;
44

55
use Illuminate\Notifications\Notification;
66

77
class SendingMessage
88
{
9-
109
protected $notifiable;
1110

1211
/** @var \Illuminate\Notifications\Notification */
1312
protected $notification;
1413

14+
protected $message;
15+
1516
/**
1617
* @param $notifiable
1718
* @param \Illuminate\Notifications\Notification $notification
19+
* @param \NotificationChannels\Twilio\SmsMessage|\NotificationChannels\Twilio\CallMessage|mixed $message
1820
*/
19-
public function __construct($notifiable, Notification $notification)
21+
public function __construct($notifiable, Notification $notification, $message)
2022
{
2123
$this->notifiable = $notifiable;
22-
2324
$this->notification = $notification;
25+
$this->message = $message;
2426
}
2527
}

0 commit comments

Comments
 (0)