forked from pimax/fb-messenger-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FbBotApp.php
149 lines (125 loc) · 3.5 KB
/
FbBotApp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace pimax;
use pimax\Messages\Message;
use pimax\Messages\MessageButton;
class FbBotApp
{
/**
* Request type GET
*/
const TYPE_GET = "get";
/**
* Request type POST
*/
const TYPE_POST = "post";
/**
* Request type DELETE
*/
const TYPE_DELETE = "delete";
/**
* FB Messenger API Url
*
* @var string
*/
protected $apiUrl = 'https://graph.facebook.com/v2.6/';
/**
* @var null|string
*/
protected $token = null;
/**
* FbBotApp constructor.
* @param string $token
*/
public function __construct($token)
{
$this->token = $token;
}
/**
* Send Message
*
* @param Message $message
* @return array
*/
public function send($message)
{ $messageResponse= $this->call('me/messages', $message->getData());
new HandleMessageResponse($message, $messageResponse) ;
}
/**
* Get User Profile Info
*
* @param int $id
* @param string $fields
* @return UserProfile
*/
public function userProfile($id, $fields = 'first_name,last_name,profile_pic,locale,timezone,gender')
{
return new UserProfile($this->call($id, [
'fields' => $fields
], self::TYPE_GET));
}
/**
* Set Persistent Menu
*
* @see https://developers.facebook.com/docs/messenger-platform/thread-settings/persistent-menu
* @param MessageButton[] $buttons
* @return array
*/
public function setPersistentMenu($buttons)
{
$elements = [];
foreach ($buttons as $btn) {
$elements[] = $btn->getData();
}
return $this->call('me/thread_settings', [
'setting_type' => 'call_to_actions',
'thread_state' => 'existing_thread',
'call_to_actions' => $elements
], self::TYPE_POST);
}
/**
* Remove Persistent Menu
*
* @see https://developers.facebook.com/docs/messenger-platform/thread-settings/persistent-menu
* @return array
*/
public function deletePersistentMenu()
{
return $this->call('me/thread_settings', [
'setting_type' => 'call_to_actions',
'thread_state' => 'existing_thread'
], self::TYPE_DELETE);
}
/**
* Request to API
*
* @param string $url
* @param array $data
* @param string $type Type of request (GET|POST|DELETE)
* @return array
*/
protected function call($url, $data, $type = self::TYPE_POST)
{
$data['access_token'] = $this->token;
$headers = [
'Content-Type: application/json',
];
if ($type == self::TYPE_GET) {
$url .= '?'.http_build_query($data);
}
$process = curl_init($this->apiUrl.$url);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, false);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
if($type == self::TYPE_POST || $type == self::TYPE_DELETE) {
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, http_build_query($data));
}
if ($type == self::TYPE_DELETE) {
curl_setopt($process, CURLOPT_CUSTOMREQUEST, "DELETE");
}
curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($process);
curl_close($process);
return json_decode($return, true);
}
}