-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgtalk_alert.rb
executable file
·59 lines (49 loc) · 1.32 KB
/
gtalk_alert.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
#!/usr/bin/env ruby
# coding: utf-8
require 'easy-gtalk-bot'
require 'yaml'
class GtalkAlert
@@user_name = nil
@@password = nil
@@CONFIG_FILENAME = 'gtalk.yml'
@@gtalk = nil
def self.init(user_name, password)
@@user_name = user_name
@@password = password
@@gtalk = GTalk::Bot.new(:email => @@user_name, :password => @@password)
@@gtalk.get_online
end
if File.readable?(@@CONFIG_FILENAME)
cfg = YAML.load(File.open(@@CONFIG_FILENAME))
self.init(cfg['user_name'], cfg['password'])
end
def self.set_basic_info(user_name, password)
cfg = {}
cfg['user_name'] = user_name
cfg['password'] = password
File.open( @@CONFIG_FILENAME, 'w' ) do |out|
YAML.dump(cfg , out)
end
end
def self.send_to(phones, content)
raise "gtalk is offline" if @@gtalk.nil?
send(@@user_name, @@password, phones, content)
end
def self.send(user_name, password, phones, content)
phones = [phones] if not phones.is_a?(Array)
phones.each do |phone|
begin
tries ||= 5
@@gtalk.message(phone.strip,"#{content}")
rescue IOError
retry unless (tries -= 1).zero?
end
end
return nil
end
end
if $0 == __FILE__
# GtalkAlert.set_basic_info(ARGV[0], ARGV[1])
ret = GtalkAlert.send_to("zhzhqiang@gmail.com", "测试#{Time.now}")
# puts ret
end