looking for a way to separate my chat routes and callbacks

17 views
Skip to first unread message

Aulbourn Knowles

unread,
May 14, 2020, 5:55:02 PM5/14/20
to Lita
Im doing a OOP project for school in which i have create a bot in ruby.  My dilemma is that i want to keep my routes in the
file generated when creating a new handler but move the response methods to a bin folder outside of the lib folder.
so for example my handler would be named dialog

the file structure would look like so


lita-dialog/lib/lita/handlers/dialog.rb



 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



this would house my routes, then  i would want  my responses to be in

lita-dialog/bin/main.rb


require_relative '../lib/lita/handlers/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


In testing when testing the routes to the responses they are passing but when i interact with the bot the bot does response when given keyword commands
that should trigger a response.

im getting this error when i run 'lita'
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>'
 




how could i fix this please.

Aulbourn Knowles

unread,
May 14, 2020, 10:22:22 PM5/14/20
to Lita
Hey Mike thanks 
I tried it but it didn’t work.  

--
You received this message because you are subscribed to the Google Groups "Lita" group.
To unsubscribe from this group and stop receiving emails from it, send an email to litaio+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/litaio/8322f195-5a57-4400-9545-752d2d18d929%40googlegroups.com.

Jimmy Cuadra

unread,
May 15, 2020, 6:00:53 AM5/15/20
to Aulbourn Knowles, Lita
Hi,

What is the reason for putting Lita code in this unusual location? You'll have an easier time if you stick to the standard structure. If you really can't reorganize your code...

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. I would recommend doing some reading on Ruby fundamentals to understand how classes, modules, and scopes work.

Hope this puts you on the right track!

Jimmy

Reply all
Reply to author
Forward
0 new messages