How I create a simples questionnaire with Lita.io?

130 Views Asked by At

I try implementing a little questionnaire in Lita as the sample:

For which system do you want to open a call?

SYSInitials

What's your problem?

I forgot my password

Thanks! Your call was opened!

Any help how I can do this?

So, I'm try this:

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

But when I register, :informar_motivo route, after passing of the :abrir_chamado route, is matched :informar_motivo route too.

lita dialog flux

but I need:

me: abrir chamado

Lita: Hello Shell User, What is your problem?

me: I forgot my password

Lita: Thanks! Your call was opened!

1

There are 1 best solutions below

0
On

I found a ugly solution, but works :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