A chat bot using Blather

275 views
Skip to first unread message

Saket Choudhary

unread,
Dec 11, 2011, 7:16:19 AM12/11/11
to xmpp-b...@googlegroups.com
I am new to Ruby and Blather, I wanted to make a chatbot  which when pinged by a certain set of users will carry out some actions like "eamiling", "searching Google" etc.

I went forward like this :
created an array of allowed users :

allowed_users=["a...@ex.com","aa@bb,com"]
message :chat?,:body do |m|
   if allowed_users.include(m.from.to_s.split("/")[0])
        say m.from "Welcome User. Enter your choice : 1>Mail 2> Google"
       message :chat?,:body => 2 do |t|
         ?????
      end
   else
        say m.from "You aren't allowed here !"
end


As youi would have realised this doesn't work ! How do I maintain a "session" for a user and do the process accordingly>

For example if I am user a...@ex.com and now I want to Google for "Ruby:"
I ping  !
I get a "Welcome" reply!
I enter  "2"
but then how do I actually Google for Ruby ?
The problem is not about making the HttpRequest to Google.! How do I personalise my code for a particular user who has pinged  ?

Jeff Smick

unread,
Dec 14, 2011, 10:52:10 AM12/14/11
to xmpp-b...@googlegroups.com
Hey Saket,

Nesting the message handlers in the way you're doing now will cause some issues. You'll create a new handler every time someone sends a message. So the first thing would be to break that inner handler out.

The other thing I'd suggest would be to use JIDs instead of strings. If you make your list of allowed users a list of JIDs you wouldn't have to do the string manipulation yourself.

The way you're designing the system though, you'll need to keep track of the user's state, where they are in the menu system. It might be easier to have a user be explicit each time. Instead of "2" then "something I want to google" they'd write "google: something I want to google"

Below is how you might better break up your current script:

allowed_users = ["a...@ex.com", "a...@bb.com"].map { |j| Blather::JID.new(j) }

# this will guard all message handlers, checking to see that the user is valid
before(:message) do |m|
  if !allowed_user.include?(m.from.stripped)
    say m.from, "You aren't allowed here"
  end
  true
end

message :chat?, :body => '1' do |m|
  # mail
end

message :chat?, :body => '2' do |m|
  # google
end

message :chat?, :body do |m|
  say m.from, "Welcome ..."
end



--
You received this message because you are subscribed to the Google Groups "Blather" group.
To view this discussion on the web visit https://groups.google.com/d/msg/xmpp-blather/-/5xNuYyAcJu4J.
To post to this group, send email to xmpp-b...@googlegroups.com.
To unsubscribe from this group, send email to xmpp-blather...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/xmpp-blather?hl=en.



--
Jeff Smick
@sprsquish
http://squishtech.com

Saket Choudhary

unread,
Dec 15, 2011, 2:44:20 AM12/15/11
to xmpp-b...@googlegroups.com
Hey Jeff,

Thanks a lot ! 
Worked like a Jewel, or should I say A Gem !

Thanks 
Saket

Saket Choudhary

unread,
Dec 15, 2011, 4:03:53 AM12/15/11
to xmpp-b...@googlegroups.com
Just one more query:

How do I set the status for the online bot?

status = Status.new
status.message = "Now Live"

This Didnt work !

Ben Langfeld

unread,
Dec 15, 2011, 5:36:12 AM12/15/11
to xmpp-b...@googlegroups.com
You will have to send the stanza, something like this:

write_to_stream Status.new(:available, "Now Live")

Regards,
Ben Langfeld

Saket Choudhary

unread,
Dec 15, 2011, 5:42:30 AM12/15/11
to xmpp-b...@googlegroups.com
This has to be protected by guards?

setup ENV['JID'], ENV['JPASSWORD']
write_to_stream Status.new(:available, "Now Live")

Didn't work.!

Ben Langfeld

unread,
Dec 15, 2011, 6:04:28 AM12/15/11
to xmpp-b...@googlegroups.com
You will have to do it in a when_ready block:

setup .....

when_ready do


write_to_stream Status.new(:available, "Now Live")

end

Regards,
Ben Langfeld

Saket Choudhary

unread,
Dec 15, 2011, 6:42:04 AM12/15/11
to xmpp-b...@googlegroups.com
Thanks Ben !

Worked !
Reply all
Reply to author
Forward
0 new messages