Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

send_messages to a room does not work with hipchat #206

Open
autumnw opened this issue Jan 4, 2017 · 1 comment
Open

send_messages to a room does not work with hipchat #206

autumnw opened this issue Jan 4, 2017 · 1 comment

Comments

@autumnw
Copy link

autumnw commented Jan 4, 2017

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 [email protected]
[] [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="[email protected]", @metadata={"mention_name"=>"AutumnWang", "email"=>"[email protected]", "name"=>"Autumn Wang"}, @name="Autumn Wang">, @private_message=true>
[] [DEBUG] Sending message to JID [email protected]: (failed) Test
[] [DEBUG] Sending message to JID [email protected]: Test message

@autumnw
Copy link
Author

autumnw commented Jan 17, 2017

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 [email protected]".

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:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]", "lita:users:id:[email protected]"]

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants