Lineãããã試ãã¦ã¿ã
å··ã§åã®ãã£ãããããã
ãã®ä¸ã§ãMicrosoftã¨Lineã試ãã¦ã¿ãã
MSのBot Frameworkã¯ãjsã§ããã¦ç°¡åã ã£ããã©ã³ã³ã½ã¼ã«ã§æ¢ã¾ã£ã¦ãã¾ã£ãã
ãªã®ã§ãããã§ã¯Lineã®BOT API Trialãè¨é²ãè¨è¼ãã¦ããã
Lineã®ãããã¯éå®10000人ã¨ãã«APIãéæ¾ãã¦ããã
https://business.line.me/services/products/4/introduction
éå®ã ãã©ãã¾ã ã¾ã åãä»ããã£ã±ãã«ãªãæ§åã¯ã¾ã ãªããã
ãã¸ãã¹ã¢ã«ã¦ã³ãã¨ãã¦ç»é²ãã¦ããã
è²ã
ã¨ç´°ããæ¸ããã¨æã£ããã©ãè¨é²ããªããã¦ãã¾ã£ã¦ããã®ã¨
ãã¡ãã®Qiitaã®è¨äºã®æ¹ãã¯ããã«ã¾ã¨ã¾ã£ã¦ããã®ã§ããã¡ããè²¼ã£ã¦ããã
http://qiita.com/Arashi/items/a1a6663d3fba0947815d
èªåããã³ã¢ã®éè«APIãå ¥ãã¦ããã®ã§ãæ§æçã«ã¯ä¸è¨ã®å½¢ã«ãªã
ã¦ã¼ã¶ <=> LINE <=> LINE_BOT_API <=> èªåã®ãµã¼ã <=> Docomo API
Docomoã®APIã¯ãªããªããããã¦ãéè«APIã質åAPIãªã©ãããã
ä»åã¯ãããã¹ãã«å¯¾ãã¦è³ªåAPIã«ã¢ã¯ã»ã¹ããåçããªããã°éè«APIãå©ãæµãã«ãã¦ã¿ãã
ã¡ãªã¿ã«ãLINEã®BOT APIããã¯ä¸è¨ã®formatãéããã¦ãã
stdClass Object ( [result] => Array ( [0] => stdClass Object ( [content] => stdClass Object ( [toType] => 1 [createdTime] => 1465532671092 [from] => xxxxxxxxxx [location] => [id] => xxxxxxxxxx [to] => Array ( [0] => xxxxxxxxxx ) [text] => \xe3\x81\xa6\xe3\x81\x99\xe3\x81\xa8 [contentMetadata] => stdClass Object ( [AT_RECV_MODE] => 2 [SKIP_BADGE_COUNT] => true ) [deliveredTime] => 0 [contentType] => 1 [seq] => ) [createdTime] => 1465532671110 [eventType] => 138311609000106303 [from] => xxxxxxxxxx [fromChannel] => xxxxxxxxxx [id] => xxxxxxxxxx [to] => Array ( [0] => xxxxxxxxxx ) [toChannel] => xxxxxxxxxx ) ) )
ãã®æ å ±ãå ã«ãã¼ã¿ãå¼ã£ãæãã¦ã´ãã§ãããã§ãã
<?php class ChatBot { private $_apiKey = 'DOCOMO_API_ACCESS_KEY'; private $_contextId = ''; public function __construct () { // LINEããhookãããã¼ã¿ãåå¾ $json_string = file_get_contents('php://input'); $jsonObj = json_decode($json_string); $to = $jsonObj->{"result"}[0]->{"content"}->{"from"}; $text = $jsonObj->{"result"}[0]->{"content"}->{"text"}; // 質åAPIã«æãã $response = $this->_getQuestion($text); if ($response === false) { // 質åã®åçããªããã°éè«ã«ãã $response = $this->_getDialog($text); } $this->_send($to, $response); } // 質åAPIã«åãåããã private function _getQuestion($text) { $query = "APIKEY=".$this->_apiKey."&q=".urlencode($text); $ch = curl_init("https://api.apigw.smt.docomo.ne.jp/knowledgeQA/v1/ask?$query"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); $answer = json_decode($result); if ($answer->{"code"} === "E020010") { return false; } $this->_contextId = ''; return $answer->{"message"}->{"textForDisplay"}; } // éè«APIã«åãåããã private function _getDialog($text) { $query = "APIKEY=".$this->_apiKey; $post_data = ["utt" => $text,]; if (empty($this->_contextId) === false) { $post_data["context"] = $this->_contextId; } $ch = curl_init("https://api.apigw.smt.docomo.ne.jp/dialogue/v1/dialogue?$query"); 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($post_data)); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charser=UTF-8')); $result = curl_exec($ch); curl_close($ch); $dialog = json_decode($result); $this->_contextId = $dialog->{"context"}; return $dialog->{"utt"}; } private function _send($to, $text) { // ããã¹ãã§è¿äºãããå ´å $response_format_text = [ 'contentType' => 1, "toType" => 1, "text" => $text ]; // toChannelã¨eventTypeã¯ãã¹ã¦ã®äººã§åºå®å¤ãå¤æ´ä¸è¦ã $post_data = [ "to"=>[$to], "toChannel" => "1383378250", "eventType" => "138311608800106203", "content" => $response_format_text ]; $ch = curl_init("https://trialbot-api.line.me/v1/events"); 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($post_data)); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charser=UTF-8', 'X-Line-ChannelID: xxxxx', // èªåã®ãã¤è¦ã¦ã 'X-Line-ChannelSecret: xxxxx', // èªåã®ãã¤è¦ã¦ã 'X-Line-Trusted-User-With-ACL: xxxxx' // èªåã®ãã¤è¦ã¦ã )); $result = curl_exec($ch); curl_close($ch); } } new ChatBot();
ããããã¨æçµçã«ãããªæãã®ä¼è©±ãã§ããããã«ã
ã¾ãã³ã³ããã¹ããè¨å®ãã¦ããªããããããããªãã¨ãå¤ããã©ã
ãªããªããåæã«ãè¿äºãè¿ã£ã¦ããã®ã¯å¬ããã¦æ¥½ããã
ä¼è©±ç³»ã®é¨åãããå°ãåå¼·ãã¦ã¿ããã¨æã£ãã