dounuo1881
dounuo1881
2016-07-11 12:35
浏览 111
已采纳

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

I am developing a FB messenger bot using PHP, but I have a query regarding button click event, how to trigger & call new card/text. Can anyone please explain with an example for button postback using PHP?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanpo6079
    duanpo6079 2016-10-17 21:15
    已采纳

    Is this what you 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'] 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.'));
    }
    
    点赞 评论
  • douhunkuang8955
    douhunkuang8955 2016-07-11 12:57

    With PHP you gonna send requests that are going to believe that your FB robot is a human.

    Normally it's javascript that generates these queries. You must send the same.

    You can do this using the CURL library and chrome devtools & Network tab.

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

    点赞 评论

相关推荐