module MosEisley::SlackWeb

Constants

BASE_URL

Public Class Methods

auth_test() click to toggle source
# File lib/slack.rb, line 149
def self.auth_test
  post_to_slack('auth.test', '')
end
chat_memessage(channel:, text:) click to toggle source
# File lib/slack.rb, line 46
def self.chat_memessage(channel:, text:)
  data = {channel: channel, text: text}
  post_to_slack('chat.meMessage', data)
end
chat_postephemeral(channel:, blocks: nil, text: nil, thread_ts: nil) click to toggle source
# 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) click to toggle source
# File lib/slack.rb, line 55
def self.chat_postmessage(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) click to toggle source
# File lib/slack.rb, line 59
def self.chat_schedulemessage(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) click to toggle source
# 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
conversations_members(channel:, cursor: nil, limit: nil) click to toggle source
# File lib/slack.rb, line 123
def self.conversations_members(channel:, cursor: nil, limit: nil)
  params = {channel: channel}
  params[:cursor] = cursor if cursor
  params[:limit] = limit if limit
  get_from_slack('conversations.members', params)
end
post_log(blocks: nil, text: nil) click to toggle source
# 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
    chat_postmessage(d)
  else
    return nil
  end
end
post_response_url(url, payload) click to toggle source
# File lib/slack.rb, line 78
def self.post_response_url(url, payload)
  post_to_slack(nil, payload, url)
end
users_info(user) click to toggle source
# File lib/slack.rb, line 130
def self.users_info(user)
  get_from_slack('users.info', {user: user})
end
users_list(cursor: nil, limit: nil) click to toggle source
# File lib/slack.rb, line 134
def self.users_list(cursor: nil, limit: nil)
  params = {include_locale: true}
  params[:cursor] = cursor if cursor
  params[:limit] = limit if limit
  get_from_slack('users.list', params)
end
users_lookupbyemail(email) click to toggle source
# File lib/slack.rb, line 141
def self.users_lookupbyemail(email)
  get_from_slack('users.lookupByEmail', {email: email})
end
users_profile_get(user) click to toggle source
# File lib/slack.rb, line 145
def self.users_profile_get(user)
  get_from_slack('users.profile.get', {user: user})
end
views_open(trigger_id:, view:) click to toggle source
# 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:) click to toggle source
# File lib/slack.rb, line 120
def self.views_push(trigger_id:, view:)
end
views_update(view_id:, view:, hash: nil) click to toggle source
# 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

Private Class Methods

get_from_slack(m, params) click to toggle source
# File lib/slack.rb, line 155
def self.get_from_slack(m, params)
  l = MosEisley.logger
  url ||= BASE_URL + m
  head = {authorization: "Bearer #{ENV['SLACK_BOT_ACCESS_TOKEN']}"}
  r = Neko::HTTP.get(url, params, head)
  if r[:code] != 200
    l.warn("#{m} HTTP failed: #{r[:message]}")
    return nil
  end
  begin
    h = JSON.parse(r[:body], {symbolize_names: true})
    if h[:ok]
      return h
    else
      l.warn("#{m} Slack failed: #{h[:error]}")
      l.debug("#{h[:response_metadata]}")
      return nil
    end
  rescue
    return {body: r[:body]}
  end
end
post_to_slack(method, data, url = nil) click to toggle source
# File lib/slack.rb, line 178
def self.post_to_slack(method, data, url = nil)
  l = MosEisley.logger
  url ||= BASE_URL + method
  head = {authorization: "Bearer #{ENV['SLACK_BOT_ACCESS_TOKEN']}"}
  r = Neko::HTTP.post_json(url, data, head)
  if r[:code] != 200
    l.warn("post_to_slack HTTP failed: #{r[:message]}")
    return nil
  end
  begin
    h = JSON.parse(r[:body], {symbolize_names: true})
    if h[:ok]
      return h
    else
      l.warn("post_to_slack Slack failed: #{h[:error]}")
      l.debug("#{h[:response_metadata]}")
      return nil
    end
  rescue
    return {body: r[:body]}
  end
end