雑記

2009年10月11日日曜日

ZendFramework: Zend_Service_Twitterで超簡単なbotを作ってみる

このあいだ、ZendFrameworkのZend_ServiceにTwitterが追加されている事を知って、簡単なbotを作ってみました。

botといっても本当に簡単で、reply返しをするだけのもの。


require_once "Zend/Service/Twitter.php";
define(DATEFILE, "date.dat");


$datefile = file_get_contents(DATEFILE);


$twitter = new Zend_Service_Twitter("ユーザー名", "パスワード");


$response = $twitter->status->replies();


foreach ($response as $reply) {
    if ($datefile <= date("Y-m-d H:i:s",strtotime($reply->created_at)))
        $twitter->status->update("@".$reply->user->name." "."こんにちは、".$reply->user->name."さん。");
}


file_put_contents(DATEFILE, date("Y-m-d H:i:s"));


エラーチェックは特にしてない。
一応、簡単にだけど前回の実行時間を取って置いて、重複して送らないようにしてみる。
Twitterbotはお手軽だけど、色々やろうとするとなかなか面白い。
もう少し凝ったbotも作ってみたけど、正直用途ってむずかしいよね・・・ネタbotはすぐに飽きるし。

Zend_Service_Twitterについてはマニュアルを読んでおくと良いかも。Twitter APIで出来る事は大体できるようだけど。

0 件のコメント: