twitter bot 講習会
講師紹介 小林佑輔(KOBA) 上野研究室 4年 卒研はフィールドワーク系 twitter id:KOBA5884 2008/6/12 twitterをはじめる
【出典】「機械はどれだけ人間に近づけるのか」第2回チームラボアルゴリズムコンテストより http://www.myopenarchive.org/documents/view/440
2009 年 5 月:「あ、 twitter の bot 作りてぇ。」 6 月: bot 開発関連のサイトを漁り始める 7 月: KOBA5884_bot の原型が完成
@KOBA5884_bot 定時で独り言をつぶやく。(5回/ h ) 話しかけると、適当に返事を返す。 お願いすると歌う。寝ろと命令すると寝る。 メモ機能(特定のつぶやきを記録&再送)
本日の目標 botの開発環境を整えよう botに何か喋らせてみよう 話しかけると反応するようにしよう
bot の開発環境を整えよう bot 用のアカウントを作ろう。 今回は PHP を使って開発します。 -> xampp を使ってみよう
bot 用アカウントの作成 http://www.lifehacker.jp/2009/08/twittergmail.html Gmail では、 メールアカウント名に「 . (ピリオド)」を追加 することができ、 かつ これらのアカウントは同じアカウントで受信 さ れるようになっています。 [email_address] [email_address] m [email_address] com 本アド 偽装アド 本アドにメールが届く
今回は日本語推奨 半角英数字 「いつもの」はダメ!絶対!
phpとは 動的にwebページを生成する webサーバの拡張機能のひとつ サーバーサイドプログラミングってヤツです。 webサーバに置かないと動作しない ->ローカル環境で動作させる環境を作ろう
xampp を使おう xamppのダウンロード&インストール ダウンロード: http://bit.ly/JA5bX インストール: http://bit.ly/651sFF サンプルファイルのダウンロード ダウンロード: http://bit.ly/6mtZxe サンプルファイルの設置 xampplite -> htdocs -> twitterbot
bot に何か喋らせてみよう 決まった台詞を喋らせる TwitterAPIについて(POST) 用意した台詞をランダムで喋らせる
 
Lesson 1
【出典】 PHPでTwitterに投稿 - HIRACCHI H.D. http://d.hatena.ne.jp/hirataka 522/20080126/1201300282 <?php $username  =  ”your username” ; $password  =  ”your password” ; $params  = &quot;status=&quot;. rawurlencode( ”message” ); $url = &quot; http://twitter.com/statuses/update.xml? &quot;; $result = file_get_contents($url.$params , false, stream_context_create(array( &quot;http&quot; => array( &quot;method&quot; => &quot; POST &quot;, &quot;header&quot; => &quot;Authorization: Basic &quot;. base64_encode($username. &quot;:&quot;. $password) ) ))); ?>
決まった台詞を喋らせる $username  =  your username ; $password  =  your password ; $params  = &quot;status=&quot;. rawurlencode( message ); とりあえず、ここだけいじればOK
決まった台詞を喋らせる $username  = ”KOBA5884_bot” ; $password  = ”************” ; $params  = &quot;status=&quot;. rawurlencode( “ メッセージ” );
決まった台詞を喋らせる $username  =  ”your username” ; $password  =  ”your password” ; $mes  =  “ メッセージ” ; $params  = &quot;status=&quot;. rawurlencode( $mes );
【オマケ】投稿時のおまじない http://kuroutan.blog.shinobi.jp/Entry/3/ PHP とかをゆるりとべんきょうする twitter bot をとりあえず作ってみる (1) $url = &quot; http://twitter.com/statuses/update.xml? &quot;; $result = file_get_contents($url.$params , false, stream_context_create(array( &quot;http&quot; => array( &quot;method&quot; => &quot;POST&quot;, &quot;header&quot; => &quot;Authorization: Basic &quot;. base64_encode($username. &quot;:&quot;. $password) ) ))); 【参考サイト】
鯖 php twitter API GETの要求/POST GET
Lesson 2
用意した台詞をランダムで喋らせる $username  =  your username ; $password  =  your password ; $mes[]  =  “ メッセージ 1” ; $mes[]  =  “ メッセージ 2” ; $mes[]  =  “ メッセージ 3” ; $mes  = $mes[mt_rand(0,count($mes)-1)]; $params  = &quot;status=&quot;. rawurlencode( $mes );
用意した台詞をランダムで喋らせる $username  =  your username ; $password  =  your password ; $mes[0]  =  “ メッセージ 1” ; $mes[1]  =  “ メッセージ 2” ; $mes[2]  =  “ メッセージ 3” ; $mes  = $mes[mt_rand(0,count($mes)-1)] $params  = &quot;status=&quot;. rawurlencode( $mes ); 配列の個数を数える ( この場合は 3 つ )
用意した台詞をランダムで喋らせる $username  =  your username ; $password  =  your password ; $mes  = array( “ メッセージ 1”,“ メッセージ 2”,“ メッセージ 3”, ); $mes  = $mes[mt_rand(0,count($mes)-1)] $params  = &quot;status=&quot;. rawurlencode( $mes ); 配列
用意した台詞をランダムで喋らせる $username  =  your username ; $password  =  your password ; $mes  = array( “ メッセージ 1”, “ メッセージ 2”, “ メッセージ 3”, ); $mes  = $mes[mt_rand(0,count($mes)-1)] $params  = &quot;status=&quot;. rawurlencode( $mes );
用意した台詞をランダムで喋らせる $username  =  your username ; $password  =  your password ; $mes  = file(“ mes.dat ”); $mes  = $mes[mt_rand(0,count($mes)-1)] $params  = &quot;status=&quot;. rawurlencode( $mes ); mes.dat メッセージ 1 メッセージ 2 メッセージ 3 ※ 改行毎に1要素扱い->
話しかけると反応するようにしよう TwitterAPIについて(GET) 話しかけてきた人にお返事 (reply) させる 「どの発言に対する返事か」をわかりやすくする しつこくお返事させないためには?
Lesson 3
http://twitter.com/statuses/mentions.xml
 
GET で得られる主な要素 id twitter全体での発言の通し番号 text 発言の内容 name ユーザー名 (KOBA) screen_name ユーザーid(@KOBA5884)
 
id:6646318098 id:6646339011 id:6646347184 id:6646395413 KOBA5884_bot :  KOBA5884_bot 428bot_2 : 小岩井よつば 2 号
@ + screen_name + 台詞 $mes  =
取得時のおまじない $replyurl  = &quot; http://twitter.com/statuses/mentions.xml?count=1 &quot; ; $result = file_get_contents($replyurl , false, stream_context_create(array( &quot;http&quot; => array( &quot;method&quot; => &quot; GET &quot;, &quot;header&quot; => &quot;Authorization: Basic &quot;. base64_encode($username. &quot;:&quot;. $password) ) )));
鯖 php twitter API GETの要求/POST GET
↑ $result の中身 $result をソース表示してみた->
$id  = $xml->status->id; $name  = $xml->status->user->name; $screen_name  = $xml->status->user->screen_name; $text  = $xml->status->text; $xml  = simplexml_load_string($result); 各要素を変数に突っ込んで扱いやすくする 文字列をオブジェクトとして扱うようにする
返信用メッセージの生成 $mes  = array( “ メッセージ 1”,“ メッセージ 2”,“ メッセージ 3”, ); $mes  = $mes[mt_rand(0,count($mes)-1)]; $mes  = &quot;@&quot; .  $screen_name  . &quot; &quot; .  $mes  ; 空白
どの発言に返信したかをわかりやすくしたい ← コレを付けたい ← 現状だとこんな感じ
どの発言に返信したかをわかりやすくしたい id:6646318098 id:6646339011 id:6646347184 id:6646395413
どの発言に返信したかをわかりやすくしたい $params  =  &quot;status=&quot;. rawurlencode( $mes ) .&quot;&in_reply_to_status_id=&quot;. $id ; $params  = &quot;status=&quot;. rawurlencode( $mes ); 書き込み id の情報を付加してあげる
おまけ(Lesson 4) 特定のユーザーに対して特殊な反応を返す if($screen_name == &quot; 反応させたいユーザー id&quot;){ [ 特定のユーザーに対する処理 ] }else{ [ それ以外のユーザーに対する処理 ] }
おまけ (Lesson 5) 特定の単語に対して特殊な反応を返す if(preg_match( &quot;/ 反応させたいワード /&quot;, $text)){ [ 特定のワードに対する処理 ] }else{ [ それ以外のワードに対する処理 ] }
おまけ (Lesson 5) 特定の単語に対して特殊な反応を返す(一例) if(preg_match( &quot;/ おはよ /&quot;, $text)){ 文中に「 おはよ 」が入っていれば処理を実行 例:「 おはよ う」「 おはよ ー」「皆様 おはよ うございます」 }else if(preg_match( &quot;/ おやすみ | 寝る /&quot;, $text)){ 文中に「 おやすみ 」 または 「 寝る 」が入っていれば処理を実行 例:「 おやすみ なさい」「 もう寝る 」 }else if(preg_match( &quot;/( メモ | memo )( : | : )/&quot;, $text)){ 文中に以下のいずれかが入っていれば処理を実行 「 メモ : 」「 メモ : 」「 memo : 」「 memo : 」 }
 
Lesson 6
「しつこくお返事」…? 現状だと、 最新1件の reply に対して 返信する たとえ既に返信済であっても。
id:6774038773
しつこくお返事させないために 一番最後に返信した相手の発言を覚えさせれば…! そんなわけで、ここでも  $id  を使います。
latest.dat 0( 初期値 ) if( $latest  ==  $id ){ [ 投稿の処理を行わない ] }else{ [ 1.投稿処理 ] [ 2. latest.dat の書き換え ] } 6774038773 $id    = [ 自分宛の呟き最新1件の id] $latest  = [ 前回返信した呟きの id] # もし、それらが同一なら # 違った場合 別ファイルに 記憶させておく
tips: 外部ファイルの読み書き $fp  = fopen(&quot; latest.dat &quot;, ' r '); $latest  =  fgets ($fp); fclose($fp); $fp = fopen(&quot; latest.dat &quot;, ' w '); fputs ($fp,  $id ); fclose($fp); ファイルの内容を読み込む ファイルに値を書き込む
おまけ 【定期実行させるための環境】 1. php が動作する 2. cron が書き換え出来る サーバー上のスクリプトを自動実行 させるためのデーモン(常駐)プロセス
鯖 php twitter API GETの要求/POST GET
鯖 php twitter API GETの要求/POST GET cron で実行させる 外部からアクセス
自動実行に関する参考サイト 【 cron が使える環境なら】  crontabの書き方 — server-memo.net http://www.server-memo.net/tip s/crontab.html 【cronが使えない環境なら】 cron タスクの設定 【   webcron  の利用】    http://drupal.jp/drupal5/guide/cron_task_webcron
Let’s Try!!

1221bot講習会