Skip to content

Commit d9692d7

Browse files
Merge pull request #82 from amiranagram/reply-to-status-update
Add reply to tweet status update feature
2 parents c9f7c02 + f0b702a commit d9692d7

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ return (new TwitterStatusUpdate('Laravel notifications are awesome!'))->withImag
120120
public_path('mohamed.png')
121121
]);
122122
````
123+
### Publish Twitter status update in reply to another tweet
124+
Additionally, you can publish a status update in reply to another tweet. That is possible using the method `inReplyTo`.
125+
````php
126+
public function toTwitter(mixed $notifiable): TwitterMessage
127+
{
128+
return (new TwitterStatusUpdate('@christophrumpel Laravel notifications are awesome!'))->inReplyTo(123);
129+
}
130+
````
131+
> Note that the reply status id will be ignored if you omit the author of the original tweet, according to Twitter docs.
123132
### Send a direct message
124133
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.
125134
````php

src/TwitterStatusUpdate.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class TwitterStatusUpdate extends TwitterMessage
1010
{
1111
public ?Collection $imageIds = null;
1212
private ?array $images = null;
13+
private ?int $inReplyToStatusId = null;
1314

1415
/**
1516
* @throws CouldNotSendNotification
@@ -52,6 +53,25 @@ public function getImages(): ?array
5253
return $this->images;
5354
}
5455

56+
/**
57+
* @param int $statusId
58+
* @return $this
59+
*/
60+
public function inReplyTo(int $statusId): self
61+
{
62+
$this->inReplyToStatusId = $statusId;
63+
64+
return $this;
65+
}
66+
67+
/**
68+
* @return int|null
69+
*/
70+
public function getInReplyToStatusId(): ?int
71+
{
72+
return $this->inReplyToStatusId;
73+
}
74+
5575
/**
5676
* Build Twitter request body.
5777
*/
@@ -63,6 +83,10 @@ public function getRequestBody(): array
6383
$body['media_ids'] = $this->imageIds->implode(',');
6484
}
6585

86+
if ($this->inReplyToStatusId) {
87+
$body['in_reply_to_status_id'] = $this->inReplyToStatusId;
88+
}
89+
6690
return $body;
6791
}
6892

tests/TwitterChannelTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,26 @@ public function it_can_send_a_status_update_notification_with_images()
7474
$this->channel->send(new TestNotifiable(), new TestNotificationWithImage());
7575
}
7676

77+
/** @test */
78+
public function it_can_send_a_status_update_notification_with_reply_to_status_id(): void
79+
{
80+
$postParams = [
81+
'status' => 'Laravel Notification Channels are awesome!',
82+
'in_reply_to_status_id' => $replyToStatusId = 123,
83+
];
84+
85+
$this->twitter->shouldReceive('post')
86+
->once()
87+
->with('statuses/update', $postParams, false)
88+
->andReturn([]);
89+
90+
$this->twitter->shouldReceive('getLastHttpCode')
91+
->once()
92+
->andReturn(200);
93+
94+
$this->channel->send(new TestNotifiable(), new TestNotificationWithReplyToStatusId($replyToStatusId));
95+
}
96+
7797
/** @test */
7898
public function it_throws_an_exception_when_it_could_not_send_the_notification()
7999
{
@@ -147,3 +167,26 @@ public function toTwitter(mixed $notifiable): TwitterMessage
147167
return (new TwitterStatusUpdate('Laravel Notification Channels are awesome!'))->withImage(public_path('image.png'));
148168
}
149169
}
170+
171+
class TestNotificationWithReplyToStatusId extends Notification
172+
{
173+
private int $replyToStatusId;
174+
175+
/**
176+
* @param int $replyToStatusId
177+
*/
178+
public function __construct(int $replyToStatusId)
179+
{
180+
$this->replyToStatusId = $replyToStatusId;
181+
}
182+
183+
/**
184+
* @param mixed $notifiable
185+
* @return TwitterMessage
186+
*/
187+
public function toTwitter(mixed $notifiable): TwitterMessage
188+
{
189+
return (new TwitterStatusUpdate('Laravel Notification Channels are awesome!'))
190+
->inReplyTo($this->replyToStatusId);
191+
}
192+
}

tests/TwitterStatusUpdateTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,22 @@ public function it_provides_exceeded_message_count_when_the_status_update_is_too
9393
$e->getMessage());
9494
}
9595
}
96+
97+
/** @test */
98+
public function it_has_in_reply_to_status_id_as_optional_parameter(): void
99+
{
100+
$message = new TwitterStatusUpdate('Hello world!');
101+
102+
$this->assertEquals(null, $message->getInReplyToStatusId());
103+
}
104+
105+
/** @test */
106+
public function it_accepts_in_reply_to_status_id(): void
107+
{
108+
$message = (new TwitterStatusUpdate($content = 'Foo!'))
109+
->inReplyTo($inReplyToStatusId = 12345);
110+
111+
$this->assertEquals($content, $message->getContent());
112+
$this->assertEquals($inReplyToStatusId, $message->getInReplyToStatusId());
113+
}
96114
}

0 commit comments

Comments
 (0)