edi 0.3.6 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/:w +57 -0
- data/lib/edi.rb +12 -0
- data/lib/edi/service_runner.rb +1 -0
- data/lib/edi/slack.rb +1 -0
- data/lib/edi/slack/channel.rb +18 -0
- data/lib/edi/version.rb +1 -1
- data/lib/edi/websocket/client.rb +19 -1
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 601cdae478aa39f3a62c7fa674c5611004d72d5c
|
|
4
|
+
data.tar.gz: 4c663b2a26f1006c0ab47c63f3e323b68b4fd985
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 430594d7295f5dc3827b2d4cf1d29e988d1599eb3b0cfbd6b8e6f1f12ffd4ba32348abae9b92f11205f52b6efb5ef581d87cd4b90723a3d1fb0a3c87cdc67291
|
|
7
|
+
data.tar.gz: bb11a163d75bb8eb35bce7949ed1cbc1891ea89833ead95b1c0607c98f8516c9a3d40068204c8a65b486b8e079e8a3a4714fd6b5b9e84742c9c64e2378edd607
|
data/:w
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require 'faye/websocket'
|
|
2
|
+
require 'eventmachine'
|
|
3
|
+
|
|
4
|
+
module Websocket
|
|
5
|
+
class Client
|
|
6
|
+
attr_accessor :ws_url, :client, :id
|
|
7
|
+
|
|
8
|
+
def initialize
|
|
9
|
+
self.id = 1
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def connect
|
|
13
|
+
connection = EDI.get("https://slack.com/api/rtm.start?token=#{EDI.bot_token}").response
|
|
14
|
+
require 'byebug'
|
|
15
|
+
debugger
|
|
16
|
+
self.ws_url = connection["url"]
|
|
17
|
+
EM.run {
|
|
18
|
+
|
|
19
|
+
self.client = Faye::WebSocket::Client.new(ws_url)
|
|
20
|
+
|
|
21
|
+
client.on :open do |event|
|
|
22
|
+
puts "EDI is now online"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Respond to Messages
|
|
26
|
+
client.on :message do |event|
|
|
27
|
+
incoming_message = Slack::WebsocketIncomingMessage.new(event.data)
|
|
28
|
+
if incoming_message.should_respond?
|
|
29
|
+
puts "EDI received message #{incoming_message.text} in channel #{incoming_message.channel}"
|
|
30
|
+
response_text = ""
|
|
31
|
+
service = Proc.new { response_text = EDI.runner.new(message: incoming_message).execute }
|
|
32
|
+
response = Proc.new { client.send Slack::WebsocketOutgoingMessage.new(text: response_text, channel: incoming_message.channel, id: id).to_json if response_text }
|
|
33
|
+
EM.defer service, response
|
|
34
|
+
increment_id
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Kepp Websocket Connection Alive
|
|
39
|
+
EM.add_periodic_timer(1) do
|
|
40
|
+
client.ping
|
|
41
|
+
end
|
|
42
|
+
}
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def send_message(message)
|
|
46
|
+
client.send Slack::WebsocketOutgoingMessage.new(text: message, channel: "D046WN43L", id: id).to_json
|
|
47
|
+
increment_id
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
# There has to be a way to do this better
|
|
53
|
+
def increment_id
|
|
54
|
+
self.id += 1
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
data/lib/edi.rb
CHANGED
|
@@ -70,6 +70,18 @@ module EDI
|
|
|
70
70
|
self.config.bot_token
|
|
71
71
|
end
|
|
72
72
|
|
|
73
|
+
def channels
|
|
74
|
+
@channels ||= []
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def add_channel(channel)
|
|
78
|
+
channels << channel
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def send_message(message, channel_name: "general", channel_id: nil)
|
|
82
|
+
EDI.websocket.send_message(message, channel_name: channel_name, channel_id: channel_id)
|
|
83
|
+
end
|
|
84
|
+
|
|
73
85
|
end
|
|
74
86
|
include EDI::HTTPUtilities
|
|
75
87
|
include EDI::Configuration
|
data/lib/edi/service_runner.rb
CHANGED
data/lib/edi/slack.rb
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module Slack
|
|
2
|
+
class Channel
|
|
3
|
+
attr_accessor :id, :name
|
|
4
|
+
def initialize(id:, name:)
|
|
5
|
+
@id = id
|
|
6
|
+
@name = name
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def self.find_by_name(name)
|
|
10
|
+
EDI.channels.find { |c| c.name == name }
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.find_by_id(id)
|
|
14
|
+
EDI.channels.find { |c| c.id == id }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
end
|
|
18
|
+
end
|
data/lib/edi/version.rb
CHANGED
data/lib/edi/websocket/client.rb
CHANGED
|
@@ -10,7 +10,11 @@ module Websocket
|
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
def connect
|
|
13
|
-
|
|
13
|
+
connection = EDI.get("https://slack.com/api/rtm.start?token=#{EDI.bot_token}").response
|
|
14
|
+
connection["channels"].each do |c|
|
|
15
|
+
EDI.add_channel Slack::Channel.new(name: c["name"], id: c["id"])
|
|
16
|
+
end
|
|
17
|
+
self.ws_url = connection["url"]
|
|
14
18
|
EM.run {
|
|
15
19
|
|
|
16
20
|
self.client = Faye::WebSocket::Client.new(ws_url)
|
|
@@ -23,6 +27,7 @@ module Websocket
|
|
|
23
27
|
client.on :message do |event|
|
|
24
28
|
incoming_message = Slack::WebsocketIncomingMessage.new(event.data)
|
|
25
29
|
if incoming_message.should_respond?
|
|
30
|
+
puts "EDI received message #{incoming_message.text} in channel #{incoming_message.channel}"
|
|
26
31
|
response_text = ""
|
|
27
32
|
service = Proc.new { response_text = EDI.runner.new(message: incoming_message).execute }
|
|
28
33
|
response = Proc.new { client.send Slack::WebsocketOutgoingMessage.new(text: response_text, channel: incoming_message.channel, id: id).to_json if response_text }
|
|
@@ -38,8 +43,21 @@ module Websocket
|
|
|
38
43
|
}
|
|
39
44
|
end
|
|
40
45
|
|
|
46
|
+
def send_message(message, channel_name: "general", channel_id: nil)
|
|
47
|
+
unless channel_id
|
|
48
|
+
channel_id = lookup_channel_by_name(channel_name).id
|
|
49
|
+
end
|
|
50
|
+
client.send Slack::WebsocketOutgoingMessage.new(text: message, channel: channel_id, id: id).to_json
|
|
51
|
+
increment_id
|
|
52
|
+
end
|
|
53
|
+
|
|
41
54
|
private
|
|
42
55
|
|
|
56
|
+
def lookup_channel_by_name(name)
|
|
57
|
+
Slack::Channel.find_by_name(name)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# There has to be a way to do this better
|
|
43
61
|
def increment_id
|
|
44
62
|
self.id += 1
|
|
45
63
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: edi
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- DVG
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2015-03-
|
|
11
|
+
date: 2015-03-31 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -288,6 +288,7 @@ files:
|
|
|
288
288
|
- ".rspec"
|
|
289
289
|
- ".ruby-version"
|
|
290
290
|
- ".travis.yml"
|
|
291
|
+
- ":w"
|
|
291
292
|
- Gemfile
|
|
292
293
|
- Guardfile
|
|
293
294
|
- LICENSE.txt
|
|
@@ -342,6 +343,7 @@ files:
|
|
|
342
343
|
- lib/edi/services/tweet_that.rb
|
|
343
344
|
- lib/edi/services/weather.rb
|
|
344
345
|
- lib/edi/slack.rb
|
|
346
|
+
- lib/edi/slack/channel.rb
|
|
345
347
|
- lib/edi/slack/message.rb
|
|
346
348
|
- lib/edi/slack/websocket_incoming_message.rb
|
|
347
349
|
- lib/edi/slack/websocket_outgoing_message.rb
|