LoginSignup
9
10

More than 5 years have passed since last update.

とりあえずLINE BOT APIうごかす

Last updated at Posted at 2016-04-13

世界的ですもんね
乗るしかない このビックウェーブに

今夏?にBOT STOREオープンするらしいので
いまのうちに把握しておきたいということで。

ざっと見た感じ気をつける点は

  • SSL
  • コールバックURLがなかなか認識されない
  • WhiteList

ミニバード で使えたので
ネットオウル系のレンタルサーバーの共有SSLは使えるようです。

前述の通り、
コールバックURLがなかなか認識されず、ログも取れないので
最初に固定IPオプションで試しましたが結局できるかどうか不明。

LINE BOT APIでリクエストが飛んでこない(SSL関係)
上記で情報がまとめられているので参考に。

ベースのコード

class LINE_BOT {
    private $end_point      = 'https://trialbot-api.line.me';
    private $channel_id     = '***';
    private $channel_secret = '***';
    private $mid            = '***';
    private $event_type     = '138311608800106203';
    private $to_channel     = '1383378250';
    private $result;
    private $content;
    private $post;
    function post($path) {
        $curl = curl_init("{$this->end_point}{$path}");
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($this->post));
        curl_setopt($curl, CURLOPT_HTTPHEADER, [
            "Content-Type: application/json; charset=UTF-8",
            "X-Line-ChannelID: {$this->channel_id}",
            "X-Line-ChannelSecret: {$this->channel_secret}",
            "X-Line-Trusted-User-With-ACL: {$this->mid}"
        ]);
        $output = curl_exec($curl);
        curl_close($curl);
        //error_log($output);
    }
    function set() {
        $text = '';
        switch ( $this->result['eventType'] ) {
            case '138311609000106303'://受信
                $text = 'ンゴ?';
                break;
            default:
                $text = 'おうふ';
                break;
        }
        $this->content = [
            'toType' => 1,
            'contentType' => 1,
            'text' => $text,
        ];
        $this->post = [
            'to' => [$this->result['content']['from']],
            'toChannel' => $this->to_channel,
            'eventType' => $this->event_type,
            'content' => $this->content,
        ];
    }
    function get() {
        $json  = file_get_contents('php://input');
        $data = json_decode($json, true);
        $this->result = $data['result'][0];
    }
}
$LB = new LINE_BOT();
$LB->get();
$LB->set();
$LB->post('/v1/events');

友達追加時にもメッセージ送りたいけどeventTypeが取れないのでそのうち探る。

ブロック解除時と同じみたい。
ただ友達削除して再び追加しても取れないので新規のみか。

受信時以外の動き

メッセージ受信時以外でメッセージ送る場合。

  • ブロック時

$this->result['content']['opType']
8 の場合。
もちろんブロックされているのでメッセージは送れない。
ただ内部的になにかしら処理することは可能なもよう。

  • ブロック解除時

$this->result['content']['opType']
4 の場合。

メッセージ受信時は
$this->result['content']['opType']
はないのと中身も違うのでisset()で判定か。

一方的にメッセージを送る

コールバックを叩くだけで動く。
この場合は最初の
file_get_contents('php://input')
がnullなんでそれで判定。

友達一覧

/v1/profilesでBotのmid指定したけど友達一覧は取れない。
メッセージ受信時のfromを都度保持しておくしかないのか・・?

function addFriend($mid) {
    $friends = $this->getFriends();
    if ( !in_array($mid, $friends) ) {//新規の場合追加
        $friends[] = $mid;
        file_put_contents($this->friends_filepath, json_encode($friends));
    }
}
function getFriends() {
    $json = file_get_contents($this->friends_filepath);
    return json_decode($json, true);
}

こんなかんじで。
友達追加時のイベント取れればいいけどいまだに取れず。

追記

友達追加時のイベントもどうやらブロック解除と同じみたい。
ただ一旦削除して、その後追加しても取れないので
新規の場合しか取れないと思われ。

プロフィール取得

/v1/profilesの結果

contacts: [
     {
         displayName: *名前* (string)
         ,mid: *mid* (string)
         ,pictureUrl: *プロフィール写真*(string)
         ,statusMessage: *ステータス* (string)
    }
]

早く1アカウントで複数Botが作れるようにならんかの

9
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
10