forked from Fritak/messenger-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessengerPlatform.php
195 lines (172 loc) · 7.48 KB
/
MessengerPlatform.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace fritak;
use fritak\MessengerPlatform\Gate;
use fritak\MessengerPlatform\Config;
use fritak\MessengerPlatform\MessageSendWithReplies;
use fritak\MessengerPlatform\Receipt;
use fritak\MessengerPlatform\MessageSend;
use fritak\MessengerPlatform\StructuredMessage;
/**
* Class for Messenger Platform API.
*
* @package fritak\MessengerPlatform
* @see https://developers.facebook.com/docs/messenger-platform/implementation
*/
class MessengerPlatform
{
/** @var fritak\MessengerPlatform\Config Config for application */
public $config;
/** @var fritak\MessengerPlatform\ParseRequest Parse request. */
public $request;
/** @var fritak\MessengerPlatform\Gate Gate for API. */
protected $gate = NULL;
public function __construct($config, $request = [])
{
$this->loadConfig($config);
$this->gate = new Gate($this->config);
$this->request = new \fritak\MessengerPlatform\ParseRequest($request);
}
/**
* You can load another config later on.
*
* @param array $config
*/
public function loadConfig($config)
{
$this->config = new Config($config);
}
/**
* In order for your webhook to receive events for a specific page, you must subscribe your app to the page.
*
* @return fritak\MessengerPlatform\Response Response.
* @see https://developers.facebook.com/docs/messenger-platform/implementation#subscribe_app_pages
*/
public function subscribe()
{
return $this->gate->request(Gate::URL_SUBSCRIBED_APPS);
}
/**
* Send a simple text message.
*
* @param int $recipient This must be either:
* <ul>
* <li>an id that was retrieved through the Messenger entry points or through the Messenger callbacks</li>
* <li>or instance of fritak\MessengerPlatform\UserRecipient</li>
* </ul>
* @param string $textMessage Text.
* @param string $notificationType One of 3 types of notification, use constants (eg. NOTIFICATION_TYPE_REGULAR)
* @return fritak\MessengerPlatform\Response
* @see https://developers.facebook.com/docs/messenger-platform/send-api-reference#guidelines
*/
public function sendMessage($recipient, $textMessage, $notificationType = MessageSend::NOTIFICATION_TYPE_REGULAR)
{
$message = new MessageSend($recipient, $textMessage, $notificationType);
return $this->gate->request(Gate::URL_MESSAGES, $message->getDataForCall());
}
public function sendQuickReplies($recipient, $text, $data, $notificationType = MessageSend::NOTIFICATION_TYPE_REGULAR)
{
$message = new MessageSendWithReplies($recipient, $text, $data, $notificationType);
return $this->gate->request(Gate::URL_MESSAGES, $message->getDataForCall());
}
/**
* Send an image (file).
*
* @param int $recipient This must be either:
* <ul>
* <li>an id that was retrieved through the Messenger entry points or through the Messenger callbacks</li>
* <li>or instance of fritak\MessengerPlatform\UserRecipient</li>
* </ul>
* @param string $url Image URL.
* @param string $notificationType One of 3 types of notification, use constants (eg. NOTIFICATION_TYPE_REGULAR)
* @return fritak\MessengerPlatform\Response
* @see https://developers.facebook.com/docs/messenger-platform/send-api-reference#guidelines
*/
public function sendImage($recipient, $url, $notificationType = MessageSend::NOTIFICATION_TYPE_REGULAR)
{
$structuredMessage = new StructuredMessage($recipient, ['url' => $url], $notificationType, StructuredMessage::ATTACHMENT_TYPE_IMAGE);
return $this->gate->request(Gate::URL_MESSAGES, $structuredMessage->getDataForCall());
}
/**
* Send a structured Message - button template.
*
* @param int $recipient This must be either:
* <ul>
* <li>an id that was retrieved through the Messenger entry points or through the Messenger callbacks</li>
* <li>or instance of fritak\MessengerPlatform\UserRecipient</li>
* </ul>
* @param string $text Text.
* @param array $buttons Array of fritak\MessengerPlatform\Button.
* @param string $notificationType One of 3 types of notification, use constants (eg. NOTIFICATION_TYPE_REGULAR)
* @return fritak\MessengerPlatform\Response
* @see https://developers.facebook.com/docs/messenger-platform/send-api-reference#guidelines
*/
public function sendButton($recipient, $text, $buttons, $notificationType = MessageSend::NOTIFICATION_TYPE_REGULAR)
{
$structuredMessage = new StructuredMessage($recipient,
['text' => $text, 'buttons' => $buttons],
$notificationType,
StructuredMessage::ATTACHMENT_TYPE_TEMPLATE,
StructuredMessage::TEMPLATE_PAYLOAD_TYPE_BUTTON);
return $this->gate->request(Gate::URL_MESSAGES, $structuredMessage->getDataForCall());
}
/**
* Send a structured Message - receipt template.
*
* @param int $recipient This must be either:
* <ul>
* <li>an id that was retrieved through the Messenger entry points or through the Messenger callbacks</li>
* <li>or instance of fritak\MessengerPlatform\UserRecipient</li>
* </ul>
* @param Receipt $receipt
* @param string $notificationType One of 3 types of notification, use constants (eg. NOTIFICATION_TYPE_REGULAR)
* @return fritak\MessengerPlatform\Response
* @see https://developers.facebook.com/docs/messenger-platform/send-api-reference#guidelines
*/
public function sendReceipt($recipient, Receipt $receipt, $notificationType = MessageSend::NOTIFICATION_TYPE_REGULAR)
{
$structuredMessage = new StructuredMessage($recipient,
$receipt,
$notificationType,
StructuredMessage::ATTACHMENT_TYPE_TEMPLATE,
StructuredMessage::TEMPLATE_PAYLOAD_TYPE_RECEIPT);
return $this->gate->request(Gate::URL_MESSAGES, $structuredMessage->getDataForCall());
}
/**
* Send a complex message.
*
* @param fritak\MessengerPlatform\MessageSend|fritak\MessengerPlatform\StructuredMessage $message
* @return fritak\MessengerPlatform\Response
*/
public function sendComplexMeesage($message)
{
return $this->gate->request(Gate::URL_MESSAGES, $message->getDataForCall());
}
/**
* Send a complex message.
*
* @param fritak\MessengerPlatform\MessageSend|fritak\MessengerPlatform\StructuredMessage $message
* @return fritak\MessengerPlatform\Response
*/
public function sendComplexMessage($message)
{
return $this->sendComplexMeesage($message);
}
/**
* Get messages received. Returns FALSE if request don`t have messages.
*
* @return boolean|array Array of \fritak\MessengerPlatform\MessageReceived.
*/
public function getMessagesReceived()
{
return $this->request->getMessagesReceived();
}
/**
* Check if request is subscribe.
*
* @return boolean Is request subscribe?
*/
public function checkSubscribe()
{
return $this->request->checkSubscribe($this->config->webhookToken);
}
}