アイコンをリプライ等で変える

イコン画像をプログラムで変えるというのをやってみました
…といっても、自力では挫折で他の方の流用な形になってます。私のオリジナルではありません。

自分が使っているアイコン変更プログラム - AbyssLukeのガイドライン(Maybe archived)

すばらしい。あと、もうひとつ参考にさせてもらったが著作権者がわからない…

色々応用が利くように関数化し、例もあげてみました
下のソースですが、通常は同じアイコンで占いの時だけ変わる感じです。
ソースは違いますが邪気眼botの占いも同じ原理でアイコンが変わります

邪気眼 (@jakigan_bot) | Twitter
解説は…めんどうなのでソース見てw
これで面白いbotが増えると良いなー。

<?php

$username = "";
$password ="";

//この関数で追加が必要なのはこの3つの変数
//画像を入れておくディレクトリ
$img_dir = "./img/";

//無駄な画像アップロードをさけるため、最後にUPしたjpgファイル名を保存しておくテキスト
$last_img = "img.dat";

//画像のファイルタイプ、pngならimage/png、gifならimage/gif
$mime = 'image/jpeg';


//あとはリプライのとほとんど同じ

$filename = "last.dat";

$host = "http://twitter.com/statuses/mentions.xml"; 

$fp = @fopen($filename,'rb') or die("ファイルが開けません");
flock($fp, LOCK_EX);
$line = fgets($fp, 64);
fclose($fp); 


if(!empty($line)){
$last_id=$line;
$host .="?since_id=".$last_id;
}
else{
    $host .="?count=1";
    }


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$result = curl_exec($ch);
curl_close($ch);

$XML = simplexml_load_string($result);

$i = count($XML)-1;
$j = 0;
 while($i >= $j) {
$var =  $XML->status[$i]->text;
$com = ereg_replace("\@$username", "", $var);
$reply_name = $XML->status[$i]->user->screen_name;
$name = $XML->status[$i]->user->name;

$message = serif($reply_name,$name,$com);

tweet($message ,$username,$password);
$i--;
}


$string = $XML->status[0]->id;
if(!empty($string)){
$dat = (string)$string;
file_put_contents($filename,$dat,LOCK_EX);
}


//セリフ生成のユーザー定義関数
function serif($reply_name,$name,$com){
    
//占いの結果。おみくじと画像が同じ数・並びになっているのを確認すること。
$uranai=array("大吉","中吉","小吉","末吉","","");
$uranai_img = array("daikiti.jpg","chuukiti.jpg","syoukiti.jpg","suekiti.jpg","kiti.jpg","kyou.jpg");
$janken=array("ぐー","ちょき","ぱー"); 

//あらかじめノーマル画像を設定しておき、占いの結果以外はノーマル画像にする
$normal = "uranai.jpg";
$img = $normal;

$reply_name = "@$reply_name";

//特定語句に反応する部分
if(stristr($com, "占いして")){
     $key = array_rand($uranai);
     $post = $uranai[$key];
     $return = $reply_name." ".$name."さんの運勢…".$post;
     //ここでイメージ画像を
     $img = $uranai_img[$key];     
    }
    else if(stristr($com, "じゃんけん")){
         $key = array_rand($janken);
         $post = $janken[$key];
         $return = $reply_name." ".$name."さん、勝負だ".$post;
}
else{
    $return = $reply_name." ".$com;
}
//セリフ発言前にイメージを変える
change_profile($img);
return $return;
}

//ポスト部分の関数
function tweet($message ,$username,$password)
{
$message =urlencode($message);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://twitter.com/statuses/update.xml');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "status=$message ");
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($ch);
curl_close($ch);
}

//AbyssLukeさんのソースをほぼ流用しています。ありがとうございます。http://d.hatena.ne.jp/abyssluke/20090615/1245062610

function change_profile($img){
//グローバルで各種変数を。画像を1ファイル渡すだけで変わる仕様になってます。
    global $img_dir,$last_img,$mime,$username,$password;
    
    $dat = file_get_contents($last_img);
    if($img != (string)$dat or $dat == null){
    $file = $img_dir;
    $file .=$img;
    if(file_exists( $file )){
        
		$boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10); 

		$filedata = file_get_contents($file);
		$postdata = "--".$boundary."\r\nContent-Disposition: form-data; name=\"image\"; filename=\"".$file."\"\r\nContent-Type: ".$mime."\r\n\r\n$filedata\r\n--".$boundary."--";
		$ctype = array("Content-Type: multipart/form-data; boundary=".$boundary, "Content-Length: ".strlen($postdata), "Expect:");

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'http://twitter.com/account/update_profile_image.xml');
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $ctype);
		curl_setopt($ch, CURLOPT_VERBOSE, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, 15);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

		$result = curl_exec($ch);
        curl_close($ch);
        echo 'ファイル'.$img.'をアップしました';
        $dat = (string)$img;
        file_put_contents($last_img,$dat,LOCK_EX);

        return true; 
}else{
    echo 'ファイル'.$img.'が存在しないためアップロードしませんでした';
    }
    }else{
        echo 'ファイルが同じだったのでアップしませんでした';
    }
}


?>