Class: Slacks::Connection
- Inherits:
-
Object
- Object
- Slacks::Connection
- Includes:
- Observer
- Defined in:
- lib/slacks/connection.rb
Constant Summary collapse
- EVENT_CHANNEL_CREATED =
"channel_created".freeze
- EVENT_GROUP_JOINED =
"group_joined".freeze
- EVENT_MESSAGE =
"message".freeze
- EVENT_USER_JOINED =
"team_join".freeze
Instance Attribute Summary collapse
-
#bot ⇒ Object
readonly
Returns the value of attribute bot.
-
#team ⇒ Object
readonly
Returns the value of attribute team.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#typing_speed ⇒ Object
Returns the value of attribute typing_speed.
Instance Method Summary collapse
- #add_reaction(emojis, message) ⇒ Object
- #can_see?(channel) ⇒ Boolean
- #channels ⇒ Object
- #find_channel(id) ⇒ Object
- #find_conversation(id) ⇒ Object
- #find_user(id) ⇒ Object
- #find_user_by_nickname(nickname) ⇒ Object
- #get_message(channel, ts) ⇒ Object
-
#initialize(token, options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #listen! ⇒ Object
- #listening? ⇒ Boolean
- #ping ⇒ Object
- #send_message(message, options = {}) ⇒ Object (also: #say)
- #typing_on(channel) ⇒ Object
- #update_message(ts, message, options = {}) ⇒ Object
- #user_exists?(username) ⇒ Boolean
- #users ⇒ Object
Methods included from Observer
Constructor Details
#initialize(token, options = {}) ⇒ Connection
Returns a new instance of Connection.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/slacks/connection.rb', line 18 def initialize(token, ={}) raise ArgumentError, "Missing required parameter: 'token'" if token.nil? or token.empty? @token = token @typing_speed = .fetch(:typing_speed, 100.0) @user_ids_dm_ids = {} @users_by_id = {} @user_id_by_name = {} @conversations_by_id = {} @conversation_ids_by_name = {} end |
Instance Attribute Details
#bot ⇒ Object (readonly)
Returns the value of attribute bot.
15 16 17 |
# File 'lib/slacks/connection.rb', line 15 def bot @bot end |
#team ⇒ Object (readonly)
Returns the value of attribute team.
15 16 17 |
# File 'lib/slacks/connection.rb', line 15 def team @team end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
15 16 17 |
# File 'lib/slacks/connection.rb', line 15 def token @token end |
#typing_speed ⇒ Object
Returns the value of attribute typing_speed.
16 17 18 |
# File 'lib/slacks/connection.rb', line 16 def typing_speed @typing_speed end |
Instance Method Details
#add_reaction(emojis, message) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/slacks/connection.rb', line 68 def add_reaction(emojis, ) Array(emojis).each do |emoji| api("reactions.add", name: emoji.gsub(/^:|:$/, ""), channel: .channel.id, timestamp: .) end end |
#can_see?(channel) ⇒ Boolean
160 161 162 163 164 165 |
# File 'lib/slacks/connection.rb', line 160 def can_see?(channel) channel_id = to_channel_id(channel) channel_id && !channel_id.empty? rescue ArgumentError false end |
#channels ⇒ Object
151 152 153 154 155 156 157 158 |
# File 'lib/slacks/connection.rb', line 151 def channels channels = user_id_by_name.keys + conversation_ids_by_name.keys if channels.empty? fetch_conversations! fetch_users! end channels end |
#find_channel(id) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/slacks/connection.rb', line 169 def find_channel(id) case id when /^U/ then find_user(id) when /^D/ user = find_user(get_user_id_for_dm(id)) Slacks::Channel.new self, { "id" => id, "is_im" => true, "name" => user.username } else Slacks::Channel.new(self, find_conversation(id)) end end |
#find_conversation(id) ⇒ Object
183 184 185 186 187 188 189 190 |
# File 'lib/slacks/connection.rb', line 183 def find_conversation(id) conversations_by_id.fetch(id) do fetch_conversations! conversations_by_id.fetch(id) do raise ArgumentError, "Unable to find a conversation with the ID #{id.inspect}" end end end |
#find_user(id) ⇒ Object
192 193 194 195 196 197 198 199 200 |
# File 'lib/slacks/connection.rb', line 192 def find_user(id) user = users_by_id.fetch(id) do fetch_users! users_by_id.fetch(id) do raise ArgumentError, "Unable to find a user with the ID #{id.inspect}" end end Slacks::User.new(self, user) end |
#find_user_by_nickname(nickname) ⇒ Object
202 203 204 |
# File 'lib/slacks/connection.rb', line 202 def find_user_by_nickname(nickname) find_user to_user_id(nickname) end |
#get_message(channel, ts) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/slacks/connection.rb', line 46 def (channel, ts) params = { channel: to_channel_id(channel), timestamp: ts } api("reactions.get", **params) end |
#listen! ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/slacks/connection.rb', line 95 def listen! response = api("rtm.start") store_context!(response) @websocket = Slacks::Driver.new websocket.connect_to websocket_url trigger "connected" websocket.on(:error) do |event| raise ConnectionError.new(event) end websocket.on(:message) do |data| case data["type"] when NilClass # Every event has a `type` property: # https://api.slack.com/rtm#events # If an event comes across without # one, we'll skill it. next when EVENT_GROUP_JOINED, EVENT_CHANNEL_CREATED conversation = data["channel"] @conversations_by_id[conversation["id"]] = conversation @conversation_ids_by_name[conversation["name"]] = conversation["id"] when EVENT_USER_JOINED user = data["user"] @users_by_id[user["id"]] = user @user_id_by_name[user["name"]] = user["id"] when EVENT_MESSAGE # Don't respond to things that this bot said next if data["user"] == bot.id # ...or to messages with no text next if data["text"].nil? || data["text"].empty? end trigger data["type"], data end websocket.main_loop rescue EOFError # Slack hung up on us, we'll ask for a new WebSocket URL and reconnect. trigger "error", "Websocket Driver received EOF; reconnecting" retry end |
#listening? ⇒ Boolean
89 90 91 |
# File 'lib/slacks/connection.rb', line 89 def listening? !websocket.nil? end |
#ping ⇒ Object
84 85 86 87 |
# File 'lib/slacks/connection.rb', line 84 def ping raise NotListeningError unless listening? websocket.ping end |
#send_message(message, options = {}) ⇒ Object Also known as: say
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/slacks/connection.rb', line 32 def (, ={}) channel = .fetch(:channel) { raise ArgumentError, "Missing parameter :channel" } = Array([:attachments]) params = { channel: to_channel_id(channel), text: , as_user: true, # post as the authenticated user (rather than as slackbot) link_names: 1} # find and link channel names and user names params.merge!(attachments: MultiJson.dump()) if .any? params.merge!(.select { |key, _| SEND_MESSAGE_PARAMS.member?(key) }) api("chat.postMessage", **params) end |
#typing_on(channel) ⇒ Object
79 80 81 82 |
# File 'lib/slacks/connection.rb', line 79 def typing_on(channel) raise NotListeningError unless listening? websocket.write MultiJson.dump(type: "typing", channel: to_channel_id(channel)) end |
#update_message(ts, message, options = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/slacks/connection.rb', line 53 def (ts, , ={}) channel = .fetch(:channel) { raise ArgumentError, "Missing parameter :channel" } = Array([:attachments]) params = { ts: ts, channel: to_channel_id(channel), text: , as_user: true, # post as the authenticated user (rather than as slackbot) link_names: 1} # find and link channel names and user names params.merge!(attachments: MultiJson.dump()) if .any? params.merge!(.select { |key, _| [:username, :as_user, :parse, :link_names, :unfurl_links, :unfurl_media, :icon_url, :icon_emoji].member?(key) }) api("chat.update", **params) end |
#user_exists?(username) ⇒ Boolean
208 209 210 211 212 213 214 |
# File 'lib/slacks/connection.rb', line 208 def user_exists?(username) return false if username.nil? user_id = to_user_id(username) user_id && !user_id.empty? rescue ArgumentError false end |
#users ⇒ Object
216 217 218 219 |
# File 'lib/slacks/connection.rb', line 216 def users fetch_users! if @users_by_id.empty? @users_by_id.values end |