-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5b49481
Showing
18 changed files
with
1,173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/nbproject/private |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
|
||
namespace fritak; | ||
|
||
use fritak\MessengerPlatform\Gate; | ||
use fritak\MessengerPlatform\Config; | ||
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::SUBSCRIBED_APPS); | ||
} | ||
|
||
/** | ||
* Send a simple text message. | ||
* | ||
* @param int $recipientId This must be an id that was retrieved through the Messenger entry points or through the Messenger callbacks. | ||
* @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($recipientId, $textMessage, $notificationType = MessageSend::NOTIFICATION_TYPE_REGULAR) | ||
{ | ||
$message = new MessageSend($recipientId, $textMessage, $notificationType); | ||
|
||
return $this->gate->request(Gate::URL_MESSAGES, $message->getDataForCall()); | ||
} | ||
|
||
/** | ||
* Send an image (file). | ||
* | ||
* @param int $recipientId This must be an id that was retrieved through the Messenger entry points or through the Messenger callbacks. | ||
* @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($recipientId, $url, $notificationType = MessageSend::NOTIFICATION_TYPE_REGULAR) | ||
{ | ||
$structuredMessage = new StructuredMessage($recipientId, ['url' => $url], $notificationType, StructuredMessage::ATTACHMENT_TYPE_IMAGE); | ||
|
||
return $this->gate->request(Gate::URL_MESSAGES, $structuredMessage->getDataForCall()); | ||
} | ||
|
||
/** | ||
* Send a structured Message - button template. | ||
* | ||
* @param int $recipientId This must be an id that was retrieved through the Messenger entry points or through the Messenger callbacks. | ||
* @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($recipientId, $text, $buttons, $notificationType = MessageSend::NOTIFICATION_TYPE_REGULAR) | ||
{ | ||
$structuredMessage = new StructuredMessage($recipientId, | ||
['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 $recipientId This must be an id that was retrieved through the Messenger entry points or through the Messenger callbacks. | ||
* @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($recipientId, Receipt $receipt, $notificationType = MessageSend::NOTIFICATION_TYPE_REGULAR) | ||
{ | ||
$structuredMessage = new StructuredMessage($recipientId, | ||
$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()); | ||
} | ||
|
||
/** | ||
* 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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "fritak/MessengerPlatform", | ||
"description": "Facebook Messenger Platform PHP API for bots.", | ||
"homepage": "https://github.com/fritak/messengerPlatform", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Marek Sušický", | ||
"email": "[email protected]", | ||
"homepage": "http://www.fritak.eu", | ||
"role": "Developer" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.4.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"fritak\\": "" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
use fritak\MessengerPlatform\Button; | ||
use fritak\MessengerPlatform\Element; | ||
use fritak\MessengerPlatform\MessageSend; | ||
use fritak\MessengerPlatform\StructuredMessage; | ||
|
||
|
||
use fritak\MessengerPlatform\ReceiptElement; | ||
use fritak\MessengerPlatform\Receipt; | ||
use fritak\MessengerPlatform\Summary; | ||
use fritak\MessengerPlatform\Address; | ||
use fritak\MessengerPlatform\Adjustment; | ||
|
||
require_once(dirname(__FILE__) . '/vendor/autoload.php'); | ||
|
||
$userToSendMessage = 123; // This must be an id that was retrieved through the Messenger entry points or through the Messenger callbacks. | ||
|
||
// This is just an example, this method of getting request is not safe! | ||
$stream = file_get_contents("php://input"); | ||
$request = empty($stream)? $_REQUEST : $stream; | ||
|
||
$bot = new \fritak\MessengerPlatform( | ||
['accessToken' => 'CAAWiukRglNoBAJN4D0pUwKZCe3ZBOmwZA6ACwuDs4YkZAOsFyc5lVZBxcU2trTlNVteRy3T2ZANlyZAUn5znRwHKqFv0dmp8KOvs8mdOZCSH0cvH1dOXSR8xl9gZCU2GWwOQzBsnZCBJEhQaaaTvwHEIsUZAdOZCwXoPkrZCQSmzBkPyj5RWjLEQA8ZCZCuEPUpMgrbYcfP4wYyZCPR3XgZDZD', | ||
'webhookToken' => 'my_secret_token', | ||
'facebookApiUrl' => 'https://graph.facebook.com/v2.6/me/' | ||
], $request); | ||
|
||
// Check if request is subscribe and then return challenge (see https://developers.facebook.com/docs/messenger-platform/implementation#setup_webhook) | ||
if($bot->checkSubscribe()) | ||
{ | ||
print $bot->request->getChallenge(); | ||
exit; | ||
} | ||
|
||
// Subscribe the App to a Page. Messenger documentation: In order for your webhook to receive events for a specific page, you must subscribe your app to the page. | ||
$bot->subscribe(); | ||
|
||
|
||
// Messenger is calling your URL, someone is sending a message... | ||
$bot->getMessagesReceived(); | ||
|
||
// Simple sending messages: | ||
|
||
// Send a simple text message. | ||
$bot->sendMessage($userToSendMessage, 'Example!'); | ||
|
||
// Send an image (file). | ||
$bot->sendImage($userToSendMessage, 'http://placehold.it/150x150'); | ||
|
||
// Send a structured Message - button template. | ||
$buttons = [new Button('Click', Button::TYPE_WEB, 'example.com'), new Button('Click2', Button::TYPE_POSTBACK, 'example.com')]; | ||
$bot->sendButton($userToSendMessage, 'Example text... Not too long, hehe.', $buttons); | ||
|
||
|
||
// Send a structured Message - receipt template. | ||
$elements = [new ReceiptElement(['title' => 'Panda', 'price' => 9.99]), new ReceiptElement(['title' => 'Bunny', 'price' => 9.99])]; | ||
$summary = new Summary(['total_cost' => 17.98]); | ||
$address = new Address(['street_1' => 'Queens 1', 'city' => 'Example city', 'postal_code' => '10000', 'state' => 'DO', 'country' => 'CZ']); | ||
$receipt = new Receipt('User', Rand(1,9999), 'USD', 'card', $elements, $summary, $address, $adjustments, time(), 'example.com'); | ||
$adjustments = [new Adjustment(['name' => 'Discount', 'amount' => 2])]; | ||
|
||
$bot->sendReceipt($userToSendMessage, $receipt); | ||
|
||
// You can send it with StructuredMessage: | ||
|
||
$bot->sendComplexMessage(new StructuredMessage($userToSendMessage, | ||
['url' => 'http://placehold.it/150x150'], | ||
MessageSend::NOTIFICATION_TYPE_SILENT_PUSH, | ||
StructuredMessage::ATTACHMENT_TYPE_IMAGE)); | ||
|
||
$bot->sendComplexMeesage(new StructuredMessage($userToSendMessage, | ||
[new Element('Example.', 'Example...', 'http://placehold.it/150x150', 'http://placehold.it/150x150', [new Button('Click', Button::TYPE_WEB, 'example.com')])], | ||
MessageSend::NOTIFICATION_TYPE_SILENT_PUSH, | ||
StructuredMessage::ATTACHMENT_TYPE_TEMPLATE, | ||
StructuredMessage::TEMPLATE_PAYLOAD_TYPE_GENERIC)); | ||
|
||
$bot->sendComplexMeesage(new StructuredMessage($userToSendMessage, | ||
['text' => 'Example text... Not too long, hehe.', | ||
'buttons' => [new Button('Click', Button::TYPE_WEB, 'example.com'), new Button('Click2', Button::TYPE_POSTBACK, 'example.com')]], | ||
MessageSend::NOTIFICATION_TYPE_SILENT_PUSH, | ||
StructuredMessage::ATTACHMENT_TYPE_TEMPLATE, | ||
StructuredMessage::TEMPLATE_PAYLOAD_TYPE_BUTTON)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
include.path=${php.global.include.path} | ||
php.version=PHP_54 | ||
source.encoding=UTF-8 | ||
src.dir=. | ||
tags.asp=false | ||
tags.short=false | ||
web.root=. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://www.netbeans.org/ns/project/1"> | ||
<type>org.netbeans.modules.php.project</type> | ||
<configuration> | ||
<data xmlns="http://www.netbeans.org/ns/php-project/1"> | ||
<name>messenger-platform</name> | ||
</data> | ||
</configuration> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
Facebook Messenger Platform PHP API for bots. | ||
======================== | ||
|
||
This code is an implementation of the Messenger Platform in a PHP. | ||
|
||
INSTALLATION | ||
------------ | ||
|
||
``` | ||
composer require "fritak/MessengerPlatform" | ||
``` | ||
|
||
REQUIREMENTS | ||
------------ | ||
The minimum requirement is PHP 5.4 on your Web Server. | ||
|
||
|
||
## SETUP | ||
```php | ||
$userToSendMessage = $userToSendMessage; // now just and ID, phone matching will come in the future | ||
|
||
// This is just an example, this method of getting request is not safe! | ||
$stream = file_get_contents("php://input"); | ||
$request = empty($stream)? $_REQUEST : $stream; | ||
|
||
$bot = new \fritak\MessengerPlatform( | ||
['accessToken' => 'token_for_app', | ||
'webhookToken' => 'my_secret_token', | ||
'facebookApiUrl' => 'https://graph.facebook.com/v2.6/me/' //2.6 is minimum | ||
], $request); | ||
``` | ||
|
||
## BASIC USAGE | ||
```php | ||
// Check if request is subscribe and then return challenge - [documentation](https://developers.facebook.com/docs/messenger-platform/implementation#setup_webhook) | ||
if($bot->checkSubscribe()) | ||
{ | ||
print $bot->request->getChallenge(); | ||
exit; | ||
} | ||
|
||
// Subscribe the App to a Page. In order for your webhook to receive events for a specific page, you must subscribe your app to the page. | ||
$bot->subscribe(); | ||
``` | ||
|
||
## GETTING MESSAGES | ||
```php | ||
// Messenger is calling your URL, someone is sending a message... | ||
$messages = $bot->getMessagesReceived(); | ||
``` | ||
|
||
## SENDING MESSAGES | ||
```php | ||
// Send a simple text message. | ||
$bot->sendMessage($userToSendMessage, 'Example!'); | ||
|
||
// Send an image (file). | ||
$bot->sendImage($userToSendMessage, 'http://placehold.it/150x150'); | ||
|
||
// Send a structured Message - button template. | ||
$buttons = [new Button('Click', Button::TYPE_WEB, 'example.com'), new Button('Click2', Button::TYPE_POSTBACK, 'example.com')]; | ||
$bot->sendButton($userToSendMessage, 'Example text... Not too long, hehe.', $buttons); | ||
|
||
|
||
// Send a structured Message - receipt template. | ||
$elements = [new ReceiptElement(['title' => 'Panda', 'price' => 9.99]), new ReceiptElement(['title' => 'Bunny', 'price' => 9.99])]; | ||
$summary = new Summary(['total_cost' => 17.98]); | ||
$address = new Address(['street_1' => 'Queens 1', 'city' => 'Example city', 'postal_code' => '10000', 'state' => 'DO', 'country' => 'CZ']); | ||
$receipt = new Receipt('User', Rand(1,9999), 'USD', 'card', $elements, $summary, $address, $adjustments, time(), 'example.com'); | ||
$adjustments = [new Adjustment(['name' => 'Discount', 'amount' => 2])]; | ||
|
||
$bot->sendReceipt($userToSendMessage, $receipt); | ||
``` | ||
|
||
## SENDING MESSAGES WITH StructuredMessage | ||
```php | ||
$bot->sendComplexMessage(new StructuredMessage($userToSendMessage, | ||
['url' => 'http://placehold.it/150x150'], | ||
MessageSend::NOTIFICATION_TYPE_SILENT_PUSH, | ||
StructuredMessage::ATTACHMENT_TYPE_IMAGE)); | ||
|
||
$bot->sendComplexMeesage(new StructuredMessage($userToSendMessage, | ||
[new Element('Example.', 'Example...', 'http://placehold.it/150x150', 'http://placehold.it/150x150', [new Button('Click', Button::TYPE_WEB, 'example.com')])], | ||
MessageSend::NOTIFICATION_TYPE_SILENT_PUSH, | ||
StructuredMessage::ATTACHMENT_TYPE_TEMPLATE, | ||
StructuredMessage::TEMPLATE_PAYLOAD_TYPE_GENERIC)); | ||
|
||
$bot->sendComplexMeesage(new StructuredMessage($userToSendMessage, | ||
['text' => 'Example text... Not too long, hehe.', | ||
'buttons' => [new Button('Click', Button::TYPE_WEB, 'example.com'), new Button('Click2', Button::TYPE_POSTBACK, 'example.com')]], | ||
MessageSend::NOTIFICATION_TYPE_SILENT_PUSH, | ||
StructuredMessage::ATTACHMENT_TYPE_TEMPLATE, | ||
StructuredMessage::TEMPLATE_PAYLOAD_TYPE_BUTTON)); | ||
``` |
Oops, something went wrong.