8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LINE BOTを使用してユーザー情報を取得する

Last updated at Posted at 2016-10-05

##このBOTについて

この投稿はDeveloper Trialの内容です。
正式版として公開されたLINE MESSAGE APIを使用した実装は
LINE MESSAGE APIを使用してユーザー情報を取得するに公開致しました。

無料で使えるLINE BOTを使ってユーザー情報を取り出す必要があったので、その際のメモです。

事前にLINE BUSINESS CENTERで登録を完了しておいてください。
コールバックURLにはsslが必須のようですが無料で使えるレンタルサーバー共有sslやHerokuでもできました。

##ユーザー名を取得して送り返すbot


new Bot;

class Bot{
  private $channel_id = "[ユーザーのCHANNEL ID]";
  private $channel_secret = "[ユーザーのCHANNEL SECRET]";
  private $mid = "[ユーザーのMID]";
  private $header;

  private $from;
  private $text;
  private $content_type;

  private $name;

  public function __construct(){

    $json_string = file_get_contents('php://input');
    $receive = json_decode($json_string);
    $this->from = $receive->result{0}->content->from;
    $this->text = $receive->result{0}->content->text;
    $this->content_type = $receive->result[0]->content->contentType;
   
    $this->header = array(
      "Content-Type: application/json; charser=UTF-8",
      "X-Line-ChannelID:" . $this->channel_id,
      "X-Line-ChannelSecret:" . $this->channel_secret,
      "X-Line-Trusted-User-With-ACL:" . $this->mid,
    );

    $this->getting_user_profile_rinformation($this->from);
    $this->sending_messages($this->name);

  }

  private function sending_messages($message){
    $url = "https://trialbot-api.line.me/v1/events";

    $data = array("to" => array($this->from), "toChannel" => 1383378250, "eventType" => "138311608800106203", "content" => array("contentType" => 1, "toType" => 1, "text" => $message));
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
    $result = curl_exec($ch);
    curl_close($ch);
  }

  private function getting_user_profile_rinformation($mid) {
    $url = "https://trialbot-api.line.me/v1/profiles?mids={$mid}";

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($curl);
    $receive = json_decode($output);
    error_log($output);

    $this->name = $receive->contacts[0]->displayName; //displayName=>名前 pictureUrl=>プロフィール画像 statusMessage=>自己紹介文
    $this->name .= "さんこんにちは!";

    file_put_contents("json.php", $output);
  }
}

8
7
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
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?