Skip to content

Instantly share code, notes, and snippets.

@j67678
Last active September 22, 2015 07:12
Show Gist options
  • Save j67678/a1569f0455c479d2accf to your computer and use it in GitHub Desktop.
Save j67678/a1569f0455c479d2accf to your computer and use it in GitHub Desktop.
Simple bearychat image robot in world's best language
<?php
$post = json_decode(file_get_contents("php://input"));
if (!$post) {
die('Hello world!');
}
$text = trim(substr($post->text, strlen($post->trigger_word)));
if (!$text) {
die('{ "text": "what?" }');
}
$ret = json_decode(file_get_contents('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&imgsz=small|medium|large&q=' . urlencode($text)));
if (!count($ret->responseData->results)) {
die('{ "text": "404" }');
}
$rand = array_rand($ret->responseData->results);
$image_url = $ret->responseData->results[$rand]->url;
echo json_encode([
'attachments' => array([
# 'text' => $image_url,
'images' => array([
'url' => $image_url
])
])], JSON_UNESCAPED_SLASHES);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment