Module: MosEisley::SlackWeb
- Defined in:
- lib/slack.rb
Constant Summary collapse
- BASE_URL =
'https://slack.com/api/'.freeze
Class Method Summary collapse
- .auth_test ⇒ Object
- .chat_memessage(channel:, text:) ⇒ Object
- .chat_postephemeral(channel:, blocks: nil, text: nil, thread_ts: nil) ⇒ Object
- .chat_postmessage(channel:, blocks: nil, text: nil, thread_ts: nil) ⇒ Object
- .chat_schedulemessage(channel:, post_at:, blocks: nil, text: nil, thread_ts: nil) ⇒ Object
- .chat_send(m, channel, blocks, text, thread_ts, post_at = nil) ⇒ Object
- .post_log(blocks: nil, text: nil) ⇒ Object
- .post_response_url(url, payload) ⇒ Object
- .users_info(user) ⇒ Object
- .users_list(cursor: nil, limit: nil) ⇒ Object
- .users_lookupbyemail(email) ⇒ Object
- .users_profile_get(user) ⇒ Object
- .users_send(m, params) ⇒ Object
- .views_open(trigger_id:, view:) ⇒ Object
- .views_push(trigger_id:, view:) ⇒ Object
- .views_update(view_id:, view:, hash: nil) ⇒ Object
Class Method Details
.auth_test ⇒ Object
166 167 168 |
# File 'lib/slack.rb', line 166 def self.auth_test post_to_slack('auth.test', '') end |
.chat_memessage(channel:, text:) ⇒ Object
46 47 48 49 |
# File 'lib/slack.rb', line 46 def self.(channel:, text:) data = {channel: channel, text: text} post_to_slack('chat.meMessage', data) end |
.chat_postephemeral(channel:, blocks: nil, text: nil, thread_ts: nil) ⇒ Object
51 52 53 |
# File 'lib/slack.rb', line 51 def self.chat_postephemeral(channel:, blocks: nil, text: nil, thread_ts: nil) chat_send(:postEphemeral, channel, blocks, text, thread_ts) end |
.chat_postmessage(channel:, blocks: nil, text: nil, thread_ts: nil) ⇒ Object
55 56 57 |
# File 'lib/slack.rb', line 55 def self.(channel:, blocks: nil, text: nil, thread_ts: nil) chat_send(:postMessage, channel, blocks, text, thread_ts) end |
.chat_schedulemessage(channel:, post_at:, blocks: nil, text: nil, thread_ts: nil) ⇒ Object
59 60 61 |
# File 'lib/slack.rb', line 59 def self.(channel:, post_at:, blocks: nil, text: nil, thread_ts: nil) chat_send(:scheduleMessage, channel, blocks, text, thread_ts, post_at) end |
.chat_send(m, channel, blocks, text, thread_ts, post_at = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/slack.rb', line 63 def self.chat_send(m, channel, blocks, text, thread_ts, post_at = nil) data = {channel: channel} if m == :scheduleMessage post_at ? data[:post_at] = post_at : raise end if blocks data[:blocks] = blocks data[:text] = text if text else text ? data[:text] = text : raise end data[:thread_ts] = thread_ts if thread_ts post_to_slack("chat.#{m}", data) end |
.post_log(blocks: nil, text: nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/slack.rb', line 82 def self.post_log(blocks: nil, text: nil) if c = ENV['SLACK_LOG_CHANNEL_ID'] d = {channel: c} if blocks d[:blocks] = blocks if text d[:text] = text end else if text d[:text] = text else return nil end end (d) else return nil end end |
.post_response_url(url, payload) ⇒ Object
78 79 80 |
# File 'lib/slack.rb', line 78 def self.post_response_url(url, payload) post_to_slack(nil, payload, url) end |
.users_info(user) ⇒ Object
123 124 125 |
# File 'lib/slack.rb', line 123 def self.users_info(user) users_send('info', {user: user}) end |
.users_list(cursor: nil, limit: nil) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/slack.rb', line 127 def self.users_list(cursor: nil, limit: nil) params = {include_locale: true} params[:cursor] = cursor if cursor params[:limit] = limit if limit users_send('list', params) end |
.users_lookupbyemail(email) ⇒ Object
134 135 136 |
# File 'lib/slack.rb', line 134 def self.users_lookupbyemail(email) users_send('lookupByEmail', {email: email}) end |
.users_profile_get(user) ⇒ Object
138 139 140 |
# File 'lib/slack.rb', line 138 def self.users_profile_get(user) users_send('profile.get', {user: user}) end |
.users_send(m, params) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/slack.rb', line 142 def self.users_send(m, params) l = MosEisley.logger call = "users.#{m}" url ||= BASE_URL + call head = {authorization: "Bearer #{ENV['SLACK_BOT_ACCESS_TOKEN']}"} r = Neko::HTTP.get(url, params, head) if r[:code] != 200 l.warn("#{call} HTTP failed: #{r[:message]}") return nil end begin h = JSON.parse(r[:body], {symbolize_names: true}) if h[:ok] return h else l.warn("#{call} Slack failed: #{h[:error]}") l.debug("#{h[:response_metadata]}") return nil end rescue return {body: r[:body]} end end |
.views_open(trigger_id:, view:) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/slack.rb', line 103 def self.views_open(trigger_id:, view:) data = { trigger_id: trigger_id, view: view, } post_to_slack('views.open', data) end |
.views_push(trigger_id:, view:) ⇒ Object
120 121 |
# File 'lib/slack.rb', line 120 def self.views_push(trigger_id:, view:) end |
.views_update(view_id:, view:, hash: nil) ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/slack.rb', line 111 def self.views_update(view_id:, view:, hash: nil) data = { view_id: view_id, view: view, } data[:hash] if hash post_to_slack('views.update', data) end |