forked from ScatteredRay/CoworkBot
/
bot.rb
83 lines (68 loc) · 1.79 KB
/
bot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require "rubygems"
require "xmpp4r"
require "xmpp4r/message"
require "xmpp4r/roster"
require "xmpp4r/vcard"
require "yaml"
require "twitter"
include Jabber
require "ruby-debug"
Jabber::debug = false
def load_config()
$settings = YAML::load_file("config.yaml")
$jsettings = $settings["Jabber"];
tsettings = $settings["Twitter"];
Twitter.configure do |c|
c.consumer_key = tsettings["consumer_key"]
c.consumer_secret = tsettings["consumer_secret"]
c.oauth_token = tsettings["oauth_token"]
c.oauth_token_secret = tsettings["oauth_secret"]
end
end
load_config();
client = Client.new(JID::new($jsettings["JID"]));
client.connect($jsettings["Server"]);
client.auth($jsettings["Pass"]);
client.send(Presence.new.set_type(:available))
roster = Roster::Helper.new(client)
roster.add_subscription_request_callback do |item,pres|
roster.accept_subscription(pres.from)
end
$commands = Hash.new
def add_command(cmd, &block)
$commands[cmd] = block
end
add_command "tweet" do |m, s|
msg = Message.new(m.from, "Posting \"#{s}\"");
msg.type=:chat
client.send(msg);
tc = Twitter::Client.new;
tc.update(s);
end
add_command "hi" do |m, s|
msg = Message.new(m.from, "Hello!");
msg.type=:chat
client.send(msg);
end
client.delete_message_callback("process_message")
client.add_message_callback 0, "process_message" do |m|
begin
if($jsettings["Privl"].include?(m.from.bare.to_s.downcase))
bdy = m.body.to_s;
cmd = bdy.split[0].downcase;
s = bdy[cmd.length..bdy.length].strip
p = $commands[cmd];
if(p)
p.call(m, s);
end
end
rescue Exception => e
print "Exception caught #{e}"
begin
msg = Message.new(m.from, "Error caught handling command!");
msg.type=:chat
client.send(msg);
rescue Exception
end
end
end