Skip to content

Commit

Permalink
Merge pull request #66 from ker0x/feature/issue-61
Browse files Browse the repository at this point in the history
[feature #61] Add channel standby for Entry callback
  • Loading branch information
ker0x authored Aug 28, 2017
2 parents 3648297 + cdd4fda commit fc8bc77
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Model/Callback/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

class Entry
{
const CHANNELS = [
'messaging',
'standby'
];

/**
* @var string
*/
Expand Down Expand Up @@ -67,8 +72,13 @@ public function getEvents(): array
public static function create(array $entry): Entry
{
$events = [];
foreach ($entry['messaging'] as $event) {
$events[] = EventFactory::create($event);

foreach (self::CHANNELS as $channel) {
if (isset($entry[$channel])) {
foreach ($entry[$channel] as $event) {
$events[] = EventFactory::create($event);
}
}
}

return new static($entry['id'], $entry['time'], $events);
Expand Down
28 changes: 28 additions & 0 deletions tests/Mocks/Callback/stand_by.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"object": "page",
"entry": [
{
"id": "PAGE_ID",
"time": 1458692752478,
"standby": [
{
"sender": {
"id": "USER_ID"
},
"recipient": {
"id": "PAGE_ID"
},
"timestamp": 1458692752478,
"message": {
"mid": "mid.1457764197618:41d102a3e1ae206a38",
"seq": 73,
"text": "hello, world!",
"quick_reply": {
"payload": "DEVELOPER_DEFINED_PAYLOAD"
}
}
}
]
}
]
}
33 changes: 33 additions & 0 deletions tests/TestCase/Api/WebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,39 @@ public function testGetCallbackEvents()
$this->assertEquals([$event], $events);
}

public function testStandbyEntry()
{
$appSecret = 'app_secret';
$verifyToken = 'verify_token';
$pageToken = 'page_token';


$requestBody = file_get_contents(__DIR__ . '/../../Mocks/Callback/stand_by.json');
$requestHeaders = [
'Content-Type' => 'application/json',
'X-Hub-Signature' => 'sha1=' . hash_hmac('sha1', $requestBody, $appSecret)
];

$request = new ServerRequest('POST', '/app.php/facebook/webhook', $requestHeaders, $requestBody);

$bodyResponse = file_get_contents(__DIR__ . '/../../Mocks/Response/Webhook/success.json');
$mockedResponse = new MockHandler([
new Response(200, [], $bodyResponse),
]);

$handler = HandlerStack::create($mockedResponse);
$client = new Client([
'handler' => $handler
]);

$event = new MessageEvent('USER_ID', 'PAGE_ID', 1458692752478, new Message('mid.1457764197618:41d102a3e1ae206a38', 73, 'hello, world!', 'DEVELOPER_DEFINED_PAYLOAD'));

$webhookApi = new Webhook($appSecret, $verifyToken, $pageToken, $client, $request);
$events = $webhookApi->getCallbackEvents();

$this->assertEquals([$event], $events);
}

public function tearDown()
{
unset($this->webhookApi);
Expand Down

0 comments on commit fc8bc77

Please sign in to comment.