1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

docomoの雑談対話を試す

Last updated at Posted at 2018-08-15

既に試している記事がいくつかあったのですが、
それらは古いバージョンで動作しなかったため、新たに作成しました。
リファレンスは末尾に記載してあります。

経緯

  • 2018年6月下旬にdocomoの雑談対話APIが終了し、自然対話APIとなった。
  • 自然対話APIの中の機能の一つに雑談対話があり、こちらがこれまでの雑談対話APIの後継と位置付けられそう。

前提

参考画像
APIの申請・管理___docomo_Developer_support___NTTドコモ.png

サンプルプログラム

python3.6.5 で動作を確認しました。

# -*- coding: utf-8 -*-

import requests
import json
from datetime import datetime

class DocomoZatsudan:
    REGISTER_KEY = '【取得したAPIKEY】'

    def __init__(self, sex="", t=""):
        # オプション指定(口調)
        self.set_option(sex, t)

        # ユーザ登録
        self.register_user()

    def register_user(self):
        # エンドポイントの設定
        r_url = 'https://api.apigw.smt.docomo.ne.jp/naturalChatting/v1/registration?APIKEY=REGISTER_KEY'
        d_url = 'https://api.apigw.smt.docomo.ne.jp/naturalChatting/v1/dialogue?APIKEY=REGISTER_KEY'
        self.r_url = r_url.replace('REGISTER_KEY', DocomoZatsudan.REGISTER_KEY)
        self.d_url = d_url.replace('REGISTER_KEY', DocomoZatsudan.REGISTER_KEY)

        # ユーザ登録のパラメータを代入
        payload = {'botId':'Chatting', 'appKind':'Chat'} #appKindは任意の文字列
        headers = {'Content-type' : 'application/json;charset=UTF-8'}

        # 送信
        r = requests.post(self.r_url, data=json.dumps(payload), headers=headers)
        data = r.json()
        self.appId = data['appId']  # appIdを取得

    def chat(self, user_input):
        # 日付を取得
        # ※サンプルなので現在の時刻を指定している。
        now_datetime = datetime.now().strftime("%Y-$m-$d %H:%M:%S")

        # 自然対話のパラメータを代入
        payload = {'language':'ja-JP', 'botId':'Chatting', 'appId':self.appId,
            'voiceText':user_input, 'appRecvTime':now_datetime, 'appSendTime':now_datetime,
            'clientData':self.option}
        headers = {'Content-type': 'application/json'}

        # 送信
        r = requests.post(self.d_url, data=json.dumps(payload), headers=headers)
        data = r.json()

        # jsonの解析
        response = data['systemText']['expression']

        return response

    def set_option(self, sex="", t=""):
        self.option = {'option': {'sex':sex, 't':t}}

if __name__ == '__main__':
    docomo_zatsudan = DocomoZatsudan()
#   docomo_zatsudan = DocomoZatsudan(sex="女", t="kansai")

    print("【Enterでプログラム終了】")

    while True:
        # 会話の入力
        user_input = input('>> ')
        if user_input is not '':
            print("response: %s" %(docomo_zatsudan.chat(user_input)))
        else:
            exit()

サンプルプログラムの補足説明

DocomoZatsudanクラスのコンストラクタ引数について

以下を指定できる。
他にもいくつかのパラメータを設定できる。
詳しくは 公式のリファレンス を参照してください。

  • sex:
  • t:
    • kansai:関西弁キャラ
    • akachan:赤ちゃんキャラ
    • 指定なし:デフォルトキャラ

改変箇所

REGISTER_KEY = '【取得したAPIKEY】' という箇所がありますので、
そちらに取得したAPIKEYを入力してください。

ユーザ登録

  • ユーザ登録することで、appIdを取得します。
    • appIdはユーザ毎の会話のステータスを管理するのに用いられています。

リファレンス

公式の情報

参考にした記事

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?