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

时间:2016-07-11 12:35:04

标签: php bots facebook-messenger messenger

我正在使用PHP开发一个FB messenger机器人,但我有一个关于按钮点击事件的查询,如何触发&打电话给新卡/文字。任何人都可以用PHP解释一下按钮回发的例子吗?

2 个答案:

答案 0 :(得分:4)

这是你的意思吗?

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' => '$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. I don’t understand you.'));
}

答案 1 :(得分:1)

使用PHP,您将发送相信您的FB机器人是人类的请求。

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

您可以使用CURL库和chrome devtools&网络标签。

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