9
6

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 APIで雑話を楽しんでみるAJAXサンプル

Last updated at Posted at 2015-05-11

はじめに

HTMLのサンプルが少なかったため、AJAXのサンプルをまとめました。

docomo APIとは

docomo Developer supportで提供しているAPI。
会員登録後、画像認識、音声合成、文字認識などいろいろ試せます。

API一覧はこちら

サンプル

HTML(jQueryを読み込んで、あとはお好きに)

<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>

JavaScript

var apiUrl = 'https://api.apigw.smt.docomo.ne.jp/dialogue/v1/dialogue?APIKEY=';
var apiKey = '<提供されたKey>';
var api = apiUrl + apiKey;

var apiJson = {
    'utt' : '' // ←ここに雑話キーワードを入れる。テキスト入力ができるといいかも
}

// API送受信関数
function callBot(){
    $.ajax({
        type : 'post',
        url : api,
        data : JSON.stringify(apiJson),
        contentType: 'application/JSON',
        dataType : 'JSON',
        scriptCharset: 'utf-8'
    })
    .done(function(data){
        console.log(data.yomi); // ←いろんな雑話が返ってくる
    })
    .fail(function(data){
        console.log(data.responseText); // ←エラー時の処理
    })
}

// あとは色んなタイミングで実行
callBot();
// 例:やっほー => やっほっほー
// ※毎回同じとは限らない。結構いろんな回答が返ってきます

感想

上記さえできてしまえば、いろいろカスタマイズできると思います。
例えば、HTML5 Web Speech APIを使用して、キーボード入力無しで雑話をできるチャットを作ったり。
(ちなみに、現在作成中)

他にも色んなAPIがあるので、複合しておもしろいアプリが作れますね!

9
6
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
9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?