I'm using a Ruby Gem for wit.ai, and have created an API in ROR that queries the wit.ai client.
But unexpectedly the method in the API keeps on calling the client method repeatedly, even after getting the response.
Because of which the API method never renders the JSON response.
How can I resolve this issue? Why is this happening?
It works perfectly fine if I do the same thing from Rails Console.
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
ApiModel
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
ChatbotHelper
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