Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"image" text not responding with image attachment #27

Closed
ekundayo-ab opened this issue Apr 10, 2017 · 22 comments
Closed

"image" text not responding with image attachment #27

ekundayo-ab opened this issue Apr 10, 2017 · 22 comments

Comments

@ekundayo-ab
Copy link

After typing "image" in the chat box, Bot does not respond.

@andreshg112
Copy link

andreshg112 commented Apr 10, 2017

I have the same error.
The answer from the server that I got is:

{
    "error": {
        "message": "(#100) Invalid data",
        "type": "OAuthException",
        "code": 100,
        "error_subcode": 2018032,
        "fbtrace_id": "BDf3+Fhu7Kj"
    }
}

Error codes - Messenger Platform
I'm seeing the documentation but I don't find the corresponding error_subcode (2018032) in there.

@wittfabian
Copy link
Contributor

@ekundayo-ab Any error messages?

@ekundayo-ab
Copy link
Author

@wittfabian I got a 200 OK Response, but an undefined in response served.


Notice: Undefined index: is_echo in /var/www/html/solaso/index.php on line 63

@ekundayo-ab
Copy link
Author

@andreshg112 what tool do you use, to see server response?

@wittfabian
Copy link
Contributor

@ekundayo-ab is the index file the original one from this repo?

@ekundayo-ab
Copy link
Author

ekundayo-ab commented Apr 10, 2017 via email

@ekundayo-ab
Copy link
Author

ekundayo-ab commented Apr 10, 2017

@wittfabian here:

<?php
$verify_token = ""; // Verify token
$token = ""; // Page token

if (file_exists(__DIR__.'/config.php')) {
    $config = include __DIR__.'/config.php';
    $verify_token = $config['verify_token'];
    $token = $config['token'];
}

require_once(dirname(__FILE__) . '/vendor/autoload.php');

use solaso\FbBotApp;
use solaso\Messages\Message;
use solaso\Messages\ImageMessage;
use solaso\UserProfile;
use solaso\Messages\MessageButton;
use solaso\Messages\StructuredMessage;
use solaso\Messages\MessageElement;
use solaso\Messages\MessageReceiptElement;
use solaso\Messages\Address;
use solaso\Messages\Summary;
use solaso\Messages\Adjustment;
use solaso\Messages\AccountLink;

// Make Bot Instance
$bot = new FbBotApp($token);

if (!empty($_REQUEST['local'])) {

    $message = new ImageMessage(1585388421775947, dirname(__FILE__).'/fb4d_logo-2x.png');

    $message_data = $message->getData();
    $message_data['message']['attachment']['payload']['url'] = 'fb4d_logo-2x.png';

        echo '<pre>', print_r($message->getData()), '</pre>';

    $res = $bot->send($message);

    echo '<pre>', print_r($res), '</pre>';
}

// Receive something
if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] == 'subscribe' && $_REQUEST['hub_verify_token'] == $verify_token) {

    // Webhook setup request
    echo $_REQUEST['hub_challenge'];
} else {

    // Other event

    $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) {

            // Skipping delivery messages
            if (!empty($message['delivery'])) {
                continue;
            }
            
            
            // skip the echo of my own messages
            if (($message['message']['is_echo'] == "true")) {
                continue;
            }
            

            $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;
                
                case 'login':
                    $bot->send(new StructuredMessage($message['sender']['id'],
                        StructuredMessage::TYPE_GENERIC,
                        [
                            'elements' => [
                                new AccountLink(
                                    'Welcome to Bank',
                                    'To be sure, everything is safe, you have to login to your administration.',
                                    'https://www.example.com/oauth/authorize',
                                    'https://www.facebook.com/images/fb_icon_325x325.png')
                            ]
                        ]
                    ));
                break;
            
                case 'logout':
                    $bot->send(new StructuredMessage($message['sender']['id'],
                        StructuredMessage::TYPE_GENERIC,
                        [
                            'elements' => [
                                new AccountLink(
                                    'Welcome to Bank',
                                    'To be sure, everything is safe, you have to login to your administration.',
                                    '',
                                    'https://www.facebook.com/images/fb_icon_325x325.png',
                                    TRUE)
                            ]
                        ]
                    ));
                break;

                // Other message received
                default:
                    if (!empty($command)) // otherwise "empty message" wont be understood either
                    //$bot->send(new Message($message['sender']['id'], 'Sorry. I don’t understand you.'));
                    $bot->send(new Message($message['sender']['id'], $message['message']['text']));
            }
        }
    }
}
?>

@wittfabian
Copy link
Contributor

@andreshg112 "error codes" are not the best part of the facebook docs

@ekundayo-ab
Copy link
Author

@wittfabian the sample url seems to be the problem.
https://developers.facebook.com/images/devsite/fb4d_logo-2x.png
Is there a way it can be encoded before posting to open graph?

@ekundayo-ab
Copy link
Author

ekundayo-ab commented Apr 10, 2017

@wittfabian @andreshg112 I tested with another image link and it worked.

http://res.cloudinary.com/gememerald/image/upload/v1485745856/facebook-announces-clickable-hashtags--resolution-media-17_cckv7m.png

I'll recommend the sample url changed or encoded.

@ekundayo-ab ekundayo-ab changed the title "image" text not responding with image "image" text not responding with image attachment Apr 10, 2017
@andreshg112
Copy link

@ekundayo-ab, I use ngrok, and I add return before sending the message:

return $this->bot->send(
    new ImageMessage(
        $message['sender']['id'], 'https://developers.facebook.com/images/devsite/fb4d_logo-2x.png'
    )
);

@andreshg112
Copy link

@ekundayo-ab, It's working using the image link you posted above.

@nbaghiro
Copy link

I think the problem is $command never gets set to "Image" so it never tries to send image back.

`$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'];
        }`

@wittfabian
Copy link
Contributor

if you send "image" via the facebook messenger, the first part of the if statement is triggered

@nbaghiro
Copy link

I am wrong then, sorry I have not tried sending image yet so I just guessed maybe that is the issue

@nbaghiro
Copy link

nbaghiro commented Apr 20, 2017

@wittfabian I think you are wrong actually, I agree that it will go to first IF block but for images there won't be any "text" field inside message object. I just sent an image to my bot and the response is as following:

{ "object": "page", "entry": [ { "id": "948228081946815", "time": 1492695965342, "messaging": [ { "sender": { "id": "1084062888365040" }, "recipient": { "id": "948228081946815" }, "timestamp": 1492695965149, "message": { "mid": "mid.$cAAMEm6ehgyxhvOIl3Vbi5zbu0LLz", "seq": 302620, "attachments": [ { "type": "image", "payload": { "url": "https://scontent.xx.fbcdn.net/v/t34.0-12/18009151_1502125549811905_2134570616_n.png?_nc_ad=z-m&oh=1637634ecb1bc2c55cfa8dac6f279ecd&oe=58FB1894" } } ] } } ] } ] }

So I updated command setting code to the following and it correctly sets command to be image as I tested but stilldoes not send image back. If I send text back instead of image inside "case 'image':" block then it does send the text but not image.

if (!empty($message['message'])) { if(isset($message['message']['text'])){ $messageText = $message['message']['text']; $command = 'text'; } else if(isset($message['message']['attachments'])){ $command = $message['message']['attachments'][0]['type']; } } else if (!empty($message['postback'])) { $command = $message['postback']['payload']; }

@wittfabian
Copy link
Contributor

wittfabian commented Apr 20, 2017

No, the idea is to send the text "image" as command.
In this case, the bot sends an image back.

@nbaghiro
Copy link

nbaghiro commented Apr 20, 2017

It does not do it, in your code command would never be set to image IMO. Because there is no "text" field in "message" object of the response when you send image

EDIT: ohh you mean sending "image" as text message to bot

@wittfabian
Copy link
Contributor

Why not? $message['message']['text'] contains the text you send to the bot.

@nbaghiro
Copy link

nbaghiro commented Apr 20, 2017

I get it now, I did not understand it properly before.

I thought you would nee to send real image (not "image" text) to bot in order to receive image from bot.

@wittfabian
Copy link
Contributor

The initial problem is solved, use the following image:
http://res.cloudinary.com/gememerald/image/upload/v1485745856/facebook-announces-clickable-hashtags--resolution-media-17_cckv7m.png

@wittfabian
Copy link
Contributor

Closed. The example will be updated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants