使用 PHP 的 Facebook Messenger 机器人 - 回发示例?

Facebook messenger bot using PHP - Postback example?

我正在使用 PHP 开发一个 FB Messenger 机器人,但我有一个关于按钮点击事件的问题,如何触发和调用新的 card/text。任何人都可以使用 PHP 来解释按钮回发的示例吗?

有了 PHP,您发送的请求将相信您的 FB 机器人是人。

通常是 javascript 生成这些查询。您必须发送相同的。

您可以使用 CURL 库和 chrome 开发工具和网络选项卡来完成此操作。

http://php.net/manual/en/book.curl.php

你是这个意思吗?

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'] as $message) { 

        $command = "";

        // When bot receive message from user
        if (!empty($message['message'])) {
             $command = $message['message']['text'];           
        }
         // When bot receive button click from 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 is a 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' => ' 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. I don’t understand you.'));
}