litaio / lita

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

How do I post a message to a specific room? #160

Closed nambrot closed 8 years ago

nambrot commented 9 years ago

I want to build an anonymous messaging handler that lets you message lita which in turn will message a channel or user anonymously. It is not obvious to me how I would message that is not a direct response?

jeremy-albinet commented 9 years ago

You may create a fake Source. Something like :

target = Source.new(room: '#myroot')
robot.send_message(target, "Message")
nambrot commented 9 years ago

That's what I thought, but it didnt work for me, it just does nothing. I'm using the slack adapter.

[9] pry(#<Lita::Handlers::Anonymous>)> target = Source.new(room: '#litatest')
=> #<Lita::Source:0x007f941a8b9150 @private_message=false, @room="#litatest", @room_object=#<Lita::Room:0x007f941a8b9100 @id="#litatest", @metadata={}, @name="#litatest">, @user=nil>
[10] pry(#<Lita::Handlers::Anonymous>)> robot.send_message(target, "Message")
=> ["Message"]

Would that be an adapter problem then?

jimmycuadra commented 9 years ago

The value for the :room argument needs to be the room's ID. Either of these should work:

Source.new(room: Room.find_by_name('litatest').id)
Source.new(room_object: Room.find_by_name('litatest'))

Edit: See my other comment below, the second version I show here is wrong.

nambrot commented 9 years ago

I'm sorry if I'm missing something very obvious but neither

[12] pry(#<Lita::Handlers::Anonymous>)> target = Source.new(room: Lita::Room.find_by_name('litatest').id)
=> #<Lita::Source:0x007fb7abd6dde0
 @private_message=false,
 @room="C0FV0E1AM",
 @room_object=#<Lita::Room:0x007fb7abd6dd90 @id="C0FV0E1AM", @metadata={}, @name="C0FV0E1AM">,
 @user=nil>
[13] pry(#<Lita::Handlers::Anonymous>)> robot.send_message(target, "Message")
=> ["Message"]

nor

[14] pry(#<Lita::Handlers::Anonymous>)> target = Source.new(room_object: Lita::Room.find_by_name('litatest'))
ArgumentError: unknown keyword: room_object

In the first case, no message gets sent, in the last case, I cant seem to create the fake source

jimmycuadra commented 9 years ago

What version of Lita are you using?

jimmycuadra commented 9 years ago

Also, I'm gonna leave this issue open because this needs to be added to the documentation website. It should be easier for folks to figure out how to do this.

jimmycuadra commented 9 years ago

Oh, I was remembering wrong. The keyword argument to the Source constructor is just :room, but you can pass it either a string ID or a Room object.

The first example you show in Pry looks like it worked, though, no?

nambrot commented 9 years ago

@jimmycuadra Thanks for re-opening.

Lita version:

➜  wellframe-lita git:(master) ✗ bundle show lita
/Users/nambrot/.rvm/gems/ruby-2.2.3/gems/lita-4.6.1

The first example didn't throw any errors, but I didn't see anything get posted in the channel and I am unsure about how I would further debug it.

nambrot commented 8 years ago

@jimmycuadra Do you have any other ideas of what I should try?

jimmycuadra commented 8 years ago

Is Lita present in the room you're attempting to send the message to? I'm not sure how Slack behaves if it's not.

nambrot commented 8 years ago

Yes, I did invite Lita to the room

jimmycuadra commented 8 years ago

The code you showed in Pry where you create the source object manually looks completely correct to me. I don't have another immediate suggestion, unfortunately—this looks like it will require some interactive debugging.

nambrot commented 8 years ago

Do you have any idea where to put my focus on during the debugging? Somewhere in lita or somewhere in lita-slack?

jimmycuadra commented 8 years ago

I'd start in the Slack adapter.

nambrot commented 8 years ago

I figured it out. The problem was that I set a breakpoint inside the adapter, causing any issues of messages to be queued as lita-slack is using EM. I'm not sure why they wouldnt eventually bubble through, but it works now. Adding the handler was a breeze from there! https://github.com/nambrot/lita-anonymous