class Lita::Handlers::Talk
Constants
- API_DIALOGUE_PATH
- API_ENDPOINT
@see: dev.smt.docomo.ne.jp/?p=docs.api.page&api_name=natural_dialogue&p_name=api_4#tag01 @see: dev.smt.docomo.ne.jp/?p=docs.api.page&api_name=natural_dialogue&p_name=api_4_user_registration#tag01
- API_REGISTRATION_PATH
- CHARACTER_TYPES
@see: dev.smt.docomo.ne.jp/?p=docs.api.page&api_name=natural_dialogue&p_name=api_6#tag01
Public Instance Methods
show_type(response)
click to toggle source
# File lib/lita/handlers/talk.rb, line 51 def show_type(response) response.reply character_type end
talk(response)
click to toggle source
# File lib/lita/handlers/talk.rb, line 45 def talk(response) return unless command_missing?(response) response.reply create_dialogue(response) end
update_character_type(response)
click to toggle source
# File lib/lita/handlers/talk.rb, line 55 def update_character_type(response) type = response.matches.dig(0, 0) if CHARACTER_TYPES.include?(type) redis.set('character_type', type) response.reply 'ok' else response.reply("Invalid type: #{type}.\nValid types: #{CHARACTER_TYPES.join(', ')}") end end
Private Instance Methods
all_handler_routes()
click to toggle source
# File lib/lita/handlers/talk.rb, line 111 def all_handler_routes robot.handlers.map do |handler| next nil unless handler.respond_to?(:routes) next handler.routes.slice(1..-1) if handler == self.class handler.routes end.flatten.compact end
character_type()
click to toggle source
# File lib/lita/handlers/talk.rb, line 68 def character_type redis.get('character_type') || 'default' end
client_data()
click to toggle source
# File lib/lita/handlers/talk.rb, line 72 def client_data type = character_type return {} if type == 'default' { option: { t: type } } end
command_missing?(response)
click to toggle source
# File lib/lita/handlers/talk.rb, line 102 def command_missing?(response) all_handler_routes.each do |route| next unless route.command return false if response.matches.flatten[0].match(route.pattern) end true end
context_key(response)
click to toggle source
# File lib/lita/handlers/talk.rb, line 148 def context_key(response) if Lita.config.robot.adapter == :twitter "lita-talk:#{response.message.user.id}" else "lita-talk:#{response.message.source.room}" end end
create_dialogue(response)
click to toggle source
# File lib/lita/handlers/talk.rb, line 83 def create_dialogue(response) context = fetch_context(response) input_text = response.matches.dig(0, 0) resp = faraday.post do |req| req.url API_DIALOGUE_PATH req.body = { language: 'ja-JP', appId: context, voiceText: input_text, botId: 'Chatting', clientData: client_data } end resp.body.dig('systemText', 'expression') end
faraday()
click to toggle source
# File lib/lita/handlers/talk.rb, line 120 def faraday @faraday ||= Faraday.new(API_ENDPOINT) do |conn| conn.request :json conn.headers['Content-Type'] = 'application/json;charset=UTF-8' conn.params['APIKEY'] = config.docomo_api_key conn.response :json conn.adapter Faraday.default_adapter end end
fetch_context(response)
click to toggle source
# File lib/lita/handlers/talk.rb, line 130 def fetch_context(response) key = context_key(response) context = redis.get(key) return context if context resp = faraday.post do |req| req.url API_REGISTRATION_PATH req.body = { botId: 'Chatting', appKind: 'bot' } end context = resp.body['appId'] redis.set(key, context) context end