How to forward message responses with ruby Lita? - ruby

I am working with a chatbot operating on the lita ruby gem using lita-hipchat. After a response is made to lita using hipchat, lita will be able to return messages to the user who created the response through the reply method. I would like to change this pattern and be able to send a hipchat to a secondary user, essentially being able to cc or forward that same response to more than one user. Is this possible using only the Lita gem?
I am aware that sending messages through http or the hipchat gem is another option for sending messages to secondary users, but I would prefer to do this through lita.

You can do this using Robot#send_messages. For example:
def my_handler_route(response)
user2 = Lita::User.find_by_id("user2")
target = Lita::Source(user: user2)
robot.send_message(target, "This message will go to User2!")
end
This is essentially what Response#reply is doing—but with the convenience of automatically targeting the original source.

Related

Ruby HipChat Wrapper, how to check that a username is valid?

I am using the ruby HipChat Wrapper to send HipChat messages with a script. Before attempting to send any messages I would like to be able to check that the users I am attempting to message exist and are valid.
Is there a function in the HipChat::Client class that allows me to do this?
Note: I know that I can test if a user exists by attempting to send a message. If the script exits with a Access denied to user error then it is invalid. However I would like to test that a user exists without actually sending any messages.
I'm not familiar with that client, however looking at the library you should be able to do something like this client.user('foo#bar.org').view (assuming API V2).
Have a look at user.rb#L57 which uses https://www.hipchat.com/docs/apiv2/method/view_user, that will probably give you back an error as well if the user does not exist (unsure). It's probably still better than sending the entire message before realising the user does not exist (alternatively you can also retrieve all users and check it against that).

creating a slack bot using slack-api ruby gem not responding back as a DM

I have set up a slack bot using slack-api and the real-time-messaging api.
Here is the abbreviated setup:
client.on :message do |data|
d {data}
bot_response = BotResponse.get_bot_response(data['text'], "session_slack")
Slack.chat_postMessage channel: data['user'], text: "#{bot_response}"
end
client.start
With this version of the postMessage, the response comes from Slackbot, not my bot (named kaya).
Goal: I want to respond to come as a DM from the bot it was sent to.
When I change the channel to data['channel'], the response comes as DM from my bot kaya, but gets into an endless loop.
How do I have a non-endless loop DM response?
NOTE:
I think I see how it is happening: by selecting the bot as the "channel" the bot is responding to it's own response back to me, as if it were another user talking into the "bot's" channel. But I can't tell how else to have the response come from my bot, not slackbot.
I believe you need to include the username parameter set to the bot name per the api: https://api.slack.com/methods/chat.postMessage, or you need the as_user option.
This mixes the Web and the RealTime Messaging API. You get a message from the RealTime Messaging API then you are using the Web API to post back. The answer of including as_user: true is correct, but you should instead use the RTM API to send the message back.
Try https://github.com/dblock/slack-ruby-client instead that cleanly separates the two. Sending a message back as the bot looks like this:
client.message channel: data['channel'], text: "Hi <##{data['user']}>!"
To avoid DM loops, make sure you're not responding to commands that you emit. There're other ways, like ignoring bot messages, but it's not as reliable.

Email events in Sinatra

I am working on a Sinatra web app that needs to save any email that it receives in a db (or do something else as soon as a mail is received). What is the best tool for this job? I was looking at eventmachine and it seems a bit complex. I was considering the mail gem but it doesn't trigger events when mail is recieved. One final query: To test such an app, do I need it to host it online? If not, then how to do I send emails to the app and test?
Thanks a lot,
So Sinatra is a web server. Email doesn't come in from HTTP requests, so you need a mail server to receive the emails and assuming you still want to handle them in your Sinatra app, fire a request at your app.
check out http://steve.dynedge.co.uk/2010/09/07/incoming-email-in-rails-3-choosing-the-right-approach/ for a few options (it's thinking rails, but you should be able to translate)

Receive email in Ruby sinatra

Is there some way to receive emails with Sinatra? I've seen the pages that say how to do it with RoR, but none without. I have also found out how to send emails with the gem Pony. Thanks!
I believe you are looking for the mail gem (github). It has send and receive capability.
Update: I forgot that there is a Google Group too
Rails uses mail gem in order to send and receive emails. This gem can be used in Sinatra just as easily.

Sending XMPP message to offline Google Talk user with ruby xmpp4r

When using Perl's Net::Jabber, sending a simple message to an offline user causes the message to be delivered to the user when he comes online (it even show's in the user's gmail account as unread messages). That's as simple as doing
my $msg = Net::Jabber::Message->new();
$msg->SetMessage(...);
$connection->Send($msg);
In Ruby xmpp4r, doing the same equivalent thing does not send the message to an offline user, instead returning (async) a xmpp service-unavailable stanza or even not returning anything, and also not working. Message is simply lost.
How can one send an offline message with xmpp4r?
Also, in related subject, in xmpp's api docs for Jabber::Stream's send method, there is a "block" parameter. How does one use that?
Thanks
Make sure that you're doing type=:chat on the message.
client.send(Jabber::Message.new(jid, contents).set_type(:chat))
(code copied from Ricardo Pardini's comment, for long-term clarity)

Resources