Skip to content

Commit 36a43d0

Browse files
authored
Merge pull request #21 from TechTomaz/dev-smsfix
Update the SMS Class after 46Elks change their optional value behavior
2 parents e928d30 + d598e45 commit 36a43d0

File tree

3 files changed

+98
-59
lines changed

3 files changed

+98
-59
lines changed

README.md

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,23 @@ With more endpoints to come. Feel free to contribute.
2323

2424
## Contents
2525

26-
- [Installation](#installation)
27-
- [Setting up the 46Elks service](#setting-up-the-46Elks-service)
28-
- [Usage](#usage)
29-
- [Available Message methods](#available-message-methods)
30-
- [Changelog](#changelog)
31-
- [Testing](#testing)
32-
- [Security](#security)
33-
- [Contributing](#contributing)
34-
- [Credits](#credits)
35-
- [License](#license)
26+
- [46Elks notification channel for Laravel](#46elks-notification-channel-for-laravel)
27+
- [Contents](#contents)
28+
- [Installation](#installation)
29+
- [Setting up the 46Elks service](#setting-up-the-46elks-service)
30+
- [Usage](#usage)
31+
- [Available mediums](#available-mediums)
32+
- [SMS](#sms)
33+
- [Available Message methods for sms](#available-message-methods-for-sms)
34+
- [MMS](#mms)
35+
- [Available Message methods](#available-message-methods)
36+
- [Error handling](#error-handling)
37+
- [Changelog](#changelog)
38+
- [Testing](#testing)
39+
- [Security](#security)
40+
- [Contributing](#contributing)
41+
- [Credits](#credits)
42+
- [License](#license)
3643

3744

3845
## Installation
@@ -45,7 +52,7 @@ composer require laravel-notification-channels/46elks
4552

4653

4754
add the following to your config/services.php
48-
```
55+
``` php
4956
'46elks' => [
5057
'username' => env('FORTY_SIX_ELKS_USERNAME'),
5158
'password' => env('FORTY_SIX_ELKS_PASSWORD'),
@@ -63,33 +70,52 @@ You will find your username and password at https://46elks.se/account
6370

6471

6572
To use this channel simply create a notification that has the following content:
73+
``` php
74+
use NotificationChannels\FortySixElks\FortySixElksChannel;
75+
use NotificationChannels\FortySixElks\FortySixElksSMS;
76+
77+
public function via($notifiable)
78+
{
79+
return [FortySixElksChannel::class];
80+
}
81+
82+
83+
public function to46Elks($notifiable)
84+
{
85+
return (new FortySixElksSMS())
86+
->line('Testsms')
87+
->line('Olle')
88+
->to('+46701234567')
89+
->from('Emil')
90+
// ->flash() - Optional
91+
// ->whenDelivered(URL) - Optional
92+
// ->dontLog() - Optional
93+
// ->dry() - Optional
94+
}
6695
```
67-
use NotificationChannels\FortySixElks\FortySixElksChannel;
68-
use NotificationChannels\FortySixElks\FortySixElksSMS;
69-
70-
public function via($notifiable)
71-
{
72-
return [FortySixElksChannel::class];
73-
}
7496

75-
76-
public function to46Elks($notifiable)
77-
{
78-
return (new FortySixElksSMS())
79-
->line('Testsms')
80-
->line('Olle')
81-
->to('+46762216234')
82-
->from('Emil')
83-
// -dry()
84-
}
97+
Another example without the notification implementation.
98+
``` php
99+
use NotificationChannels\FortySixElks\FortySixElksSMS;
100+
101+
(new FortySixElksSMS())
102+
->line('Testsms')
103+
->line('Olle')
104+
->to('+46701234567')
105+
->from('Emil')
106+
// ->flash() - Optional
107+
// ->whenDelivered(URL) - Optional
108+
// ->dontLog() - Optional
109+
// ->dry() - Optional
110+
->send();
85111
```
86112
### Available mediums
87113
#### SMS
88114
The FortySixElksSMS have the following methods, all chainable.
89115
### Available Message methods for sms
90116

91117

92-
``from($mixed)`` Accepts a string up to 11 characters or number. Sms will be sent with that name.
118+
``from($mixed)`` Accepts a string containing A-Z, a-z, 0-9 up to 11 characters or numbers. Space is not supported. Sms will be sent with that name.
93119

94120
``to($number)`` International phone number.
95121

@@ -101,9 +127,9 @@ The FortySixElksSMS have the following methods, all chainable.
101127
No SMS message will be sent when this is enabled. To be able inspect a dry() request you need to
102128
send your message to +4670000000 then you can inspect it at [https://46elks.com/logs](https://46elks.com/logs)
103129

104-
``whendelivered('http://localhost')`` This webhook URL will receive a POST request every time the delivery status changes.
130+
``whenDelivered('http://localhost.se/ping')`` This webhook URL will receive a POST request every time the delivery status changes.
105131

106-
``dontlog()`` Enable to avoid storing the message text in your history.
132+
``dontLog()`` Enable to avoid storing the message text in your history.
107133
The other parameters will still be stored.
108134

109135
#### MMS
@@ -130,7 +156,7 @@ If for any reason there would be an error when sending a notification it will fi
130156
`Illuminate\Notifications\Events\NotificationFailed` event. You can then listen for that.
131157

132158
Example:
133-
```
159+
``` php
134160
Event::listen(NotificationFailed::class, function($event){
135161
info('Error while sending sms');
136162
});

src/FortySixElksMedia.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class FortySixElksMedia
1818
'lines' => [],
1919
];
2020

21+
/**
22+
* @var GuzzleHttp\Client
23+
*/
24+
protected $client;
25+
2126
/**
2227
* @var int
2328
*/

src/FortySixElksSMS.php

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@ class FortySixElksSMS extends FortySixElksMedia implements FortySixElksMediaInte
88
{
99
const ENDPOINT = 'https://api.46elks.com/a1/SMS';
1010

11-
public $type = 'SMS';
12-
protected $flash = 'no';
13-
protected $dry = 'no';
14-
protected $whendelivered = null;
15-
protected $log = false;
11+
protected array $form_params;
1612

1713
/**
1814
* FortySixElksSMS constructor.
1915
*/
2016
public function __construct()
2117
{
18+
$this->form_params = [];
19+
2220
return parent::__construct();
2321
}
2422

@@ -29,16 +27,14 @@ public function send()
2927
{
3028
try {
3129
$response = $this->client->request('POST', self::ENDPOINT, [
32-
'form_params' => [
33-
'from' => $this->from,
34-
'message' => $this->getContent(),
35-
'to' => $this->phone_number,
36-
'flashsms' => $this->flash,
37-
'dryrun' => $this->dry,
38-
'whendelivered' => $this->whendelivered,
39-
'dontlog' => $this->log ? $this->log : '',
40-
],
41-
30+
'form_params' => array_merge(
31+
$this->form_params,
32+
[
33+
'from' => $this->from,
34+
'to' => $this->phone_number,
35+
'message' => $this->getContent(),
36+
]
37+
),
4238
]);
4339
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
4440
$response = $e->getResponse();
@@ -53,43 +49,55 @@ public function send()
5349
}
5450

5551
/**
56-
* @return $this
52+
* @return array
5753
*/
58-
public function flash()
54+
public function prepareParams(string $key, mixed $value): FortySixElksSMS
5955
{
60-
$this->flash = $this->payload['flash'] ?? 'yes';
56+
$this->form_params[$key] = $value;
6157

6258
return $this;
6359
}
6460

6561
/**
66-
* @return $this
62+
* @return void
63+
*/
64+
public function flash(string $value = 'yes'): FortySixElksSMS
65+
{
66+
self::prepareParams(key: 'flash', value: $value);
67+
68+
return $this;
69+
}
70+
71+
/**
72+
* @param string $value
73+
*
74+
* @return FortySixElksSMS
6775
*/
68-
public function dry()
76+
public function dry(string $value = 'yes'): FortySixElksSMS
6977
{
70-
$this->dry = $this->payload['dryrun'] ?? 'yes';
78+
self::prepareParams(key: 'dryrun', value: $value);
7179

7280
return $this;
7381
}
7482

7583
/**
7684
* @param string $url
7785
*
78-
* @return $this
86+
* @return FortySixElksSMS
7987
*/
80-
public function whendelivered($url)
88+
public function whenDelivered(string $url): FortySixElksSMS
8189
{
82-
$this->whendelivered = $this->payload['whendelivered'] ?? $url;
90+
self::prepareParams(key: 'whendelivered', value: $url);
8391

8492
return $this;
8593
}
8694

8795
/**
88-
* @return $this
96+
* @return FortySixElksSMS
8997
*/
90-
public function dontLog()
98+
public function dontLog(): FortySixElksSMS
9199
{
92-
$this->log = $this->payload['dontLog'] ?? 'message';
100+
self::prepareParams(key: 'dontlog', value: 'message');
93101

94102
return $this;
95103
}

0 commit comments

Comments
 (0)