Upgrade to Pro — share decks privately, control downloads, hide ads and more …

簡単!Slack BOT

okakam
November 28, 2016

簡単!Slack BOT

okakam

November 28, 2016
Tweet

Other Decks in Programming

Transcript

  1. コードを書く <?php require 'vendor/autoload.php'; require_once 'BotConfig.php'; date_default_timezone_set('Asia/Tokyo'); $loop = React¥EventLoop¥Factory::create();

    $client = new Slack¥RealTimeClient($loop); $client->setToken(BotConfig::SLACK_TOKEN_TEST); $client->on('message', function ($data) use ($client) { print_r("Someone typed a message: " . $data['text'] . " : " . $data['channel'] . "¥n"); $client->getChannelById($data['channel'])->then(function ($chanel) use ($client, $data) { $client->send($data['text'], $chanel); }); }); $client->connect()->then(function () { print_r('Connect !' . "¥n"); }); $loop->run(); この辺に Slackの画 面から取得した オウム返しのソースコード 入力された文字列を そのまま返す
  2. コードを書く2 ソースを $chat_api_url = 'https://chatbot-api.userlocal.jp/api/chat'; …. if (preg_match('/^(..' . BotConfig::SLACK_BOT_HASH

    . '.)(.*)/', $data['text'], $matches)) { $message = isset($matches[2]) ? $matches[2] : null; } if(empty($message)) { return; } $client->getChannelById($data['channel'])->then(function ($chanel) use ($client, $message) { global $chat_api_url; $query = [ 'key' => BotConfig::CHAT_TOKEN, 'message' => $message, 'bot_name' => 'RoboPipin', 'platform' => 'slack', ]; $result = file_get_contents($chat_api_url . '?' . http_build_query($query)); $resultArray = json_decode($result, true); if ($resultArray['status'] === 'success') { $response = $resultArray['result']; } else { $response = 'え?'; } $client->send($response, $chanel); …. Botに話しかけたと きだけ、返事を返す 雑談APIを呼ぶ