Skip to content

Commit 0cd364a

Browse files
authored
Merge pull request #1 from laravel-notification-channels/master
Updating my fork
2 parents dd16c40 + 92aee2a commit 0cd364a

11 files changed

+50
-57
lines changed

.scrutinizer.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

.styleci.yml

Lines changed: 0 additions & 4 deletions
This file was deleted.

README.md

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
This package makes it easy to send Laravel notifications using [Twitter](https://dev.twitter.com/rest/public). (Laravel 8+)
1414

15-
PS: v.7.0.0 only supports Laravel 10 and PHP 8.1. If you have an older Laravel application or PHP version, you can use an older version of this package. Be aware that these are no longer maintained.
15+
PS: v8 now uses the new Twitter API V2. Please read the upgrade guide for your app [here](https://developer.twitter.com/en/docs/twitter-api/migrate/overview).
1616

1717
## Contents
1818

@@ -37,7 +37,7 @@ PS: v.7.0.0 only supports Laravel 10 and PHP 8.1. If you have an older Laravel a
3737

3838
This package is part of the [Laravel Notification Channels](http://laravel-notification-channels.com/) project. It provides additional Laravel Notification channels to the ones given by [Laravel](https://laravel.com/docs/master/notifications) itself.
3939

40-
The Twitter channel makes it possible to send out Laravel notifications as a `Twitter status update `(post on the timeline) or as a `direct message`.
40+
The Twitter channel makes it possible to send out Laravel notifications as a `Twitter tweet` (post on the timeline) or as a `direct message`.
4141

4242
## Installation
4343

@@ -51,9 +51,32 @@ composer require laravel-notification-channels/twitter
5151

5252
The service provider gets loaded automatically.
5353

54-
### Setting up the Twitter service
54+
### Twitter App & Credentials
5555

56-
You will need to [create](https://developer.twitter.com/apps/) a Twitter app to use this channel. Within this app, you will find the `keys and access tokens`. Place them inside your `.env` file. To load them, add this to your `config/services.php` file:
56+
You will need to [create](https://developer.twitter.com/apps/) a Twitter app to use this channel. Within this app, you will find the `keys and access tokens`.
57+
58+
Your Twitter app `must be within a project`. Also, make sure to activate the `user authentication settings`:
59+
60+
<img width="1289" alt="image" src="https://github.com/laravel-notification-channels/twitter/assets/1394539/9be260ba-d8db-4af1-be58-9e4ce3b0176d">
61+
62+
After that, you have to regenerate your access token and secret. If done correctly, you should see the right permissions for your access tokens:
63+
64+
<img width="796" alt="image" src="https://github.com/laravel-notification-channels/twitter/assets/1394539/6ee5fc99-0373-4ebb-b8c5-4c84a82632df">
65+
66+
67+
Make sure to copy the right credentials and place them inside your `.env` file.
68+
69+
```env
70+
TWITTER_CONSUMER_KEY=your-consumer-key
71+
TWITTER_CONSUMER_SECRET=your-consumer-secret
72+
TWITTER_ACCESS_TOKEN=your-accesss_token
73+
TWITTER_ACCESS_TOKEN_SECRET=your-access-token-secret
74+
```
75+
76+
<img width="1168" alt="image" src="https://github.com/laravel-notification-channels/twitter/assets/1394539/7e68325d-0255-4f66-a310-86f3277f52a7">
77+
78+
79+
To load them, add this to your `config/services.php` file:
5780

5881
```php
5982
...
@@ -66,8 +89,6 @@ You will need to [create](https://developer.twitter.com/apps/) a Twitter app to
6689
...
6790
```
6891

69-
This will load the Twitter app data from the `.env` file. Make sure to use the same keys you have used like `TWITTER_CONSUMER_KEY`.
70-
7192
## Usage
7293

7394
To use this package, you need to create a notification class, like `NewsWasPublished` from the example below, in your Laravel application. Make sure to check out [Laravel's documentation](https://laravel.com/docs/master/notifications) for this process.
@@ -160,7 +181,7 @@ public function toTwitter(mixed $notifiable): TwitterMessage
160181
}
161182
````
162183
> Note that the reply status ID will be ignored if you omit the author of the original tweet, according to Twitter docs.
163-
### Send a direct message
184+
### Send a direct message (NOT working with the FREE Twitter API plan!)
164185
To send a Twitter direct message to a specific user, you will need the `TwitterDirectMessage` class. Provide the Twitter user handler as the first parameter and the message as the second one.
165186
````php
166187
public function toTwitter(mixed $notifiable): TwitterMessage

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
"ext-fileinfo": "*"
2121
},
2222
"require-dev": {
23+
"laravel/pint": "^1.10",
2324
"mockery/mockery": "^1.3.1",
24-
"phpunit/phpunit": "^9.3",
25-
"orchestra/testbench": "^8.0"
25+
"orchestra/testbench": "^8.0",
26+
"phpunit/phpunit": "^9.3"
2627
},
2728
"autoload": {
2829
"psr-4": {

src/Exceptions/CouldNotSendNotification.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static function serviceRespondsNotSuccessful(mixed $response): CouldNotSe
1111
if (isset($response->error)) {
1212
return new static("Couldn't post notification. Response: ".$response->error);
1313
}
14-
14+
1515
$responseBody = print_r($response->detail, true);
1616

1717
return new static("Couldn't post notification. Response: ".$responseBody);

src/TwitterChannel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __construct(protected TwitterOAuth $twitter)
1515
/**
1616
* Send the given notification.
1717
*
18-
* @param mixed $notifiable Should be an object that uses the Illuminate\Notifications\Notifiable trait.
18+
* @param mixed $notifiable Should be an object that uses the Illuminate\Notifications\Notifiable trait.
1919
*
2020
* @throws CouldNotSendNotification
2121
*/
@@ -40,7 +40,7 @@ public function send($notifiable, Notification $notification): array|object
4040
$requestBody,
4141
$twitterMessage->isJsonRequest,
4242
);
43-
43+
4444
if ($this->twitter->getLastHttpCode() !== 201) {
4545
throw CouldNotSendNotification::serviceRespondsNotSuccessful($this->twitter->getLastBody());
4646
}
@@ -51,7 +51,7 @@ public function send($notifiable, Notification $notification): array|object
5151
/**
5252
* Use per user settings instead of default ones.
5353
*
54-
* @param object $notifiable Provide an object that uses the Illuminate\Notifications\Notifiable trait.
54+
* @param object $notifiable Provide an object that uses the Illuminate\Notifications\Notifiable trait.
5555
*/
5656
private function changeTwitterSettingsIfNeeded(object $notifiable)
5757
{

src/TwitterServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function boot()
2121
config('services.twitter.access_token'),
2222
config('services.twitter.access_secret')
2323
);
24-
24+
2525
return $connection;
2626
});
2727
}

src/TwitterStatusUpdate.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
class TwitterStatusUpdate extends TwitterMessage
1010
{
1111
public ?Collection $imageIds = null;
12+
1213
public ?Collection $videoIds = null;
14+
1315
private ?array $images = null;
16+
1417
private ?array $videos = null;
18+
1519
private ?int $inReplyToTweetId = null;
1620

1721
/**
@@ -90,9 +94,6 @@ public function inReplyTo(int $tweetId): self
9094
return $this;
9195
}
9296

93-
/**
94-
* @return int|null
95-
*/
9697
public function getInReplyToTweetId(): ?int
9798
{
9899
return $this->inReplyToTweetId;
@@ -112,7 +113,7 @@ public function getRequestBody(): array
112113

113114
if ($mediaIds->count() > 0) {
114115
$body['media'] = [
115-
'media_ids' => $mediaIds->toArray()
116+
'media_ids' => $mediaIds->toArray(),
116117
];
117118
}
118119

tests/TwitterChannelTest.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function it_can_send_a_status_update_notification()
4242
$this->twitter->shouldReceive('getLastHttpCode')
4343
->once()
4444
->andReturn(201);
45-
45+
4646
$this->channel->send(new TestNotifiable(), new TestNotification());
4747
}
4848

@@ -60,7 +60,7 @@ public function it_can_send_a_status_update_notification_with_images()
6060
->once()
6161
->with(
6262
'tweets',
63-
['text' => 'Laravel Notification Channels are awesome!', 'media' => [ 'media_ids' => [2]]],
63+
['text' => 'Laravel Notification Channels are awesome!', 'media' => ['media_ids' => [2]]],
6464
true
6565
)
6666
->andReturn([]);
@@ -96,7 +96,7 @@ public function it_can_send_a_status_update_notification_with_videos()
9696
->once()
9797
->with(
9898
'tweets',
99-
['text' => 'Laravel Notification Channels are awesome!', 'media' => [ 'media_ids' => [2]]],
99+
['text' => 'Laravel Notification Channels are awesome!', 'media' => ['media_ids' => [2]]],
100100
true
101101
)
102102
->andReturn([]);
@@ -195,7 +195,7 @@ public function it_throws_an_exception_when_it_could_not_send_the_notification_w
195195
->once()
196196
->with($media->media_id_string)
197197
->andReturn($status);
198-
198+
199199
$this->expectException(CouldNotSendNotification::class);
200200

201201
$this->channel->send(new TestNotifiable(), new TestNotificationWithVideo());
@@ -265,18 +265,11 @@ class TestNotificationWithReplyToStatusId extends Notification
265265
{
266266
private int $replyToStatusId;
267267

268-
/**
269-
* @param int $replyToStatusId
270-
*/
271268
public function __construct(int $replyToStatusId)
272269
{
273270
$this->replyToStatusId = $replyToStatusId;
274271
}
275272

276-
/**
277-
* @param mixed $notifiable
278-
* @return TwitterMessage
279-
*/
280273
public function toTwitter(mixed $notifiable): TwitterMessage
281274
{
282275
return (new TwitterStatusUpdate('Laravel Notification Channels are awesome!'))

tests/TwitterDirectMessageTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
class TwitterDirectMessageTest extends TestCase
1010
{
1111
protected TwitterDirectMessage $messageWithUserId;
12+
1213
protected TwitterDirectMessage $messageWithScreenName;
14+
1315
protected TwitterOAuth $twitter;
1416

1517
public function setUp(): void

0 commit comments

Comments
 (0)