Skip to content

Commit 4b090c6

Browse files
author
Christoph Rumpel
authored
Merge pull request #15 from laravel-notification-channels/feature/addImage
Add media to Twitter status update
2 parents 0fa15bb + 4bfc2f7 commit 4b090c6

11 files changed

+317
-129
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to the `Twitter notification channel` will be documented in this file
44

5+
## 0.0.5 - 2016-09-17
6+
7+
- refactor the code to provide separate classes for Twitter status updates and direct messages
8+
59

610
## 0.0.4 - 2016-08-27
711

README.md

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ This package makes it easy to send notifications using [Twitter](https://dev.twi
1414
## Contents
1515

1616
- [Installation](#installation)
17-
- [Setting up the Twitter service](#setting-up-the-Twitter-service)
17+
- [Setting up the Twitter service](#setting-up-the-twitter-service)
1818
- [Usage](#usage)
19-
- [Available Message methods](#available-message-methods)
19+
- [Publish Twitter status update](#publish-twitter-status-update)
20+
- [Publish Twitter status update with images](#publish-twitter-status-update-with-images)
21+
- [Send a direct message](#send-a-direct-message)
2022
- [Changelog](#changelog)
2123
- [Testing](#testing)
2224
- [Security](#security)
@@ -73,7 +75,7 @@ Follow Laravel's documentation to add the channel to your Notification class.
7375
use NotificationChannels\Twitter\TwitterChannel;
7476
use NotificationChannels\Twitter\TwitterMessage;
7577

76-
class NewForumDiscussionCreated extends Notification
78+
class NewsWasPublished extends Notification
7779
{
7880

7981
/**
@@ -88,25 +90,37 @@ class NewForumDiscussionCreated extends Notification
8890
}
8991

9092
public function toTwitter($notifiable) {
91-
return new TwitterMessage('Why Laravel Notification Channels are awesome -> url:...');
93+
return new TwitterStatusUpdate('Laravel notifications are awesome!');
9294
}
9395
}
9496
```
9597

96-
Take a closer look at the `TwitterMessage` object. Like this, it will send a status update to your Twitter timeline.
98+
Take a closer look at the `TwitterStatusUpdate` object. This is where the magic happens.
9799
````php
98100
public function toTwitter($notifiable) {
99-
return new TwitterMessage('Why Laravel Notification Channels are awesome -> url:...');
101+
return new TwitterStatusUpdate('Laravel notifications are awesome!');
100102
}
101-
````
103+
````
104+
### Publish Twitter status update with images
105+
It is possible to publish images with your status update too. You just have to pass the image paths as the second
106+
parameter. These images will then be shown next to your Twitter status message.
107+
````php
108+
public function toTwitter($notifiable) {
109+
return new TwitterStatusUpdate(
110+
'Laravel notifications are awesome!',
111+
[public_path('marcel.png'), public_path('mohamed.png'), public_path('freek.png')]
112+
);
113+
}
114+
````
102115
### Send a direct message
103-
But it is possible to send a Twitter direct message too. Just provide a second argument. The first one will be the user, the second one the message:
116+
To send a Twitter direct message to a specific user, you will need the `TwitterDirectMessage` class. Provide the Twitter
117+
user handler as the first parameter and the the message as the second one.
104118
````php
105119
public function toTwitter($notifiable) {
106-
return new TwitterMessage('christophrumpel', 'Hey Christoph, whats up?');
120+
return new TwitterDirectMessage('marcelpociot', 'Hey Marcel, it was nice meeting you at the Larcon.');
107121
}
108122
````
109-
Make sure the user is following you on Twitter. If this is not the case, it will throw an Exception.
123+
Make sure the user is following you on Twitter to make this work.
110124

111125

112126

src/Exceptions/CouldNotSendNotification.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class CouldNotSendNotification extends \Exception
66
{
77
public static function serviceRespondedWithAnError($response)
88
{
9-
$responseBody = print_r($response->getBody(), true);
9+
$responseBody = print_r($response->errors[0]->message, true);
1010

1111
return new static("Couldn't post Notification. Response: ".$responseBody);
1212
}

src/TwitterChannel.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,20 @@ public function __construct(TwitterOAuth $twitter)
3030
public function send($notifiable, Notification $notification)
3131
{
3232
$twitterMessage = $notification->toTwitter($notifiable);
33-
if ($twitterMessage->getReceiver() != null) {
34-
$response = $this->twitter->post('direct_messages/new', [
35-
'text' => $twitterMessage->getContent(),
36-
'screen_name' => $twitterMessage->getReceiver(),
37-
]);
38-
} else {
39-
$response = $this->twitter->post(
40-
'statuses/update', ['status' => $twitterMessage->getContent()]);
33+
if (is_a($twitterMessage, TwitterStatusUpdate::class) && $twitterMessage->getImagePaths()) {
34+
$this->twitter->setTimeouts(10, 15);
35+
36+
$twitterMessage->imageIds = $twitterMessage->getImagePaths()->map(function ($path) {
37+
$media = $this->twitter->upload('media/upload', ['media' => $path]);
38+
39+
return $media->media_id_string;
40+
});
4141
}
4242

43-
if ($response->getHttpCode() !== 200) {
44-
throw CouldNotSendNotification::serviceRespondedWithAnError($response);
43+
$this->twitter->post($twitterMessage->getApiEndpoint(), $twitterMessage->getRequestBody());
44+
45+
if ($this->twitter->getLastHttpCode() !== 200) {
46+
throw CouldNotSendNotification::serviceRespondedWithAnError($this->twitter->getLastBody());
4547
}
4648
}
4749
}

src/TwitterDirectMessage.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace NotificationChannels\Twitter;
4+
5+
class TwitterDirectMessage
6+
{
7+
/** @var string */
8+
private $content;
9+
10+
/**
11+
* @var string
12+
*/
13+
private $to;
14+
15+
/**
16+
* @var string
17+
*/
18+
private $apiEndpoint = 'direct_messages/new';
19+
20+
/*
21+
* @param string $content
22+
*/
23+
public function __construct(string $to, string $content)
24+
{
25+
$this->to = $to;
26+
$this->content = $content;
27+
}
28+
29+
/**
30+
* Get Twitter direct messsage content.
31+
*
32+
* @return string
33+
*/
34+
public function getContent()
35+
{
36+
return $this->content;
37+
}
38+
39+
/**
40+
* Get Twitter direct message receiver.
41+
*
42+
* @return string
43+
*/
44+
public function getReceiver()
45+
{
46+
return $this->to;
47+
}
48+
49+
/**
50+
* Return Twitter direct message api endpoint.
51+
* @return string
52+
*/
53+
public function getApiEndpoint()
54+
{
55+
return $this->apiEndpoint;
56+
}
57+
58+
/**
59+
* Build Twitter request body.
60+
* @return array
61+
*/
62+
public function getRequestBody()
63+
{
64+
$body = [
65+
'screen_name' => $this->getReceiver(),
66+
'text' => $this->getContent(),
67+
];
68+
69+
return $body;
70+
}
71+
}

src/TwitterMessage.php

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

src/TwitterStatusUpdate.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace NotificationChannels\Twitter;
4+
5+
class TwitterStatusUpdate
6+
{
7+
/** @var string */
8+
protected $content;
9+
10+
/**
11+
* @var array
12+
*/
13+
private $imagePaths;
14+
15+
/**
16+
* @var array
17+
*/
18+
public $imageIds;
19+
20+
/**
21+
* @var string
22+
*/
23+
private $apiEndpoint = 'statuses/update';
24+
25+
/*
26+
* @param string $content
27+
*/
28+
public function __construct(string $content, array $imagePaths = null)
29+
{
30+
$this->content = $content;
31+
$this->imagePaths = $imagePaths ? collect($imagePaths) : null;
32+
}
33+
34+
/**
35+
* Get Twitter status update content.
36+
*
37+
* @return string
38+
*/
39+
public function getContent()
40+
{
41+
return $this->content;
42+
}
43+
44+
/**
45+
* Get Twitter status update image paths.
46+
*
47+
* @return string
48+
*/
49+
public function getImagePaths()
50+
{
51+
return $this->imagePaths;
52+
}
53+
54+
/**
55+
* Return Twitter status update api endpoint.
56+
* @return string
57+
*/
58+
public function getApiEndpoint()
59+
{
60+
return $this->apiEndpoint;
61+
}
62+
63+
/**
64+
* Build Twitter request body.
65+
* @return array
66+
*/
67+
public function getRequestBody()
68+
{
69+
$body = [
70+
'status' => $this->getContent(),
71+
];
72+
73+
if ($this->imageIds) {
74+
$body['media_ids'] = $this->imageIds->implode(',');
75+
}
76+
77+
return $body;
78+
}
79+
}

0 commit comments

Comments
 (0)