最後に、完成したものを一通り見てみましょう。
自分は cron で実行するため、実行用のシェルも作っています。
これによって実際に動いているアカウントが です。
[Account] user = j2e2j pass = xxxxxxxxxx
[Environment] cache = true cacheDir = cache
cacheHttp = false cacheHttpDir = http_cache
[Behaviours] TuiBotter_Sample_j2e2j = /path/to/your/project/j2e2j.php
class TuiBotter_j2e2j implements TuiBotter_Event_BeFollowed, TuiBotter_Event_UpdatedHomeTL, TuiBotter_Event_BeMentioned { public function eventBeFollowed(Tuitter_User $user, Tuitter $tuitter) { if(!$user->isFollowing()) $user->follow(); }
public function eventBeMentioned(Tuitter_Tweet $tweet, Tuitter $tuitter) { self::_j2e2j($tweet, $tuitter); }
private static function _j2e2j($tweet, $tuitter) { $en = self::_translate($tweet->text, 'ja', 'en'); $ja = self::_translate($en, 'en', 'ja'); $tuitter->sendMessage('TT @'.$tweet->user->screen_name.': '.$ja); }
private static function _translate($text, $from, $to) { $res = ''; $url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='.urlencode($text).'&langpair='.$from.'%7C'.$to; if($trans_json = @file_get_contents($url)){ $trans_json = mb_convert_encoding($trans_json, 'utf-8', 'auto'); $trans_data = json_decode($trans_json); if(isset($trans_data->responseData->translatedText)){ $res = $trans_data->responseData->translatedText; } } return $res; } }
require_once 'Tuitter/Tuitter.php';
require_once 'TuiBotter/TuiBotter.php';
try{
$bot = new TuiBotter('TuiBotter.ini');
$bot->heartbeat();
}catch(Exception $e){
echo $e->getMessage()."\n";
}
cd /path/to/your/project/
/usr/bin/php -f ./HeartBeat.php