Ba.とWEBと梅干し太郎

WEB制作・プログラミング・音楽の学習したこと・つくったもののアウトプットブログです。あとは日記。

毎日のメディアチェックをLINEbotで一元化してみた

(今さらだけど)Linebotを作りました

ざっくり要件

  • メディアのチェックを一元化したい

  • feedのないサイトもチェックしたい

  • 毎日使うLINEで見れたら最高

wow! Linebot × スクレイピングで実現できるじゃん

ということで、こんなのを作って運用してました。
LINEbotに話かけると、WEBサイトから最新記事を取得してきてくれます。

LINEは毎日見るので、使い勝手良いです。ここ一ヶ月、毎日使っています。

f:id:masayannuu:20160712232603j:plain

巡回したいサイトが割と多いので、テキストをキーにして返信を分けています。 これなら情報の可読性もあがるし、見たい情報も仕分けできるので、クーロンでの自動実行はやめちゃいました。

毎朝、「おはよー」「ダーリン」と呟けば、もりもんbotは情報を拾ってきてくれるわけです。

f:id:masayannuu:20160712232612j:plain

あと、既読無視は寂しいので、キー以外の要素を渡すとキーの情報を教えてくれるようにしています。

f:id:masayannuu:20160712233133j:plain

参考にしたのはこれらのサイトです。
数多くの先人の方々が記事にまとめてくださっていたので、さほど時間をかけずに作れました。

※知恵をお借りしました。ありがとうございました。
LINE BOT API Trialでできる全ての事を試してみた - Qiita
http://qiita.com/betchi/items/8e5417dbf20a62f2239d

LINE BOT をとりあえずタダで Heroku で動かす - Qiita
http://qiita.com/yuya_takeyama/items/0660a59d13e2cd0b2516

さて、メディアチェックの一元管理とは言ったものの、処理としてはスクレイピングしてきた内容を文字列にしてlinebotにjsonで渡しているだけです。
スクレイピングには、phpQueryを使わせていただいてます。
js感覚で扱えるので、非常に使いやすかったです。  

ソースはこちら。

<?php
require('phpQuery-onefile.php');

/*
*スクレイピング処理のメソッド群
*@param url= メディアのURL 
*@param php_query = パースされたURL
*@param div_id = 取得したいノードのid
*@param find = 取得したいフィールド
*@param i = 取得数
*/
function perthUrl($url){
   $perthed_url = "";
   $php_query = NULL;

   $perthed_url = file_get_contents($url);
   $php_query = phpQuery::newDocument($perthed_url);
   return $php_query;
}

function getNewsContent($php_query, $div_id, $find){
   $i = 0;
   foreach($php_query[$div_id] as $value) {
       if($i++>4){
           break;
       }else
           $title = pq($value)->find($find)->text();
           $url = pq($value)->find('a')->attr('href');
           $result[] = "{$title} \\n {$url} \\n";
   }
   return $result;
}

/*
*Linebot
*/

$channel_id = "your channel id";
$channel_secret = "your channel select";
$mid = "your mid";

$request_body_string = file_get_contents('php://input');
$request_body_object = json_decode($request_body_string);
$request_content = $request_body_object->result{0}->content;
$request_text = $request_content->text;
$request_from = $request_content->from;
$content_type = $request_content->contentType;

$headers = [
"Content-Type: application/json; charset=UTF-8",
"X-Line-ChannelID: {$channel_id}", 
"X-Line-ChannelSecret: {$channel_secret}", 
"X-Line-Trusted-User-With-ACL: {$mid}", 
];

if($request_text === 'おはよー') {
    $perthed_url = perthUrl('http://techable.jp/');
    $text = implode("#", getNewsContent($perthed_url, '.h2t', 'h2'));

    $perthed_tc = perthUrl('http://jp.techcrunch.com/');
    $text_tc = implode("#", getNewsContent($perthed_tc, '.post-title', 'a'));

    $perthed_sd = perthUrl('http://social-design-net.com/');
    $text_sd =  implode("#", getNewsContent($perthed_sd, '.column', 'h3'));

    $response_text = <<< EOC
    【Techable】 \\n {$text} \\n 【Techcrunch】 \\n {$text_tc} \\n 【長沼さん】 \\n {$text_sd}
EOC;
} 
elseif($request_text === 'ダーリン') {
    $perthed_dd = perthUrl('http://design-develop.net/');
    $text_dd = implode("#", getNewsContent($perthed_dd, 'h2', 'a'));

    $perthed_cc = perthUrl('http://postd.cc/');
    $text_cc = implode("#", getNewsContent($perthed_cc, 'h2.block-entry-title', 'a'));
    
    $response_text = <<< EOC
【DesignDevelpo】 \\n {$text_dd} \\n 【posted.cc】 \\n {$text_cc}
EOC;
}
else {
    $response_text = 'おはよーで定期メディア。\\n ダーリンで技術系記事を取得してくるよ。';
    }

  $response_message = <<< EOC
    {
      "to":["{$request_from}"],
      "toChannel":1383378250,
      "eventType":"138311608800106203",
      "content":{
        "contentType":1,
        "toType":1,
        "text":"{$response_text}"
      }
    }
EOC;

$curl = curl_init('https://trialbot-api.line.me/v1/events');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $response_message);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($curl, CURLOPT_PROXY, getenv('FIXIE_URL'));
$output = curl_exec($curl);

今は気まぐれに巡回サイトを増やしつつ、gmailAPIと繋ぐ実験中です。
繋いだら忘備録でAPIの叩き方をあげまーす。