lita bot not responding to triggers

97 views Asked by At

I/m using Lita to create a chatbot, I have created a handle called dialog to where and I've opted to separate my code, i have my chat routes in lita-dialog/lib/lita/handlers/dialog.rb and my responses in lita-dialog/bin/main.rb.

the code in lita-dialog/lib/lita/handlers/dialog


 module Lita
  module Handlers
    class Dialog < Handler
      # insert handler code here
       route(/.*hello|hi|good morning|morning|welcome.*/i, :greeting_response, command: true)
      Lita.register_handler(self)
    end
  end
end

the code in lita-dialog/bin/main.rb

require_relative '../lib/dialog.rb'

 def greeting_response(response)
    greetingresponse = [
      'Hello',
      "Greetings, #{response.user.name}.",
      "Well hello there, #{response.user.name}.",
      "Hey #{response.user.name}, Hello!",
      "Good day, #{response.user.name}",
      'Hi.'
    ]

    response.reply(greetingresponse.sample)
 end

I'm using RSpec to test my code and the test is passing but I when I test out the bot to get a response I don't receive and response which is not expected

Type "exit" or "quit" to end the session.                                                                                                          │<rive/Microverse/Ruby/jaybot   lita-dialog/spec/lita/handlers/dialog_spec.rb                         ruby     utf-8     16% ☰    1/6  :  1
jaybot > jaybot hello                                                                                                                              │6 lines yanked
jaybot >                  

tests

require "spec_helper"

describe Lita::Handlers::Dialog, lita_handler: true do
  it { is_expected.to(route_command('hello').to(:greeting_response)) }
  it { is_expected.to_not(route('hello').to(:greeting_response)) }
end

this is the output that I'm getting when i run the bot within the handler folder

Passing 'id' command to redis as is; blind passthrough has been deprecated and will be removed in redis-namespace 2.0 (at /Users/aulbournknowles/.r│~                              │~
benv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/did_you_mean-1.3.0/lib/did_you_mean/core_ext/name_error.rb:8:in `inspect')                            │~                              │~
[2020-05-14 19:42:20 UTC] ERROR: Lita::Handlers::Dialog crashed. The exception was:                                                                │~                              │~
undefined method `greeting_response' for #<Lita::Handlers::Dialog:0x00007fad9b8ed988>                                                              │~                              │~
Full backtrace:                                                                                                                                    │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/lib/lita/callback.rb:31:in `public_send'                          │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/lib/lita/callback.rb:31:in `call'                                 │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/lib/lita/handler/chat_router.rb:97:in `dispatch_to_route'         │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/lib/lita/handler/chat_router.rb:82:in `block in dispatch'         │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/lib/lita/handler/chat_router.rb:72:in `map'                       │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/lib/lita/handler/chat_router.rb:72:in `dispatch'                  │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/lib/lita/robot.rb:69:in `block in receive'                        │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/2.6.0/set.rb:338:in `each_key'                                                               │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/2.6.0/set.rb:338:in `each'                                                                   │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/lib/lita/robot.rb:66:in `map'                                     │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/lib/lita/robot.rb:66:in `receive'                                 │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/lib/lita/adapters/shell.rb:94:in `block in run_loop'              │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/lib/lita/adapters/shell.rb:85:in `loop'                           │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/lib/lita/adapters/shell.rb:85:in `run_loop'                       │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/lib/lita/adapters/shell.rb:34:in `run'                            │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/lib/lita/robot.rb:80:in `run'                                     │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/lib/lita.rb:80:in `run'                                           │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/lib/lita/cli.rb:77:in `start'                                     │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/thor-1.0.1/lib/thor/command.rb:27:in `run'                                   │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/thor-1.0.1/lib/thor/invocation.rb:127:in `invoke_command'                    │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/thor-1.0.1/lib/thor.rb:392:in `dispatch'                                     │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/thor-1.0.1/lib/thor/base.rb:485:in `start'                                   │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/lita-4.7.1/bin/lita:6:in `<top (required)>'                                  │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/bin/lita:23:in `load'                                                                                 │~                              │~
/Users/aulbournknowles/.rbenv/versions/2.6.5/bin/lita:23:in `<main>'   
1

There are 1 answers

0
aulbytj On BEST ANSWER

Credit for this answer goes to Jimmy Cuadra

The Lita handler you showed defines a route that will invoke an instance method called greeting_response when an incoming message matches. There is no instance method with that name defined, hence the "undefined method" exception. You have a method with this name defined in the other file, but a method defined at the top-level scope is not the same thing as an instance method in a class. If you want that method to be included in the Lita handler class, you should define it in a module that you include in the handler.