working on pushover agent.

akil harris 11 年之前
父节点
当前提交
6d6d702b0a
共有 1 个文件被更改,包括 109 次插入0 次删除
  1. 109 0
      app/models/agents/pushover_agent.rb

+ 109 - 0
app/models/agents/pushover_agent.rb

@@ -0,0 +1,109 @@
1
+
2
+module Agents
3
+  class PushoverAgent < Agent
4
+    cannot_be_scheduled!
5
+    cannot_create_events!
6
+
7
+    description <<-MD
8
+      The PushoverAgent receives and collects events and sends them via push notification to a user/group.
9
+
10
+      You need a Pushover API Token: [https://pushover.net/apps/build](https://pushover.net/apps/build)
11
+
12
+      * `token`: your application's API token
13
+      * `user`: the user or group key (not e-mail address).
14
+      * `expected_receive_period_in_days`:  is maximum number of days that you would expect to pass between events being received by this agent.
15
+      
16
+      Your event should provide a `message` or `text` key that will contain the body of the notification. Pushover API has a `512` Character Limit including title. 
17
+
18
+      Your event can provide any of the following optional parameters or you can provide defaults:
19
+
20
+      * `device` - your user's device name to send the message directly to that device, rather than all of the user's devices
21
+      * `title` or `subject` - your notifications's title
22
+      * `url` - a supplementary URL to show with your message - `512` Character Limit
23
+      * `url_title` - a title for your supplementary URL, otherwise just the URL is shown - `100` Character Limit
24
+      * `priority` - send as -1 to always send as a quiet notification, 1 to display as high-priority and bypass the user's quiet hours, or 2 to also require confirmation from the user
25
+      * `timestamp` - a [Unix timestamp](https://en.wikipedia.org/wiki/Unix_time) of your message's date and time to display to the user, rather than the time your message is received by our API
26
+      * `sound` - the name of one of the sounds supported by device clients to override the user's default sound choice
27
+
28
+
29
+    MD
30
+
31
+    def default_options
32
+      {
33
+        'token' => 'vKxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
34
+        'user' => 'Fjxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
35
+        'device' => '',
36
+        'title' => '',
37
+        'url' => '',
38
+        'url_title' => '',
39
+        'priority' => '0',
40
+        'timestamp' => '',
41
+        'sound' => 'pushover',
42
+        'expected_receive_period_in_days' => '1'
43
+      }
44
+    end
45
+
46
+    def validate_options
47
+      unless options['token'].present? && options['user'].present? && options['expected_receive_period_in_days'].present?
48
+        errors.add(:base, 'token, user, and expected_receive_period_in_days are all required.')
49
+      end
50
+    end
51
+
52
+    def receive(incoming_events)
53
+      incoming_events.each do |event|
54
+        message = (event.payload['message'] || event.payload['text']).to_s
55
+        if message != ""
56
+            post_params = {
57
+              'token' => options['token'],
58
+              'user' => options['user'],
59
+              'message' => message
60
+            }
61
+
62
+            if event.payload['device'] || options['device']
63
+              post_params['device'] = event.payload['device'] || options['device']
64
+            end
65
+
66
+            if event.payload['title'] || options['title']
67
+              post_params['title'] = event.payload['title'] || options['title']
68
+            end
69
+
70
+            if event.payload['url'] || options['url']
71
+              url = (event.payload['url'] || options['url'] || '').to_s
72
+              url = url.slice 0..512
73
+              post_params['url'] = url
74
+            end
75
+
76
+            if event.payload['url_title'] || options['url_title']
77
+              url_title = (event.payload['url_title'] || options['url_title']).to_s
78
+              url_title = url_title.slice 0..100
79
+              post_params['url_title'] = url_title
80
+            end
81
+
82
+            if event.payload['priority'] || options['priority']
83
+              post_params['priority'] = (event.payload['priority'] || options['priority']).to_i
84
+            end
85
+
86
+            if event.payload['timestamp'] || options['timestamp']
87
+              post_params['timestamp'] = (event.payload['timestamp'] || options['timestamp']).to_s
88
+            end
89
+
90
+            if event.payload['sound'] || options['sound']
91
+              post_params['sound'] = (event.payload['sound'] || options['sound']).to_s
92
+            end
93
+            
94
+            send_notification post_params
95
+          end
96
+        end
97
+      end
98
+    end
99
+
100
+    def working?
101
+      last_receive_at && last_receive_at > options['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
102
+    end
103
+
104
+    def send_notification(post_params)
105
+      
106
+    end
107
+
108
+  end
109
+end