LoginSignup
2
4

More than 5 years have passed since last update.

【Windows版】SlackのhubotでAIを使って雑談会話を行う。

Last updated at Posted at 2017-09-18

会社でコミュニケーションツールとしてSlackを使っているのですが、AIを使って会話ができたら楽しいんじゃないかと思って導入してみました。またWindowsでの手順を書いたものが少なかったので残しておきます。

追記(2018.12.12)

「2018年6月末をもちまして機能提供を終了いたしました。」とのことです。残念ですね。

環境

Windows10(node,Git,Heroku CLIがインストール済)
Heroku
Slack

Slackでhubotを導入

色々なところで書かれているので今回は省略します。以下が参考になるのではないでしょうか。
SlackでHUBOTさんに働いてもらう方法 [Windows]

docomo雑談対話APIの登録

今回はdocomoの雑談対話APIを使用します。APIを使うにはユーザの登録(無料)を行う必要があります。以下から登録を行い新規でAPIを発行します。なお後ほどAPIキーは使うのでメモ帳などにコピーしておきます。
https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_name=dialogue&p_name=api_usage_scenario
※詳しい手順はこちらがとても参考になります。

hubot-docomo-dialogueのインストール

コマンドプロンプトを開き、自分が作成したhubotのルートフォルダまで移動し、hubot-docomo-dialogueをインストールします。

> npm install https://github.com/bouzuya/hubot-docomo-dialogue/archive/master.tar.gz
:入ったか確認
> npm ls --depth=0 | find "docomo"
npm ERR! extraneous: hubot-docomo-dialogue@1.1.1 C:\Users\naoto\Dropbox\dev\ebis\node_modules\hubot-docomo-dialogue
+-- hubot-docomo-dialogue@1.1.1 extraneous

npm ERR! extraneousと出ていますが、これはpackage.jsonに対象のパッケージが追加されてないので以下を追加します。

package.json
(省略)
"dependencies": {
    "hubot": "^2.19.0",
    (省略)
    "hubot-slack": "^4.4.0",          ←「,」を追加
    "hubot-docomo-dialogue": "https://github.com/bouzuya/hubot-docomo-dialogue/archive/master.tar.gz" ←追加
  },
(省略)

coffee-scriptの設定

scriptフォルダにdocomoapi.coffeeを追加し、内容を以下とします。こちらを参考としました。

docomoapi.coffee
# Description:
#   DOCOMOの雑談APIを利用した雑談
#
# Author:
#   FromAtom

getTimeDiffAsMinutes = (old_msec) ->
  now = new Date()
  old = new Date(old_msec)
  diff_msec = now.getTime() - old.getTime()
  diff_minutes = parseInt( diff_msec / (60*1000), 10 )
  return diff_minutes

module.exports = (robot) ->
  robot.respond /(\S+)/i, (msg) ->
    DOCOMO_API_KEY = process.env.DOCOMO_API_KEY
    message = msg.match[1]
    return unless DOCOMO_API_KEY && message

    ## ContextIDを読み込む
    KEY_DOCOMO_CONTEXT = 'docomo-talk-context'
    context = robot.brain.get KEY_DOCOMO_CONTEXT || ''

    ## 前回会話してからの経過時間調べる
    KEY_DOCOMO_CONTEXT_TTL = 'docomo-talk-context-ttl'
    TTL_MINUTES = 20
    old_msec = robot.brain.get KEY_DOCOMO_CONTEXT_TTL
    diff_minutes = getTimeDiffAsMinutes old_msec

    ## 前回会話してから一定時間経っていたらコンテキストを破棄
    if diff_minutes > TTL_MINUTES
      context = ''

    url = 'https://api.apigw.smt.docomo.ne.jp/dialogue/v1/dialogue?APIKEY=' + DOCOMO_API_KEY
    user_name = msg.message.user.name

    request = require('request');
    request.post
      url: url
      json:
        utt: message
        nickname: user_name if user_name
        context: context if context
      , (err, response, body) ->
        ## ContextIDの保存
        robot.brain.set KEY_DOCOMO_CONTEXT, body.context

        ## 会話発生時間の保存
        now_msec = new Date().getTime()
        robot.brain.set KEY_DOCOMO_CONTEXT_TTL, now_msec

        msg.send body.utt

コマンドプロンプト上で確認

bin/hubot.cmdで実行して、対話できるか確認してみます。
image.png
ちゃんとできてますね、距離感が近いですがw
ちなみに私のhubotの名前はebisです。

herokuの設定

herokuのconfigに今回追加したAPIをコマンドプロンプトで追加します。

> heroku login
> heroku config:set DOCOMO_API_KEY=[追加したDOCOMOのAPIキー]
> git add .
> git commit -m "docomoの雑談対話APIの追加"
> git push heroku master

slackで確認

slackでhubotに話かけてみます。
image.png
あれ?反応してくれませんね。
コマンドプロンプトでherokuのログを確認します。

> heroku logs
(省略)
2017-09-18T13:11:34.347786+00:00 app[web.1]: [Mon Sep 18 2017 22:11:34 GMT+0900 (JST)] ERROR Error: Cannot find module 'request'
(省略)

なるほど、requestモジュールがないと。package.jsonに追加して再度git add/commit/pushして再度、話かけます。
image.png
今度はちゃんとできましたね。

参照
[github謹製hubot]×[docomo雑談対話API]×[あんずちゃん]x slackが社内で愛されたbotのお話

2
4
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
2
4