forked from pimax/fb-messenger-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFbBotApp.php
More file actions
97 lines (82 loc) · 2.08 KB
/
FbBotApp.php
File metadata and controls
97 lines (82 loc) · 2.08 KB
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
<?php
namespace pimax;
class FbBotApp
{
/**
* Request type GET
*/
const TYPE_GET = "get";
/**
* Request type POST
*/
const TYPE_POST = "post";
/**
* FB Messenger API Url
*
* @var string
*/
protected $apiUrl = 'https://graph.facebook.com/v2.6/';
/**
* BOT username
*
* @var string|null
*/
protected $token = null;
public function __construct($token)
{
$this->token = $token;
}
/**
* Send Message
*
* @param Message $message
* @return mixed
*/
public function send($message)
{
return $this->call('me/messages', $message->getData());
}
/**
* Get User Profile Info
*
* @param $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));
}
/**
* Request to API
*
* @param $url Url
* @param $data Data
* @param string $type Type of request (GET|POST)
* @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) {
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, http_build_query($data));
}
curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($process);
curl_close($process);
return json_decode($return, true);
}
}