Facebook messenger bot using PHP - Postback example? -
i developing fb messenger bot using php, have query regarding button click event, how trigger & call new card/text. can please explain example button postback using php?
is mean?
https://github.com/pimax/fb-messenger-php-example/blob/master/index.php
$data = json_decode(file_get_contents("php://input"), true, 512, json_bigint_as_string); if (!empty($data['entry'][0]['messaging'])) { foreach ($data['entry'][0]['messaging'] $message) { $command = ""; // when bot receive message user if (!empty($message['message'])) { $command = $message['message']['text']; } // when bot receive button click user else if (!empty($message['postback'])) { $command = $message['postback']['payload']; } } } // handle command switch ($command) { // when bot receive "text" case 'text': $bot->send(new message($message['sender']['id'], 'this simple text message.')); break; // when bot receive "image" case 'image': $bot->send(new imagemessage($message['sender']['id'], 'https://developers.facebook.com/images/devsite/fb4d_logo-2x.png')); break; // when bot receive "image" case 'local image': $bot->send(new imagemessage($message['sender']['id'], dirname(__file__).'/fb4d_logo-2x.png')); break; // when bot receive "profile" case 'profile': $user = $bot->userprofile($message['sender']['id']); $bot->send(new structuredmessage($message['sender']['id'], structuredmessage::type_generic, [ 'elements' => [ new messageelement($user->getfirstname()." ".$user->getlastname(), " ", $user->getpicture()) ] ] )); break; // when bot receive "button" case 'button': $bot->send(new structuredmessage($message['sender']['id'], structuredmessage::type_button, [ 'text' => 'choose category', 'buttons' => [ new messagebutton(messagebutton::type_postback, 'first button'), new messagebutton(messagebutton::type_postback, 'second button'), new messagebutton(messagebutton::type_postback, 'third button') ] ] )); break; // when bot receive "generic" case 'generic': $bot->send(new structuredmessage($message['sender']['id'], structuredmessage::type_generic, [ 'elements' => [ new messageelement("first item", "item description", "", [ new messagebutton(messagebutton::type_postback, 'first button'), new messagebutton(messagebutton::type_web, 'web link', 'http://facebook.com') ]), new messageelement("second item", "item description", "", [ new messagebutton(messagebutton::type_postback, 'first button'), new messagebutton(messagebutton::type_postback, 'second button') ]), new messageelement("third item", "item description", "", [ new messagebutton(messagebutton::type_postback, 'first button'), new messagebutton(messagebutton::type_postback, 'second button') ]) ] ] )); break; // when bot receive "receipt" case 'receipt': $bot->send(new structuredmessage($message['sender']['id'], structuredmessage::type_receipt, [ 'recipient_name' => 'fox brown', 'order_number' => rand(10000, 99999), 'currency' => 'usd', 'payment_method' => 'visa', 'order_url' => 'http://facebook.com', 'timestamp' => time(), 'elements' => [ new messagereceiptelement("first item", "item description", "", 1, 300, "usd"), new messagereceiptelement("second item", "item description", "", 2, 200, "usd"), new messagereceiptelement("third item", "item description", "", 3, 1800, "usd"), ], 'address' => new address([ 'country' => 'us', 'state' => 'ca', 'postal_code' => 94025, 'city' => 'menlo park', 'street_1' => '1 hacker way', 'street_2' => '' ]), 'summary' => new summary([ 'subtotal' => 2300, 'shipping_cost' => 150, 'total_tax' => 50, 'total_cost' => 2500, ]), 'adjustments' => [ new adjustment([ 'name' => 'new customer discount', 'amount' => 20 ]), new adjustment([ 'name' => '$10 off coupon', 'amount' => 10 ]) ] ] )); break; case 'set menu': $bot->setpersistentmenu([ new messagebutton(messagebutton::type_web, "first link", "http://yandex.ru"), new messagebutton(messagebutton::type_web, "second link", "http://google.ru") ]); break; case 'delete menu': $bot->deletepersistentmenu(); break; // other message received default: $bot->send(new message($message['sender']['id'], 'sorry. don’t understand you.')); }
Comments
Post a Comment