Created
May 12, 2015 07:50
-
-
Save dedikisme/66de772832fab87f443a to your computer and use it in GitHub Desktop.
This file contains 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 | |
namespace console\controllers; | |
use console\components\YMEngine; | |
class YmController extends \yii\console\Controller { | |
public $username = 'xxxx'; | |
public $password = 'xx'; | |
public $consumer_key = 'xxx--'; | |
public $secret_key = 'xxxxx'; | |
public $debug = false; | |
public function actionIndex() { | |
return $this->render('index'); | |
} | |
public function actionRun() { | |
$engine = new YMEngine($this->consumer_key, $this->secret_key, $this->username, $this->password); | |
$engine->debug = $this->debug; | |
if ($engine->debug) | |
echo '> Fetching request token' . PHP_EOL; | |
if (!$engine->fetch_request_token()) | |
die('Fetching request token failed'); | |
if ($engine->debug) | |
echo '> Fetching access token' . PHP_EOL; | |
if (!$engine->fetch_access_token()) | |
die('Fetching access token failed'); | |
echo '> Signon as: ' . $this->username . PHP_EOL; | |
if (!$engine->signon()) | |
die('Signon failed'); | |
$seq = -1; | |
while (true) { | |
$resp = $engine->fetch_long_notification($seq + 1); | |
if (isset($resp)) { | |
if ($resp === false) { | |
if ($engine->get_error() != -10) { | |
if ($engine->debug) | |
echo '> Fetching access token' . PHP_EOL; | |
if (!$engine->fetch_access_token()) | |
die('Fetching access token failed'); | |
if ($engine->debug) | |
echo '> Signon as: ' . $this->username . PHP_EOL; | |
if (!$engine->signon(date('H:i:s'))) | |
die('Signon failed'); | |
$seq = -1; | |
} | |
continue; | |
} | |
foreach ($resp as $row) { | |
foreach ($row as $key => $val) { | |
if ($val['sequence'] > $seq) | |
$seq = intval($val['sequence']); | |
/* | |
* do actions | |
*/ | |
if ($key == 'buddyInfo') { //contact list | |
if (!isset($val['contact'])) | |
continue; | |
if ($engine->debug) | |
echo PHP_EOL . 'Contact list: ' . PHP_EOL; | |
foreach ($val['contact'] as $item) { | |
if ($engine->debug) | |
echo $item['sender'] . PHP_EOL; | |
} | |
if ($engine->debug) | |
echo '----------' . PHP_EOL; | |
} | |
else if ($key == 'message') { //incoming message | |
echo '+ Incoming message from: "' . $val['sender'] . '" on "' . date('H:i:s', $val['timeStamp']) . '"' . PHP_EOL; | |
/* if ($engine->debug) */ echo ' ' . $val['msg'] . PHP_EOL; | |
/* if ($engine->debug) */ echo '----------' . PHP_EOL; | |
//reply | |
$words = explode(' ', trim(strtolower($val['msg']))); | |
if ($words[0] == 'help') { | |
$out = 'This is Yahoo! Open API demo' . PHP_EOL; | |
$out .= ' To get recent news from yahoo type: news' . PHP_EOL; | |
$out .= ' To get recent entertainment news from yahoo type: omg' . PHP_EOL; | |
$out .= ' To change my/robot status type: status newstatus' . PHP_EOL; | |
} else if ($words[0] == 'news') { | |
if ($engine->debug) | |
echo '> Retrieving news rss' . PHP_EOL; | |
$rss = file_get_contents('http://rss.news.yahoo.com/rss/topstories'); | |
if (preg_match_all('|<title>(.*?)</title>|is', $rss, $m)) { | |
$out = 'Recent Yahoo News:' . PHP_EOL; | |
for ($i = 2; $i < 7; $i++) { | |
$out .= str_replace("\n", ' ', $m[1][$i]) . PHP_EOL; | |
} | |
} | |
} else if ($words[0] == 'omg') { | |
if ($engine->debug) | |
echo '> Retrieving OMG news rss' . PHP_EOL; | |
$rss = file_get_contents('http://rss.omg.yahoo.com/latest/news/'); | |
if (preg_match_all('|<title>(.*?)</title>|is', $rss, $m)) { | |
$out = 'Recent OMG News:' . PHP_EOL; | |
for ($i = 2; $i < 7; $i++) { | |
$out .= str_replace(array('<![CDATA[', ']]>'), array('', ''), $m[1][$i]) . PHP_EOL; | |
} | |
} | |
} else if ($words[0] == 'status') { | |
$engine->change_presence(str_replace('status ', '', strtolower($val['msg']))); | |
$out = 'My status is changed'; | |
} else { | |
$out = 'Please type: help'; | |
} | |
//send message | |
echo '> Sending reply message ' . PHP_EOL; | |
echo ' ' . $out . PHP_EOL; | |
echo '----------' . PHP_EOL; | |
$engine->send_message($val['sender'], json_encode($out)); | |
} else if ($key == 'buddyAuthorize') { //incoming contact request | |
echo PHP_EOL . 'Accept buddy request from: ' . $val['sender'] . PHP_EOL; | |
echo '----------' . PHP_EOL; | |
if (!$engine->response_contact($val['sender'], true, 'Welcome to my list')) { | |
$engine->delete_contact($val['sender']); | |
$engine->response_contact($val['sender'], true, 'Welcome to my list'); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment