クイックスタート|Tropo Web API
タイムアウトを変更する
完全な質問をするための5つのステップで説明したように、Tropoでは、デフォルトで30秒間までユーザーの応答を待機します。
30秒間の代わりに、10秒間待つようにしたい場合は、
require 'tropo-webapi-ruby' require 'sinatra' post '/index.json' do t = Tropo::Generator.new t.ask :name => 'color', :timeout => 10, :attempts => 3, :say => {:value => "好きな色は何ですか?レッド、ブルー、グリーンの中から選んでください。"}, :choices => {:value => "red, blue, green"} t.on :event => 'continue', :next => '/continue.json' t.response end post '/continue.json' do v = Tropo::Generator.parse request.env["rack.input"].read t = Tropo::Generator.new answer = v[:result][:actions][:color][:value] t.say(:value => "選んだ色は、 " + answer) t.response end
var http = require('http'); var express = require('express'); var app = express.createServer(); var tropo_webapi = require('tropo-webapi'); // Required to process the HTTP body. // req.body has the Object while req.rawBody has the JSON string. app.configure(function(){ app.use(express.bodyParser()); }); app.post('/', function(req, res){ var tropo = new TropoWebAPI(); var say = new Say("好きな色は何ですか?レッド、ブルー、グリーンの中から選んでください。"); var choices = new Choices("red, blue, green"); // (choices, attempts, bargein, minConfidence, name, recognizer, required, say, timeout, voice); tropo.ask(choices, 3, null, null, "color", null, null, say, 10, null); tropo.on("continue", null, "/continue", true); res.send(TropoJSON(tropo)); }); app.post('/continue', function(req, res){ var tropo = new TropoWebAPI(); var answer = req.body['result']['actions']['value']; tropo.say("選んだ色は、 " + answer); res.send(TropoJSON(tropo)); }); app.listen(8000); console.log('Server running on port :8000');
<?php require 'tropo.class'; require 'lib/limonade'; dispatch_post('/start', 'app_start'); function app_start() { $tropo = new Tropo(); $options = array("choices" => "red, blue, green", "name" => "color", "attempts" => 3, "timeout" => 10); $tropo->ask("好きな色は何ですか?レッド、ブルー、グリーンの中から選んでください。", $options); $tropo->on(array("event" => "continue", "next" => "hello_world?uri=continue")); $tropo->RenderJson(); } dispatch_post('/continue', 'app_continue'); function app_continue() { $tropo = new Tropo(); @$result = new Result(); $answer = $result->getValue(); $tropo->say("選んだ色は、 " . $answer); $tropo->RenderJson(); } run(); ?>
from itty import * from tropo import Tropo, Result @post('/index.json') def index(request): t = Tropo() t.ask(choices = "red, blue, green", attempts=3, timeout=10, name="color", say = "好きな色は何ですか?レッド、ブルー、グリーンの中から選んでください。") t.on(event = "continue", next ="/continue") return t.RenderJson() @post("/continue") def index(request): r = Result(request.body) t = Tropo() answer = r.getValue() t.say("選んだ色は、 " + answer) return t.RenderJson() run_itty(server='wsgiref', host='0.0.0.0', port=8888)
上記では、Tropoは、質問は3回までしますが、それぞれの間は10秒しか待ちません。
次の章では、エラー処理について説明します。