1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Twitter; |
4
|
|
|
|
5
|
|
|
use Abraham\TwitterOAuth\TwitterOAuth; |
6
|
|
|
use NotificationChannels\Twitter\Exceptions\CouldNotSendNotification; |
7
|
|
|
|
8
|
|
|
class TwitterDirectMessage |
9
|
|
|
{ |
10
|
|
|
/** @var string */ |
11
|
|
|
private $content; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
private $to; |
15
|
|
|
|
16
|
|
|
/** @var bool */ |
17
|
|
|
public $isJsonRequest = true; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $apiEndpoint = 'direct_messages/events/new'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* TwitterDirectMessage constructor. |
24
|
|
|
* |
25
|
|
|
* @param $to |
26
|
|
|
* @param $content |
27
|
|
|
*/ |
28
|
6 |
|
public function __construct($to, $content) |
29
|
|
|
{ |
30
|
6 |
|
$this->to = $to; |
31
|
6 |
|
$this->content = $content; |
32
|
6 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get Twitter direct message content. |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
3 |
|
public function getContent() |
40
|
|
|
{ |
41
|
3 |
|
return $this->content; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get Twitter direct message receiver. |
46
|
|
|
* |
47
|
|
|
* @param TwitterOAuth $twitter |
48
|
|
|
* @return string |
49
|
|
|
* @throws CouldNotSendNotification |
50
|
|
|
*/ |
51
|
4 |
|
public function getReceiver(TwitterOAuth $twitter) |
52
|
|
|
{ |
53
|
4 |
|
if (is_int($this->to)) { |
54
|
3 |
|
return $this->to; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
$user = $twitter->get('users/show', [ |
58
|
1 |
|
'screen_name' => $this->to, |
59
|
|
|
'include_user_entities' => false, |
60
|
|
|
'skip_status' => true, |
61
|
|
|
]); |
62
|
|
|
|
63
|
1 |
|
if ($twitter->getLastHttpCode() === 404) { |
64
|
|
|
throw CouldNotSendNotification::userWasNotFound($twitter->getLastBody()); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
return $user->id; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Return Twitter direct message api endpoint. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
1 |
|
public function getApiEndpoint() |
76
|
|
|
{ |
77
|
1 |
|
return $this->apiEndpoint; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Build Twitter request body. |
82
|
|
|
* |
83
|
|
|
* @param TwitterOAuth $twitter |
84
|
|
|
* @return array |
85
|
|
|
* @throws CouldNotSendNotification |
86
|
|
|
*/ |
87
|
1 |
|
public function getRequestBody(TwitterOAuth $twitter) |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
'event' => [ |
91
|
1 |
|
'type' => 'message_create', |
92
|
|
|
'message_create' => [ |
93
|
|
|
'target' => [ |
94
|
1 |
|
'recipient_id' => $this->getReceiver($twitter), |
95
|
|
|
], |
96
|
|
|
'message_data' => [ |
97
|
1 |
|
'text' => $this->getContent(), |
98
|
|
|
], |
99
|
|
|
], |
100
|
|
|
], |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|