概要
Hubotでは雑談、何かのアクション、情報の取得等様々なことをAPIを利用して実装することができます。
例えば今回はdocomoの自然対話で雑談、OpenWeatherMapで天気の取得を実装します。
・雑談BOT(docomo自然対話)
・天気取得BOT(OpenWeatherMap)
hubot実装については以下になります。
yunabe.hatenablog.com
雑談BOTの作成
APIトークンを取得する
docomoのAPIを利用するため、ユーザ登録を行います。
| docomo Developer support | NTTドコモ
クライアントキーとシークレッドキーを取得する
ユーザ登録をしたあとサイトでクライアントキーとシークレッドキーとAPIキーを確認する。
先ほど取得したAPIキーを使ってappIdを取得する
curlコマンドでもいいですが、postmanを使うとGUIで簡単にAPIを叩くことができます。
https://api.apigw.smt.docomo.ne.jp/naturalChatting/v1/registration?APIKEY=<apiKey>
appIdが返却されるので、メモをしておきます。
Hubotの変数にキーを記載します。
cd myhubot
vi bin/hubot
#docomo-API-Token export HUBOT_DOCOMO_DIALOGUE_APPID="xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxx-xxxxxxxx" export HUBOT_DOCOMO_DIALOGUE_API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
coffeeスクリプトを作成する
vi scripts/docomo-chat.coffee
module.exports = (robot) -> status = {} robot.respond /(\S+)/i, (msg) -> message = msg.match[1] HUBOT_DOCOMO_DIALOGUE_API_KEY = process.env.HUBOT_DOCOMO_DIALOGUE_API_KEY HUBOT_DOCOMO_DIALOGUE_APPID = process.env.HUBOT_DOCOMO_DIALOGUE_APPID url = 'https://api.apigw.smt.docomo.ne.jp/naturalChatting/v1/dialogue?APIKEY='+HUBOT_DOCOMO_DIALOGUE_API_KEY headers = {'Content-Type':'application/json'} d = new Date() appSendTime = d.getFullYear() + '-' + ('0' + (d.getMonth() + 1)).slice(-2) + '-' + ('0' + d.getDate()).slice(-2) + ' '\ + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() request = require('request'); request.post url: url headers: headers json: language: "ja-JP" botId: "Chatting" appId: HUBOT_DOCOMO_DIALOGUE_APPID voiceText: message #clientData: {"option": {"mode":"dialog","t":"akachan"}} clientData: {"option": {"mode":"dialog","t":"kansai"}} appRecvTime: status['appRecvTime'] appSendTime: appSendTime , (err, response, body) -> msg.send body.systemText.expression status = "appRecvTime": body.serverSendTime
スクリプトを作成したあとに再起動をかけます
bin/hubot restart
天気取得BOT
openweathermapをユーザの登録をしappIdを取得する
openweathermap.org
取得したキーを以下のスクリプトに記載します、今回以下のスクリプト2つを使ってTokyoの天気を取得します。
今日の天気の詳細をopenwether APIを使って取得する
vi scripts/open-weather.coffee
module.exports = (robot) -> send = (msg) -> request = robot.http("http://api.openweathermap.org/data/2.5/weather?q=Tokyo,jp&appid=<取得したappIdを記載する>&units=metric").get() stMessage = request (err, res, body) -> json = JSON.parse body weatherName = json['weather'][0]['main'] icon = json['weather'][0]['icon'] temp = json['main']['temp'] temp_max = json['main']['temp_max'] temp_min = json['main']['temp_min'] msg.send "気温:"+ temp + "℃ 最高気温:" + temp_max+ "℃ 最低気温:" + temp_min + "℃\nhttp://openweathermap.org/img/w/" + icon + ".png" robot.hear /今日の天気/, (msg) -> send(msg)
明日と明後日の天気を取得する
vi scripts/livedoor-weather.coffee
エリアのcity部分は以下で調べられます、ちなみに今回は270000で東京になっています。
weather.livedoor.com
module.exports = (robot) -> robot.hear /(.*)の天気/i, (msg) -> switch msg.match[1] when '今日' day = 0 agenda = "今日の東京の天気は" when '明日' day = 1 agenda = "明日の東京の天気は" when '明後日' day = 2 agenda = "明後日の東京の天気は" else day = 3 agenda = "うーん、" break request = msg.http('http://weather.livedoor.com/forecast/webservice/json/v1?city=270000') .get() request (err, res, body) -> json = JSON.parse body if day == 3 then forecast = '今日/明日/明後日のどれかにして欲しいん' else forecast = json['forecasts'][day]['telop'] msg.send agenda + forecast + "だよー"
起動しているデーモンを確認
forever list
デーモンプロセスを停止
forever stop 0
スクリプトを記載したらまた再起動をします
bin/hubot restart