我如何使用Lita.io创建简单问卷?

时间:2017-04-06 14:42:17

标签: ruby chatbot lita

我尝试在丽塔实施一个小问卷作为样本:

  

您想打开哪个系统?

     

SYSInitials

     

你的问题是什么?

     

我忘记了密码

     

谢谢!你的电话已经开通了!

我可以帮忙怎么做?

所以,我试试这个:

module Lita
  module Handlers
    class Helpdesk < Handler
      on :shut_down_complete, :clear_context

      route(/^abrir chamado$/i, :abrir_chamado)
      route(/^.*$/i, :motivo)
      http.get '/info', :web

      def motivo(response)
        return unless context == 'abrir_chamado'
        response.reply('Thanks! Your call was opened!')
        clear_context
      end

      def abrir_chamado(response)
        redis.set(:context, :abrir_chamado)
        user = response.user
        response.reply(
          %(Hello #{user.name}, What is your problem?)
        )
      end

      def context
        @contetx ||= redis.get(:context)
      end

      def clear_context
        redis.del(:context)
      end

      Lita.register_handler(Helpdesk)
    end
  end
end

但是当我注册时,:informar_motivo route在通过:abrir_chamado route后,也会匹配:informar_motivo路线。

lita dialog flux

但我需要:

  

me: abrir chamado

     

Lita: Hello Shell用户,您有什么问题?

     

我:我忘记了密码

     

丽塔:谢谢!你的电话已经开通了!

1 个答案:

答案 0 :(得分:0)

我找到了一个丑陋的解决方案,但有效:P

module Lita
  module Handlers
    class Helpdesk < Handler
      on :shut_down_complete, :clear_context
      on :unhandled_message, :motivo

      route(/^abrir chamado$/i, :abrir_chamado)

      http.get '/info', :web

      def motivo(payload)
        response = payload[:message]
        return unless context == 'abrir_chamado'
        response.reply('Thanks! Your call was opened!')
        clear_context
      end

      def abrir_chamado(response)
        redis.set(:context, :abrir_chamado)
        user = response.user
        response.reply(
          %(Hello #{user.name}, What is your problem?)
        )
      end

      def context
        @contetx ||= redis.get(:context)
      end

      def clear_context
        redis.del(:context)
      end

      Lita.register_handler(Helpdesk)
    end
  end
end

enter image description here