1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Kerox\Messenger; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\HandlerStack; |
8
|
|
|
use Kerox\Messenger\Http\Client; |
9
|
|
|
use Kerox\Messenger\Http\Middleware; |
10
|
|
|
use Psr\Http\Client\ClientInterface; |
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
12
|
|
|
|
13
|
|
|
class Messenger |
14
|
|
|
{ |
15
|
|
|
public const API_URL = 'https://graph.facebook.com/'; |
16
|
|
|
public const API_VERSION = 'v3.1'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $appSecret; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $verifyToken; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $pageToken; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ClientInterface |
35
|
|
|
*/ |
36
|
|
|
protected $client; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Messenger constructor. |
40
|
|
|
* |
41
|
|
|
* @param string $appSecret |
42
|
|
|
* @param string $verifyToken |
43
|
|
|
* @param string $pageToken |
44
|
|
|
* @param string $apiVersion |
45
|
|
|
* @param ClientInterface $client |
46
|
|
|
*/ |
47
|
1 |
|
public function __construct( |
48
|
|
|
string $appSecret, |
49
|
|
|
string $verifyToken, |
50
|
|
|
string $pageToken, |
51
|
|
|
string $apiVersion = self::API_VERSION, |
52
|
|
|
?ClientInterface $client = null |
53
|
|
|
) { |
54
|
1 |
|
$this->appSecret = $appSecret; |
55
|
1 |
|
$this->verifyToken = $verifyToken; |
56
|
1 |
|
$this->pageToken = $pageToken; |
57
|
|
|
|
58
|
1 |
|
if ($client === null) { |
59
|
1 |
|
$client = $this->createClient($apiVersion); |
60
|
|
|
} |
61
|
1 |
|
$this->client = $client; |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $apiVersion |
66
|
|
|
* |
67
|
|
|
* @return ClientInterface |
68
|
|
|
*/ |
69
|
1 |
|
private function createClient(string $apiVersion): ClientInterface |
70
|
|
|
{ |
71
|
1 |
|
$stack = HandlerStack::create(); |
72
|
1 |
|
$stack->push(Middleware::queryParam('access_token', $this->pageToken)); |
73
|
|
|
|
74
|
1 |
|
return new Client([ |
75
|
1 |
|
'base_uri' => self::API_URL . $apiVersion . '/', |
76
|
|
|
'headers' => [ |
77
|
|
|
'Accept' => 'application/json', |
78
|
|
|
'Content-Type' => 'application/json', |
79
|
|
|
], |
80
|
1 |
|
'handler' => $stack, |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return Api\Send |
86
|
|
|
*/ |
87
|
1 |
|
public function send(): Api\Send |
88
|
|
|
{ |
89
|
1 |
|
return new Api\Send($this->client); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param ServerRequestInterface|null $request |
94
|
|
|
* |
95
|
|
|
* @return Api\Webhook |
96
|
|
|
*/ |
97
|
1 |
|
public function webhook(?ServerRequestInterface $request = null): Api\Webhook |
98
|
|
|
{ |
99
|
1 |
|
return new Api\Webhook($this->appSecret, $this->verifyToken, $this->client, $request); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return Api\User |
104
|
|
|
*/ |
105
|
1 |
|
public function user(): Api\User |
106
|
|
|
{ |
107
|
1 |
|
return new Api\User($this->client); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return Api\Profile |
112
|
|
|
*/ |
113
|
1 |
|
public function profile(): Api\Profile |
114
|
|
|
{ |
115
|
1 |
|
return new Api\Profile($this->client); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return Api\Code |
120
|
|
|
*/ |
121
|
1 |
|
public function code(): Api\Code |
122
|
|
|
{ |
123
|
1 |
|
return new Api\Code($this->client); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return Api\Insights |
128
|
|
|
*/ |
129
|
1 |
|
public function insights(): Api\Insights |
130
|
|
|
{ |
131
|
1 |
|
return new Api\Insights($this->client); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return Api\Tag |
136
|
|
|
*/ |
137
|
1 |
|
public function tag(): Api\Tag |
138
|
|
|
{ |
139
|
1 |
|
return new Api\Tag($this->client); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return Api\Thread |
144
|
|
|
*/ |
145
|
1 |
|
public function thread(): Api\Thread |
146
|
|
|
{ |
147
|
1 |
|
return new Api\Thread($this->client); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return Api\Nlp |
152
|
|
|
*/ |
153
|
1 |
|
public function nlp(): Api\Nlp |
154
|
|
|
{ |
155
|
1 |
|
return new Api\Nlp($this->client); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return Api\Broadcast |
160
|
|
|
*/ |
161
|
1 |
|
public function broadcast(): Api\Broadcast |
162
|
|
|
{ |
163
|
1 |
|
return new Api\Broadcast($this->client); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return Api\Persona |
168
|
|
|
*/ |
169
|
1 |
|
public function persona(): Api\Persona |
170
|
|
|
{ |
171
|
1 |
|
return new Api\Persona($this->client); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|