Last active
September 22, 2015 07:12
-
-
Save j67678/a1569f0455c479d2accf to your computer and use it in GitHub Desktop.
Simple bearychat image robot in world's best language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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