ガチャつくブログ

IT系のお勉強のために、やってみたことをアウトプットしていきます。

MENU

Hubot で遊ぶ - AIを使って雑談してみる -

f:id:tmt-tty:20190221114212p:plain

 はじめに

『Hubot』を雑談できるボットに進化させたいと思います。

こんな感じに返してくれる様になります。

f:id:tmt-tty:20190221110050p:plain

 やり方 

『Hubot』を構築して、Slackと連携させている状態からスタートします。

以下の記事を参考にしてください。

www.gachatsuku.com

 

雑談用のAPIとHubotを連携させていきます。

雑談用のAPIは、リクルートテクノロジーが無償公開しているA3RTから、TalkAPIを使用します。

a3rt.recruit-tech.co.jp

 

PRODUCT > Talk API から APIKEYの発行をしておきます。

APIKEYはメールで連絡がきます。

 

f:id:tmt-tty:20190221110536p:plain

 

hubotにcoffee script を追加します。なんぞやという方にWikipediaのリンクを貼っておきます。

ja.wikipedia.org

 

スクリプト作成

cd [hubotディレクトリ]/scripts
vi talkapi.coffee


talkapi.coffee

request = require('request')
 
API_KEY = 'APIKEY'
endPoint = 'https://api.a3rt.recruit-tech.co.jp/talk/v1/smalltalk'
 
module.exports = (robot)->
 
#  robot.respond /(.*)/i, (res) ->
  robot.hear /(.*)/i, (res) ->
    text = res.match[1]
    console.log('Input text='+text)
 
    request.post(endPoint,
      { form: { apikey: API_KEY, query: text} },
      (error, response, body)->
        if (!error && response.statusCode == 200)
          console.log("Request success.")
          resObj = JSON.parse(body)
          console.log(resObj)
 
          res.reply resObj.results[0].reply
 
        else
          console.log("Request error.")
          resObj = JSON.parse(body)
          console.log(resObj)
 
          res.reply 'Request error.'
    )

 

@メンションした時だけ、反応してほしい場合は robot.respond /(.*)/i, (res) ->

会話にいちいち反応してほしい場合は robot.hear /(.*)/i, (res) ->

をアンコメントしてください。

 

Hubotが起動している場合は、再起動してください。

 

Slackで話すとこんな感じになります。流行りの”りんな”とかと比べると全然ですが。。。Docomoの雑談API(提供終了してしまった。。。)よりもひどい。。

 

f:id:tmt-tty:20190221111802p:plain

 

最後にボット同士の会話を。

SlackにHubotをもう1体召喚しました。

f:id:tmt-tty:20190221112230p:plain

ループ!!!

そこまで、臨機応変な雑談には対応できない様です。

なんか良い雑談APIないかなぁ。