Class: JabberBot
- Inherits:
-
Object
- Object
- JabberBot
- Defined in:
- lib/jabber_bot.rb
Instance Method Summary collapse
- #add_command(command, syntax, description, public = false, &callback) ⇒ Object
- #connect ⇒ Object
- #disconnect ⇒ Object
-
#initialize(config) ⇒ JabberBot
constructor
A new instance of JabberBot.
- #listen ⇒ Object
- #run_command(command, params) ⇒ Object
- #say(to, message) ⇒ Object
Constructor Details
#initialize(config) ⇒ JabberBot
6 7 8 9 10 11 12 13 |
# File 'lib/jabber_bot.rb', line 6 def initialize(config) @config = config @keep_alive_status = false @commands = {} @jabber = Client.new(JID::new(@config['JID'] + '/' + @config['resource'])) #add_default_commands keep_alive end |
Instance Method Details
#add_command(command, syntax, description, public = false, &callback) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/jabber_bot.rb', line 45 def add_command(command, syntax, description, public = false, &callback) @commands[command.downcase] = { 'syntax' => syntax, 'description' => description, 'callback' => callback, 'public' => public } end |
#connect ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/jabber_bot.rb', line 15 def connect begin if not @jabber.is_connected? @keep_alive_status = false @jabber.connect(@config['host']) @jabber.auth(@config['password']) @jabber.send(Presence.new.set_type(:available)) end rescue Exception => e puts "Error connecting: #{e} (#{e.class})!" ensure @keep_alive_status = true end end |
#disconnect ⇒ Object
30 31 32 33 |
# File 'lib/jabber_bot.rb', line 30 def disconnect @keep_alive_status = false; @jabber.close end |
#listen ⇒ Object
35 36 37 38 39 |
# File 'lib/jabber_bot.rb', line 35 def listen @jabber. do || () unless .body.to_s == '' end end |
#run_command(command, params) ⇒ Object
54 55 56 |
# File 'lib/jabber_bot.rb', line 54 def run_command(command, params) return @commands[command]['callback'].call(params) end |
#say(to, message) ⇒ Object
41 42 43 |
# File 'lib/jabber_bot.rb', line 41 def say(to, ) @jabber.send(Message.new(to, ).set_type(:chat)) end |