litaio / lita

ChatOps for Ruby.
https://www.lita.io
MIT License
1.68k stars 179 forks source link

send_messages to a room does not work with hipchat #206

Open autumnw opened 7 years ago

autumnw commented 7 years ago

Hi,

I try to have Lita BOT to send_messages to a hipchat room, but never succeeded. Here are my code:

    def get_target_by_user(owner)
      user = Lita::User.find_by_name(owner)

      # Need to prevent the user does not exist in hipchat.
      return nil if user.nil?

      return Lita::Source.new(user: user)
    end

    def get_target_by_room(room)
      room = Lita::Room.find_by_name(room)

      # Need to prevent the user does not exist in hipchat.
      return nil if room.nil?

      robot.join(room)
      return Lita::Source.new(room: room)
    end

    def send_message_to_target(target, header, msg)
      log.info("sending message to #{target.inspect}")
      robot.send_messages(target, header)
      robot.send_messages(target, msg)
    end

    def send_message(owner, cc, header, msg)
      receivers = [owner]
      receivers += cc if not cc.nil?
      receivers.each do |owner|
        begin
          target = get_target_by_user(owner)
          if target.nil?
            target = get_target_by_room(owner)
          end

          next if target.nil? # skip if user does not exist in hipchat.

          send_message_to_target(target, header, msg)
        rescue StandardError => e
          log.error("Error when sending message to #{target}: #{e}")
        end
      end
    end
    header = "(failed) Test"
        msg = "Test message\n"
        config.notify_to.each do |owner|
          send_message(owner, nil, header, msg)
        end

And the log likes the following: We can see it sent to the user successfully, but did not send message to the room. So what could be the problem?

[] [DEBUG] Already in room with JID 599001_devtestroom@conf.hipchat.com
[] [INFO] sending message to #<Lita::Source:0x007fe42d4648f8 @user=nil, @room="599001_devtestroom", @room_object=#<Lita::Room:0x007fe42d466018 @id="599001_devtestroom", @metadata={"name"=>"599001_devtestroom"}, @name="599001_devtestroom">, @private_message=false>
[] [INFO] sending message to #<Lita::Source:0x007fe42d45dbe8 @user=#<Lita::User:0x007fe42d45dcd8 @id="599001_4263932@chat.hipchat.com", @metadata={"mention_name"=>"AutumnWang", "email"=>"autumn.wang@illumio.com", "name"=>"Autumn Wang"}, @name="Autumn Wang">, @private_message=true>
[] [DEBUG] Sending message to JID 599001_4263932@chat.hipchat.com: (failed) Test
[] [DEBUG] Sending message to JID 599001_4263932@chat.hipchat.com: Test message
autumnw commented 7 years ago

I found the problem lies in room ID: In redis, the room ID missed the domain name "@chat.hipchat.com", while user ID is correct. That is why user works, but Room does not work. In hipchat, the Room ID does include the domain name "XMPP JID 669627_self-serv@conf.hipchat.com".

irb(main):024:0> redis.keys('*lita:rooms:*')
=> ["lita:rooms:name:669627_self-serv", "lita:rooms:name:599001_ilo-bot-testing", "lita:rooms:name:599001_self-serv", "lita:rooms:id:599001_self-serv", "lita:rooms:id:669627_self-serv", "lita:rooms:id:599001_devtestroom", "lita:rooms:name:669627_awang-test", "lita:rooms:id:669627_awang-test", "lita:rooms:id:599001_ilo-bot-testing", "lita:rooms:name:599001_devtestroom"]
irb(main):025:0> redis.keys('*lita:rooms:id*')
=> ["lita:rooms:id:599001_self-serv", "lita:rooms:id:669627_self-serv", "lita:rooms:id:599001_devtestroom", "lita:rooms:id:669627_awang-test", "lita:rooms:id:599001_ilo-bot-testing"]
irb(main):026:0> redis.keys('*lita:users:id*')
=> ["lita:users:id:599001_4074394@chat.hipchat.com", "lita:users:id:599001_4074736@chat.hipchat.com", "lita:users:id:599001_4074915@chat.hipchat.com", "lita:users:id:599001_4074451@chat.hipchat.com", "lita:users:id:599001_4074727@chat.hipchat.com", "lita:users:id:599001_4080057@chat.hipchat.com", "lita:users:id:599001_4675343@chat.hipchat.com", "lita:users:id:599001_4263932@chat.hipchat.com", "lita:users:id:599001_4075411@chat.hipchat.com", "lita:users:id:669627_4681583@chat.hipchat.com", "lita:users:id:599001_4267255@chat.hipchat.com", "lita:users:id:669627_4681600@chat.hipchat.com", "lita:users:id:669627_4681881@chat.hipchat.com", "lita:users:id:599001_4074731@chat.hipchat.com", "lita:users:id:599001_4193374@chat.hipchat.com", "lita:users:id:599001_4290462@chat.hipchat.com", "lita:users:id:599001_4190557@chat.hipchat.com", "lita:users:id:599001_4434091@chat.hipchat.com", "lita:users:id:599001_4074735@chat.hipchat.com"]