我正在为 wit.ai 使用 Ruby Gem,并在 ROR 中创建了一个 API 来查询 wit.ai 客户端。
但出乎意料的是,API 中的方法不断重复调用客户端方法,即使在得到响应后也是如此。
因此,API 方法从不呈现 JSON 响应。
我该如何解决这个问题?为什么会这样?
如果我从 Rails 控制台做同样的事情,它工作得很好。
ApiController
module Api
module V0
class ApiController < ApplicationController
def response
q = params[:q]
response = Api::V0::ApiModel.handle_response q
render :json => response, :status => 200
end
end
end
end
接口模型
module Api
module V0
class ApiModel
def self.handle_response q
response = ChatbotHelper.query q
if response['type'] == "msg"
message = response["msg"]
json = {"message" => message}
else
json = response
end
json
end
end
end
end
聊天机器人助手
module ChatbotHelper
def self.init
actions = {
send: -> (request, response) {
puts "REQUEST #{request} RESPONSE #{response}"
puts("#{response['text']}")
response
},
getData: -> (context){
},
}
@client = Wit.new(access_token: "XYZ", actions: actions)
end
def self.query q
begin
self.init
response = self.get_response q
rescue SocketError
response = {"message": "SocketError"}
end
response
end
def self.get_response q
puts "GET RESPONSE"
response = @client.converse("b", q, {})
response
end
end