lita 机器人不响应触发器
lita bot not responding to triggers
I/m 使用 Lita 创建聊天机器人,我创建了一个名为 dialog to where 的句柄,我选择将我的代码分开,我在 lita-dialog/lib/lita/handlers/dialog.rb
中有我的聊天路线和我的回复在 lita-dialog/bin/main.rb
。
代码在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
代码在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
我正在使用 RSpec 来测试我的代码并且测试通过了,但是当我测试机器人以获得响应时我没有收到响应,这不是预期的
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 >
测试
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
这是我在 运行 处理程序文件夹中的机器人时得到的输出
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>'
此答案归功于 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.
I/m 使用 Lita 创建聊天机器人,我创建了一个名为 dialog to where 的句柄,我选择将我的代码分开,我在 lita-dialog/lib/lita/handlers/dialog.rb
中有我的聊天路线和我的回复在 lita-dialog/bin/main.rb
。
代码在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
代码在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
我正在使用 RSpec 来测试我的代码并且测试通过了,但是当我测试机器人以获得响应时我没有收到响应,这不是预期的
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 >
测试
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
这是我在 运行 处理程序文件夹中的机器人时得到的输出
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>'
此答案归功于 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.