[天気予報リプライ]<?php // twitteroauth.phpを読み込む require_once("twitteroauth.php"); $consumer_key = ""; $consumer_secret = ""; $access_token = ""; $access_token_secret = ""; //最後に返事したリプライを保存するファイル $filename = ""; $host = "https://api.twitter.com/1.1/statuses/mentions_timeline.json"; //返信一覧 $fp = @fopen($filename,'rb') or die("ファイルが開けません"); flock($fp, LOCK_EX); $line = fgets($fp, 64); fclose($fp); // OAuthオブジェクト生成 $to = new TwitterOAuth($consumer_key,$consumer_secret,$access_token,$access_token_secret); //保存しておいたステータスid取得。このid以降の返信を読み込む。なかったり読み込めなかったら最新1件 if(!empty($line)){ $req = $to->OAuthRequest($host,"GET",array("since_id"=>$line)); }else{ $req = $to->OAuthRequest($host,"GET",array("count"=>"1")); } //json文字列をオブジェクトに代入する $JSON = json_decode($req); //フレンドリストのオブジェクト作成 $host = "https://api.twitter.com/1.1/friends/ids.json"; $req = $to->OAuthRequest($host,"GET",array()); $friends = json_decode($req); $friendslist = array(); foreach($friends->ids as $item){ $friendslist[] = (String)$item; } //順に返信 $i = count($JSON)-1; $j = 0; while($i >= $j) { $var = $JSON[$i]->text; $com = ereg_replace("\@$username", "", $var); $reply_name = $JSON[$i]->user->screen_name; $name = $JSON[$i]->user->name; $id = $JSON[$i]->id_str; //ゴチャゴチャするので、返信のセリフ組み合わせはユーザー定義関数で。 $message = serif($reply_name,$name,$com,$id); //そのままポストの関数へ… if($message){ $result = $to->OAuthRequest("https://api.twitter.com/1.1/statuses/update.json","POST",array("status"=>$message,"in_reply_to_status_id"=>"$id")); print $result; } $i--; } //最終発言リプライのidを保存してる部分。先頭を調べて… $string = $JSON[0]->id_str; //空じゃなかったら書き込み if(!empty($string)){ $dat = (string)$string; file_put_contents($filename,$dat,LOCK_EX); } //セリフ生成のユーザー定義関数 function serif($reply_name,$name,$com,$id){ $break_flag = false; $today = date("Y/m/d l H:i:s");//デバッグ用の日付表示(なくてよい) //お天気 $w_city = array( // 都市名→都市番号の変換用配列 "稚内"=>"011000","旭川"=>"012010","留萌"=>"012020","札幌"=>"016010","岩見沢"=>"016020","倶知安"=>"016030","網走"=>"013010","北見"=>"013020","紋別"=>"013030","根室"=>"014010","釧路"=>"014020","帯広"=>"014030","室蘭"=>"015010","浦河"=>"015020","函館"=>"017010","江差"=>"017020","青森"=>"020010","むつ"=>"020020","八戸"=>"020030","秋田"=>"050010","横手"=>"050020","盛岡"=>"030010","宮古"=>"030020","大船渡"=>"030030","仙台"=>"040010","白石"=>"040020","山形"=>"060010","米沢"=>"060020","酒田"=>"060030","新庄"=>"060040","福島"=>"070010","小名浜"=>"070020","若松"=>"070030","水戸"=>"080010","土浦"=>"080020","宇都宮"=>"090010","大田原"=>"090020","前橋"=>"100010","みなかみ"=>"100020","さいたま"=>"110010","熊谷"=>"110020","秩父"=>"110030","東京"=>" 130010","大島"=>"130020","八丈島"=>"130030","父島"=>"130040","千葉"=>" 120010","銚子"=>"120020","館山"=>"120030","横浜"=>" 140010","小田原"=>"140020","甲府"=>"190010","河口湖"=>"190020","富山"=>"160010","伏木"=>"160020","金沢"=>"170010","輪島"=>"170020","福井"=>"180010","敦賀"=>"180020","新潟"=>"150010","長岡"=>"150020","高田"=>"150030","相川"=>"150040","長野"=>"200010","松本"=>"200030","飯田"=>"200030","静岡"=>"220010","綱代"=>"220020","三島"=>"220030","浜松"=>"220040","名古屋"=>"230010","豊橋"=>"230020","岐阜"=>"210010","高山"=>"210020","尾鷲"=>"240020","大津"=>"250010","彦根"=>"250020","京都"=>"260010","舞鶴"=>"260020","大阪"=>"270000","神戸"=>"280010","豊岡"=>"280020","奈良"=>"290010","風屋"=>"290020","和歌山"=>"300010","潮岬"=>"300020","岡山"=>"330010","津山"=>"330020","広島"=>"340010","庄原"=>"340020","松江"=>"320010","浜田"=>"320020","西郷"=>"320030","鳥取"=>"310010","米子"=>"310020","下関"=>"350010","山口"=>"350020","柳井"=>"350030","萩"=>"350040","徳島"=>"360010","日和佐"=>"360020","高松"=>"370000","松山"=>"380010","新居浜"=>"380020","宇和島"=>"380030","高知"=>"390010","室戸"=>"390020","清水"=>"390030","福岡"=>"400010","八幡"=>"400020","飯塚"=>"400030","久留米"=>"400040","大分"=>"440010","中津"=>"440020","日田"=>"440030","佐伯"=>"440040","長崎"=>"420010","佐世保"=>"420020","厳原"=>"420030","福江"=>"420040","佐賀"=>"410010","伊万里"=>"410020","熊本"=>"430010","阿蘇乙姫"=>"430020","牛深"=>"430030","人吉"=>"430040","宮崎"=>"450010","延岡"=>"450020","都城"=>"450030","高千穂"=>"450040","鹿児島"=>"460010","鹿屋"=>"460020","種子島"=>"460030","名瀬"=>"460040","那覇"=>"471010","名護"=>"471020","久米島"=>"471030","南大東島"=>"472000","宮古島"=>"473000","石垣島"=>"474010","与那国島"=>"474020","津"=>"240020" ); foreach( $w_city as $w_area => $w_areaNo){ if( preg_match( "/".$w_area."/u", $com ) ){ $cityName = $w_area ; break; } } if( empty($cityName) ){ $cityName = "東京"; } $where = $w_city["$cityName"]; $w_day = array( // 日付→日付パラメータの変換用配列 "今日"=> 0,"きょう"=> 0,"明日"=> 1,"あす"=> 1,"あした"=> 1,"明後日"=> 2,"あさって"=> 2 ); foreach( $w_day as $w_time => $w_timeNo){ if( preg_match( "/".$w_time."/u", $com ) ){ $timeName = $w_time ; break; } } if( empty($timeName) ){ $timeName = "今日"; } $day = $w_day["$timeName"]; $tenJSON = json_decode(file_get_contents('http://weather.livedoor.com/forecast/webservice/json/v1?city='.$where)); $tenki_obj = $tenJSON->forecasts[$day]; $telop = $tenki_obj->telop; $max = $tenki_obj->temperature->max->celsius; $min = $tenki_obj->temperature->min->celsius; //語句のマッチを探す if($break_flag === false){ $tenki=array("".$timeName."の".$cityName."の天気は".$telop."! 最高気温は".$max."℃、最低気温は".$min."℃となっています!","はい、現場のプラムです! ".$timeName."の".$cityName."の天気は".$telop."! 最高気温は".$max."℃、最低気温は".$min."℃の模様です!","はーい、現場のプラムです! ".$timeName."の".$cityName."の天気は".$telop."! 最高気温は".$max."℃、最低気温は".$min."℃と予測されます!"); $tenk=array("".$day.""); //特定語句に反応する部分 if($reply_name != null){ $reply_name = "@$reply_name"; if(stristr($com, "RT")){ $return = null; }else if(preg_match("/天気|てんき/u",$com)){ $key = array_rand($tenki); $post = $tenki[$key]; $return = $reply_name." ".$post; }else if(preg_match("/動作テスト/u",$com)){ $key = array_rand($tenk); $post = $tenk[$key]; $return = $reply_name." ".$post; } } //戻り値 return $return; }else{ //$reply_nameが空のときはnullになる return null; } } ?> 一覧に戻る |