はじめに
HTMLのサンプルが少なかったため、AJAXのサンプルをまとめました。
docomo APIとは
docomo Developer supportで提供している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があるので、複合しておもしろいアプリが作れますね!