1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kerox\Messenger; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\ClientInterface; |
7
|
|
|
use Kerox\Messenger\Api\Code; |
8
|
|
|
use Kerox\Messenger\Api\Insights; |
9
|
|
|
use Kerox\Messenger\Api\Profile; |
10
|
|
|
use Kerox\Messenger\Api\Send; |
11
|
|
|
use Kerox\Messenger\Api\Tag; |
12
|
|
|
use Kerox\Messenger\Api\Thread; |
13
|
|
|
use Kerox\Messenger\Api\User; |
14
|
|
|
use Kerox\Messenger\Api\Webhook; |
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
16
|
|
|
|
17
|
|
|
class Messenger |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
const API_URL = 'https://graph.facebook.com/'; |
21
|
|
|
const API_VERSION = 'v2.9'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $appSecret; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $verifyToken; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $pageToken; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \GuzzleHttp\ClientInterface |
40
|
|
|
*/ |
41
|
|
|
protected $client; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Messenger constructor. |
45
|
|
|
* |
46
|
|
|
* @param string $appSecret |
47
|
|
|
* @param string $verifyToken |
48
|
|
|
* @param string $pageToken |
49
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
50
|
|
|
*/ |
51
|
1 |
|
public function __construct(string $appSecret, string $verifyToken, string $pageToken, ClientInterface $client = null) |
52
|
|
|
{ |
53
|
1 |
|
$this->appSecret = $appSecret; |
54
|
1 |
|
$this->verifyToken = $verifyToken; |
55
|
1 |
|
$this->pageToken = $pageToken; |
56
|
|
|
|
57
|
1 |
|
if ($client === null) { |
58
|
1 |
|
$client = new Client([ |
59
|
1 |
|
'base_uri' => self::API_URL . self::API_VERSION . '/', |
60
|
|
|
]); |
61
|
|
|
} |
62
|
1 |
|
$this->client = $client; |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return \Kerox\Messenger\Api\Send |
67
|
|
|
*/ |
68
|
1 |
|
public function send(): Send |
69
|
|
|
{ |
70
|
1 |
|
return Send::getInstance($this->pageToken, $this->client); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
75
|
|
|
* @return \Kerox\Messenger\Api\Webhook |
76
|
|
|
*/ |
77
|
1 |
|
public function webhook(ServerRequestInterface $request = null): Webhook |
78
|
|
|
{ |
79
|
1 |
|
return Webhook::getInstance($this->appSecret, $this->verifyToken, $this->pageToken, $this->client, $request); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return \Kerox\Messenger\Api\User |
84
|
|
|
*/ |
85
|
1 |
|
public function user(): User |
86
|
|
|
{ |
87
|
1 |
|
return User::getInstance($this->pageToken, $this->client); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return \Kerox\Messenger\Api\Profile |
92
|
|
|
*/ |
93
|
1 |
|
public function profile(): Profile |
94
|
|
|
{ |
95
|
1 |
|
return Profile::getInstance($this->pageToken, $this->client); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return \Kerox\Messenger\Api\Code |
100
|
|
|
*/ |
101
|
1 |
|
public function code(): Code |
102
|
|
|
{ |
103
|
1 |
|
return Code::getInstance($this->pageToken, $this->client); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return \Kerox\Messenger\Api\Insights |
108
|
|
|
*/ |
109
|
1 |
|
public function insights(): Insights |
110
|
|
|
{ |
111
|
1 |
|
return Insights::getInstance($this->pageToken, $this->client); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return \Kerox\Messenger\Api\Tag |
116
|
|
|
*/ |
117
|
1 |
|
public function tag(): Tag |
118
|
|
|
{ |
119
|
1 |
|
return Tag::getInstance($this->pageToken, $this->client); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|