0

我/正在使用 Lita 创建一个聊天机器人,我创建了一个名为 dialog 的句柄到 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.                                                                                                          │&lt;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>'   
4

1 回答 1

0

这个答案归功于 Jimmy Cuadra

greeting_response您展示的 Lita 处理程序定义了一个路由,该路由将在传入消息匹配时调用一个实例方法。没有定义该名称的实例方法,因此出现“未定义方法”异常。您在另一个文件中定义了具有此名称的方法,但在顶级范围定义的方法与类中的实例方法不同。如果您希望该方法包含在 Lita 处理程序类中,您应该在包含在处理程序中的模块中定义它。

于 2020-05-15T18:13:53.780 回答