Grynets
Grynets

Reputation: 2525

How to get flickr random images for facebook messenger bot?

I wan't my bot to receive images if user asks for it.
But I really not sure how to make it possible.
I've created special page on my web site, where I get images by request.
But, my bot isn't working.
I've created flickr account, created special page, the connected my bot's code with flickr page code.
So, the question is, how to make my bot search pictures in flickr and then provide them to user?

Here is code for bot page:

 include 'flickr.php';
 $row = "My access token";
 $url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$row;


 $ch = curl_init($url);
 if($message == "image'.$set.'")
{
    $load_images = $hasil;
    $image = file_get_contents($load_images);
    $jsonData = '{
        "recipient":{
        "id":"'.$sender.'"
        },
          "message":{
              "attachment":{
                   "type":"image",
                   "payload":{
                       "url":"'.$image.'"
                  }
             }
        }
    }'; 
 };

 $json_enc = $jsonData;
 curl_setopt($ch, CURLOPT_POST, 1);   
 curl_setopt($ch, CURLOPT_POSTFIELDS, $json_enc);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));  

 if(!empty($input['entry'][0]['messaging'][0]['message'])){
    $result = curl_exec($ch);
 }

And here is my flickr code.
I think that the mistake is somewhere here, but I can't see it.

 require_once 'webhook.php';

 class flickr
 {
   var $api;
     function __construct($api) {
        $this->api = $api;
      }
  function flickr_photos_search($search,$count_image,$size)
    {
    $params = array(
    'api_key'   => $this->api,
    'method'    => 'flickr.photos.search',
    'text'      => $search,
    'format'    => 'rest',
    'per_page'  => $count_image,
    'page'      => 1,);
    $xml = $this->create_url($params);
    if(@$rsp = simplexml_load_file($xml))
    {
        if (count($rsp)<>0)
        {
            foreach($rsp->photos->children() as $photo)
            {
                if ($photo->getName()=='photo')
                {
                    $farm=$photo->attributes()->farm;
                    $server=$photo->attributes()->server;
                    $id=$photo->attributes()->id;
                    $secret=$photo->attributes()->secret;
                    if ($size=='Med')
                    {
                        $sz="";
                    }
                    else
                    {
                        $sz = "_".$size;
                    }
                    $gbr[]='<img src="https://farm'.$farm.'.staticflickr.com/'.$server.'/'.$id.'_'.$secret.$sz.'.jpg'.'" /> ';
                }
            }
        }
        else
        {
            die("No images found!");
        }
    }else
    {
        die("wrong parameter");
    }
    return $gbr;
  }
 function create_url($params)
    {
    $encoded_params = array();
    foreach ($params as $k => $v){

        $encoded_params[] = urlencode($k).'='.urlencode($v);
    }
    $url = "https://api.flickr.com/services/rest/?".implode('&', $encoded_params);
    return $url;
}
 }

 if(isset($_REQUEST[$set]))
 {

 $search=$_REQUEST[$set];
 $result= 30;
 $size = 'm';

 $flickr = new flickr('My flickr secret code');
 $gbr    = $flickr->flickr_photos_search($search,$result,$size);
 foreach($gbr as $hasil)
    {
        echo $hasil.' ';
    }
 }

Upvotes: 0

Views: 407

Answers (1)

fritak
fritak

Reputation: 191

There is a problem:

$load_images = $hasil;
$image = file_get_contents($load_images);
"payload":{
    "url":"'.$image.'"
}

From documentation - Payload image is just URL of that image

You can use my API (https://github.com/Fritak/messenger-platform) for that, really easy to use:

// Send an image (file).
$bot->sendImage($userToSendMessage, 'http://placehold.it/150x150');

Upvotes: 1

Related Questions