Skip to content

Commit

Permalink
Merge pull request #82 from amiranagram/reply-to-status-update
Browse files Browse the repository at this point in the history
Add reply to tweet status update feature
  • Loading branch information
christophrumpel authored Dec 1, 2022
2 parents c9f7c02 + f0b702a commit d9692d7
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ return (new TwitterStatusUpdate('Laravel notifications are awesome!'))->withImag
public_path('mohamed.png')
]);
````
### Publish Twitter status update in reply to another tweet
Additionally, you can publish a status update in reply to another tweet. That is possible using the method `inReplyTo`.
````php
public function toTwitter(mixed $notifiable): TwitterMessage
{
return (new TwitterStatusUpdate('@christophrumpel Laravel notifications are awesome!'))->inReplyTo(123);
}
````
> Note that the reply status id will be ignored if you omit the author of the original tweet, according to Twitter docs.
### Send a direct message
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.
````php
Expand Down
24 changes: 24 additions & 0 deletions src/TwitterStatusUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TwitterStatusUpdate extends TwitterMessage
{
public ?Collection $imageIds = null;
private ?array $images = null;
private ?int $inReplyToStatusId = null;

/**
* @throws CouldNotSendNotification
Expand Down Expand Up @@ -52,6 +53,25 @@ public function getImages(): ?array
return $this->images;
}

/**
* @param int $statusId
* @return $this
*/
public function inReplyTo(int $statusId): self
{
$this->inReplyToStatusId = $statusId;

return $this;
}

/**
* @return int|null
*/
public function getInReplyToStatusId(): ?int
{
return $this->inReplyToStatusId;
}

/**
* Build Twitter request body.
*/
Expand All @@ -63,6 +83,10 @@ public function getRequestBody(): array
$body['media_ids'] = $this->imageIds->implode(',');
}

if ($this->inReplyToStatusId) {
$body['in_reply_to_status_id'] = $this->inReplyToStatusId;
}

return $body;
}

Expand Down
43 changes: 43 additions & 0 deletions tests/TwitterChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ public function it_can_send_a_status_update_notification_with_images()
$this->channel->send(new TestNotifiable(), new TestNotificationWithImage());
}

/** @test */
public function it_can_send_a_status_update_notification_with_reply_to_status_id(): void
{
$postParams = [
'status' => 'Laravel Notification Channels are awesome!',
'in_reply_to_status_id' => $replyToStatusId = 123,
];

$this->twitter->shouldReceive('post')
->once()
->with('statuses/update', $postParams, false)
->andReturn([]);

$this->twitter->shouldReceive('getLastHttpCode')
->once()
->andReturn(200);

$this->channel->send(new TestNotifiable(), new TestNotificationWithReplyToStatusId($replyToStatusId));
}

/** @test */
public function it_throws_an_exception_when_it_could_not_send_the_notification()
{
Expand Down Expand Up @@ -147,3 +167,26 @@ public function toTwitter(mixed $notifiable): TwitterMessage
return (new TwitterStatusUpdate('Laravel Notification Channels are awesome!'))->withImage(public_path('image.png'));
}
}

class TestNotificationWithReplyToStatusId extends Notification
{
private int $replyToStatusId;

/**
* @param int $replyToStatusId
*/
public function __construct(int $replyToStatusId)
{
$this->replyToStatusId = $replyToStatusId;
}

/**
* @param mixed $notifiable
* @return TwitterMessage
*/
public function toTwitter(mixed $notifiable): TwitterMessage
{
return (new TwitterStatusUpdate('Laravel Notification Channels are awesome!'))
->inReplyTo($this->replyToStatusId);
}
}
18 changes: 18 additions & 0 deletions tests/TwitterStatusUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,22 @@ public function it_provides_exceeded_message_count_when_the_status_update_is_too
$e->getMessage());
}
}

/** @test */
public function it_has_in_reply_to_status_id_as_optional_parameter(): void
{
$message = new TwitterStatusUpdate('Hello world!');

$this->assertEquals(null, $message->getInReplyToStatusId());
}

/** @test */
public function it_accepts_in_reply_to_status_id(): void
{
$message = (new TwitterStatusUpdate($content = 'Foo!'))
->inReplyTo($inReplyToStatusId = 12345);

$this->assertEquals($content, $message->getContent());
$this->assertEquals($inReplyToStatusId, $message->getInReplyToStatusId());
}
}

0 comments on commit d9692d7

Please sign in to comment.