Created
March 20, 2014 12:10
-
-
Save barenko/9662391 to your computer and use it in GitHub Desktop.
client gtalk ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'xmpp4r' | |
require 'xmpp4r/client' | |
require 'xmpp4r/roster' | |
require 'rss' | |
require 'open-uri' | |
include Jabber | |
class Bot | |
attr_reader :client | |
def initialize jabber_id, jabber_password | |
@jabber_id = jabber_id | |
@jabber_password = jabber_password | |
connect | |
end | |
def send(to, msg) | |
message = Message.new(to, msg) | |
message.type=:chat | |
@client.send(message) | |
end | |
def on_message | |
mainthread = Thread.current | |
@client.add_message_callback do |message| | |
unless message.body.nil? && message.type != :error | |
reply = case message.body | |
when "Time" then reply(message, "Current time is #{Time.now}") | |
when "Help" then reply(message, "Available commands are: 'Time', 'Help'.") | |
else reply(message, "You said: #{message.body}") | |
end | |
end | |
end | |
Thread.stop | |
@client.close | |
end | |
private | |
def reply message, reply_content | |
reply_message = Message.new(message.from, reply_content) | |
reply_message.type = message.type | |
@client.send reply_message | |
end | |
def connect | |
jid = JID.new(@jabber_id) | |
@client = Client.new jid | |
@client.connect | |
@client.auth @jabber_password | |
@client.send(Presence.new.set_type(:available)) | |
puts "Hurray...!! Connected..!!" | |
end | |
end | |
@bot = Bot.new 'email@gmail.com', 'password_here' | |
threads = [] | |
threads << Thread.new { | |
@bot.send 'other@gmail.com', 'teste!' | |
sleep 10 | |
@bot.send 'other@gmail.com', 'teste again!' | |
} | |
threads << Thread.new { | |
@bot.on_message | |
} | |
threads.each { |thr| thr.join } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment