freeeeedomに行こうやー

やったこと興味持ったことなどなど書いていきます!

line bot 試してみたよ!

f:id:music431per:20160508231746j:plain

こんにちは!
初めまして!
こっちに移行してきて初めての投稿です。
eriって言います!
よろしくお願いします!

今回はline bot試してみたよってことで
やったことを書いてみました!

botとは

ボットとは、「ロボット」の略称で、もともと人間がコンピュータを操作して行っていたような処理を、人間に代わって自動的に実行するプログラムのこと。

ボットとは|bot|ロボット|robot - 意味/定義 : IT用語辞典より引用

つまり、このline botってのは
lineのチャットの返事をプログラムで返してくれるよ!ってものです。

このline botを個人で開発できるapiのお試し版を
lineが提供をしていたので今回はそれを使って試してみました!

BOT API Trial Account

やってみたこと

  • 送信者のデータ取ってみた!
  • webページからbotに通知を送ってみた!

botの動かし方については
ありがたいことにたくさんの方が
記事にしてくださっているので
それを参考にさせていただきましたm(_ _)m

送信者のデータを取ってみた!

『mydata』 って言ったら送信者のデータ取ってくれるやつです!

f:id:music431per:20160509144039j:plain こんな感じ

ソースはこんな感じ!

index.php

<?php
error_log("callback start.");

// アカウント情報設定
$channel_id = "[チャンネルID]";
$channel_secret = "[チャンネルシークレット]";
$mid = "[MID]";

// メッセージ受信
$json_string = file_get_contents('php://input');
$json_object = json_decode($json_string);
$content = $json_object->result{0}->content;
$text = $content->text;
$from = $content->from;


// ユーザ情報取得
// api_get_user_profile_request($from);

$json_user = (api_get_user_profile_request($from));
$user = json_decode($json_user);

$user_name = $user->contacts[0]->displayName;
$user_mid = $user->contacts[0]->mid;
$user_picture = $user->contacts[0]->pictureUrl;
$user_status_message = $user->contacts[0]->statusMessage;



// mydataってきたら返すメッセージ
if($text == "mydata"){
// ユーザ名
$name_content = <<< EOM
      "contentType":1,
      "text":"displayName「{$user_name}」"
EOM;
// ユーザid
$mid_content = <<< EOM
      "contentType":1,
      "text":"mid「{$user_mid}」"
EOM;
// ユーザのアイコン
$image_content = <<< EOM
        "contentType":2,
        "originalContentUrl":"{$user_picture}",
        "previewImageUrl":"{$user_picture}"
EOM;
// ステータスメッセージ
$status_message_content = <<< EOM
      "contentType":1,
      "text":"statusMessage「{$user_status_message}」"
EOM;

$event_type = "140177271400161403";
$content = <<< EOM
"messageNotified": 0,
"messages": [
    {{$name_content}},
    {{$mid_content}},
    {{$image_content}},
    {{$status_message_content}}
]
EOM;

$post = <<< EOM
{
    "to":["{$from}"],
    "toChannel":1383378250,
    "eventType":"{$event_type}",
    "content":{
        "toType":1,
        {$content}
    }
}
EOM;

api_post_request("/v1/events", $post);

error_log("callback end.");

function api_post_request($path, $post) {
    $url = "https://trialbot-api.line.me{$path}";
    $headers = array(
        "Content-Type: application/json",
        "X-Line-ChannelID: {$GLOBALS['channel_id']}",
        "X-Line-ChannelSecret: {$GLOBALS['channel_secret']}",
        "X-Line-Trusted-User-With-ACL: {$GLOBALS['mid']}"
    );

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($curl);
    error_log($output);

}

function api_get_user_profile_request($mid) {
    $url = "https://trialbot-api.line.me/v1/profiles?mids={$mid}";
    $headers = array(
        "X-Line-ChannelID: {$GLOBALS['channel_id']}",
        "X-Line-ChannelSecret: {$GLOBALS['channel_secret']}",
        "X-Line-Trusted-User-With-ACL: {$GLOBALS['mid']}"
    );

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($curl);
    error_log("=============================");
    error_log($output);
    error_log("=============================");
    return $output;
}

これは試しにやってみた感じ!
どんなデータが帰ってきてるとか
どんな風にしたら取れるかとか
わかったので満足!

参考にさせていただいたサイト
qiita.com

webページからbotに通知を送ってみた!

説明下手なので画像で!
やったことはこんな感じ!

f:id:music431per:20160504194516p:plain

ソースは

index.html

<!DOCTYPE html>
<html lang="ja">
<head>
  <title>eriを呼ぶ</title>
</head>
<body>
  <h1>eriにメッセージを送ってみる</h1>
  <form action = "[web.phpがあるurl]" method = "post">
    <input type = "text" name ="comment"></br>
    <input type = "submit" value ="送信">
  </form>
</body>
</html>

web.php

<?php

// アカウント情報設定
$channel_id = "[チャンネルID]";
$channel_secret = "[チャンネルシークレット]";
$mid = "[MID]";

$my_from = "[ユーザID]";

// コメント受け取り
if(isset($_POST['comment'])){
  $comment = $_POST['comment'];
}


$event_type = "138311608800106203";

$content = <<< EOM
        "contentType":1,
        "text":"webよりメッセージ「{$comment}」"
EOM;

$post = <<< EOM
{
    "to":["{$my_from}"],
    "toChannel":1383378250,
    "eventType":"{$event_type}",
    "content":{
        "toType":1,
        {$content}
    }
}
EOM;

api_post_request("/v1/events", $post);


function api_post_request($path, $post) {
    $url = "https://trialbot-api.line.me{$path}";
    $headers = array(
        "Content-Type: application/json",
        "X-Line-ChannelID: {$GLOBALS['channel_id']}",
        "X-Line-ChannelSecret: {$GLOBALS['channel_secret']}",
        "X-Line-Trusted-User-With-ACL: {$GLOBALS['mid']}"
    );

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($curl);
    error_log($output);
    redirect_func();
}

// リダイレクト
function redirect_func(){
  $url = '[入力するwebページへリダイレクト]';
  header("Location: {$url}");
  exit;
}

webからも案外簡単に送れてびっくり!
でもこれはbotにする意味もないよなーと思いながら。。。笑
この方法はできるけれどあまりやらないほうがいいのかも?

やってみてまとめ

botっていろいろできそうだけれど
botがいい!ってものはあまりないのかもしれません。
でも、コミュニケーションツールとして
lineなどのチャットツールは欠かせないものになっているので
この強みが生かせるものが何かしらあるんだろうな、、、と模索中です。
まだまだわからないこと多いので
遊んでいけたらなーと思います!

では!