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
- #channels ⇒ Object
- #find_channel(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
- #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.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/slacks/connection.rb', line 19 def initialize(token, ={}) @token = token @typing_speed = .fetch(:typing_speed, 100.0) @user_ids_dm_ids = {} @users_by_id = {} @user_id_by_name = {} @groups_by_id = {} @group_id_by_name = {} @channels_by_id = {} @channel_id_by_name = {} end |
Instance Attribute Details
#bot ⇒ Object (readonly)
Returns the value of attribute bot.
16 17 18 |
# File 'lib/slacks/connection.rb', line 16 def bot @bot end |
#team ⇒ Object (readonly)
Returns the value of attribute team.
16 17 18 |
# File 'lib/slacks/connection.rb', line 16 def team @team end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
16 17 18 |
# File 'lib/slacks/connection.rb', line 16 def token @token end |
#typing_speed ⇒ Object
Returns the value of attribute typing_speed.
17 18 19 |
# File 'lib/slacks/connection.rb', line 17 def typing_speed @typing_speed end |
Instance Method Details
#add_reaction(emojis, message) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/slacks/connection.rb', line 71 def add_reaction(emojis, ) Array(emojis).each do |emoji| api("reactions.add", { name: emoji.gsub(/^:|:$/, ""), channel: .channel.id, timestamp: . }) end end |
#channels ⇒ Object
144 145 146 |
# File 'lib/slacks/connection.rb', line 144 def channels user_id_by_name.keys + group_id_by_name.keys + channel_id_by_name.keys end |
#find_channel(id) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/slacks/connection.rb', line 150 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 } when /^G/ Slacks::Channel.new self, groups_by_id.fetch(id) do raise ArgumentError, "Unable to find a group with the ID #{id.inspect}" end else Slacks::Channel.new self, channels_by_id.fetch(id) do raise ArgumentError, "Unable to find a channel with the ID #{id.inspect}" end end end |
#find_user(id) ⇒ Object
170 171 172 173 174 |
# File 'lib/slacks/connection.rb', line 170 def find_user(id) Slacks::User.new self, users_by_id.fetch(id) do raise ArgumentError, "Unable to find a user with the ID #{id.inspect}" end end |
#find_user_by_nickname(nickname) ⇒ Object
176 177 178 |
# File 'lib/slacks/connection.rb', line 176 def find_user_by_nickname(nickname) find_user to_user_id(nickname) end |
#get_message(channel, ts) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/slacks/connection.rb', line 49 def (channel, ts) params = { channel: to_channel_id(channel), timestamp: ts } api("reactions.get", params) end |
#listen! ⇒ Object
86 87 88 89 90 91 92 93 94 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 |
# File 'lib/slacks/connection.rb', line 86 def listen! response = api("rtm.start") unless response["ok"] raise MigrationInProgress if response["error"] == "migration_in_progress" raise ResponseError.new(response, response["error"]) end 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 EVENT_GROUP_JOINED group = data["channel"] @groups_by_id[group["id"]] = group @group_id_by_name[group["name"]] = group["id"] when EVENT_USER_JOINED user = data["user"] @users_by_id[user["id"]] = user @user_id_by_name[user["name"]] = user["id"] when EVENT_CHANNEL_CREATED channel = data["channel"] @channels_by_id[channel["id"]] = channel @channel_id_by_name[channel["name"]] = channel["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 |
#send_message(message, options = {}) ⇒ Object Also known as: say
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/slacks/connection.rb', line 34 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, _| [:username, :as_user, :parse, :link_names, :unfurl_links, :unfurl_media, :icon_url, :icon_emoji].member?(key) }) api("chat.postMessage", params) end |
#typing_on(channel) ⇒ Object
80 81 82 |
# File 'lib/slacks/connection.rb', line 80 def typing_on(channel) websocket.write MultiJson.dump(type: "typing", channel: to_channel_id(channel)) end |
#update_message(ts, message, options = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/slacks/connection.rb', line 56 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
182 183 184 185 186 187 |
# File 'lib/slacks/connection.rb', line 182 def user_exists?(username) return false if username.nil? to_user_id(username).present? rescue ArgumentError false end |
#users ⇒ Object
189 190 191 192 |
# File 'lib/slacks/connection.rb', line 189 def users fetch_users! if @users_by_id.empty? @users_by_id.values end |