Class: GTalk::Bot

Inherits:
Object
  • Object
show all
Includes:
Jabber
Defined in:
lib/easy-gtalk-bot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_data) ⇒ Bot

Returns a new instance of Bot.



22
23
24
25
26
27
28
# File 'lib/easy-gtalk-bot.rb', line 22

def initialize()
  @email = [:email]
  @password = [:password]

  @jid = JID::new(self.email)
  @jabber_client = Client.new(self.jid)
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.



19
20
21
# File 'lib/easy-gtalk-bot.rb', line 19

def email
  @email
end

#jabber_clientObject (readonly)

Returns the value of attribute jabber_client.



20
21
22
# File 'lib/easy-gtalk-bot.rb', line 20

def jabber_client
  @jabber_client
end

#jidObject (readonly)

Returns the value of attribute jid.



20
21
22
# File 'lib/easy-gtalk-bot.rb', line 20

def jid
  @jid
end

#passwordObject (readonly)

Returns the value of attribute password.



19
20
21
# File 'lib/easy-gtalk-bot.rb', line 19

def password
  @password
end

Instance Method Details

#accept_invitation(inviter) ⇒ Object



41
42
43
44
45
# File 'lib/easy-gtalk-bot.rb', line 41

def accept_invitation(inviter)
  inviter = JID::new(inviter)
  self.roster.accept_subscription(inviter)
  invite(JID::new(inviter))
end

#get_onlineObject



30
31
32
33
34
# File 'lib/easy-gtalk-bot.rb', line 30

def get_online
  self.jabber_client.connect
  self.jabber_client.auth(self.password)
  self.jabber_client.send(Presence.new.set_type(:available))
end

#invite(invitee) ⇒ Object



36
37
38
39
# File 'lib/easy-gtalk-bot.rb', line 36

def invite(invitee)
  subscription_request = Presence.new.set_type(:subscribe).set_to(JID::new(invitee))
  self.jabber_client.send(subscription_request)
end

#message(to, text) ⇒ Object



47
48
49
50
51
# File 'lib/easy-gtalk-bot.rb', line 47

def message(to, text)
  message = Message::new(JID::new(to), text)
  message.type = :chat
  self.jabber_client.send(message)
end

#on_invitation(&block) ⇒ Object



53
54
55
56
57
# File 'lib/easy-gtalk-bot.rb', line 53

def on_invitation(&block)
  self.roster.add_subscription_request_callback do |_, presence|
    block.call(presence.from.to_email)
  end
end

#on_message(&block) ⇒ Object



59
60
61
62
63
# File 'lib/easy-gtalk-bot.rb', line 59

def on_message(&block)
  self.jabber_client.add_message_callback do |message|
    block.call(message.from.to_email, message.body)
  end
end