Module: SlackMessage::Api

Extended by:
Api
Included in:
Api
Defined in:
lib/slack_message/api.rb

Instance Method Summary collapse

Instance Method Details

#delete(message, profile) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/slack_message/api.rb', line 101

def delete(message, profile)
  params = if message.scheduled?
    {
      channel: message.channel,
      scheduled_message_id: message.scheduled_message_id,
    }
  else
    {
      channel: message.channel,
      ts: message.timestamp,
    }
  end

  if SlackMessage::Configuration.debugging?
    warn params.inspect
  end

  response = delete_message(profile, params)

  SlackMessage::ErrorHandling.raise_delete_response_errors(response, message, profile)
  response
end

#post(payload, target, profile, time) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/slack_message/api.rb', line 32

def post(payload, target, profile, time)
  params  = {
    channel: target,
    username: payload.custom_bot_name || profile[:name],
    blocks: payload.render,
    text: payload.custom_notification,
  }

  if params[:blocks].length == 0
    raise ArgumentError, "Tried to send an entirely empty message."
  end

  if params[:blocks].length > 50
    raise ArgumentError, "Message cannot contain more than 50 blocks."
  end

  icon = payload.custom_bot_icon || profile[:icon]
  if icon =~ /^:\w+:$/
    params[:icon_emoji] = icon
  elsif icon =~ /^(https?:\/\/)?[0-9a-z]+\.[-_0-9a-z]+/ # very naive regex, I know. it'll be fine.
    params[:icon_url] = icon
  elsif !(icon.nil? || icon == '')
    raise ArgumentError, "Couldn't figure out icon '#{icon}'. Try :emoji: or a URL."
  end

  if !time.nil?
    params[:post_at] = time.to_i

    if payload.custom_bot_name || payload.custom_bot_icon
      raise ArgumentError, "Sorry, setting an image / emoji icon for scheduled messages isn't supported."
    end
  end

  if SlackMessage::Configuration.debugging?
    warn params.inspect
  end

  response = post_message(profile, params)

  SlackMessage::ErrorHandling.raise_post_response_errors(response, params, profile)
  SlackMessage::Response.new(response, profile[:handle])
end

#update(payload, message, profile) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/slack_message/api.rb', line 75

def update(payload, message, profile)
  params  = {
    channel: message.channel,
    ts: message.timestamp,
    blocks: payload.render,
    text: payload.custom_notification
  }

  if params[:blocks].length == 0
    raise ArgumentError, "Tried to send an entirely empty message."
  end

  if params[:blocks].length > 50
    raise ArgumentError, "Message cannot contain more than 50 blocks."
  end

  if SlackMessage::Configuration.debugging?
    warn params.inspect
  end

  response = update_message(profile, params)

  SlackMessage::ErrorHandling.raise_update_response_errors(response, params, profile)
  SlackMessage::Response.new(response, profile[:handle])
end

#user_id_for(email, profile) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/slack_message/api.rb', line 8

def user_id_for(email, profile)
  unless email =~ SlackMessage::EMAIL_PATTERN
    raise ArgumentError, "Tried to find profile by invalid email address '#{email}'"
  end

  if SlackMessage::Configuration.debugging?
    warn [email, profile].inspect
  end

  response = look_up_user_by_email(email, profile)

  if response.code != "200"
    raise SlackMessage::ApiError, "Got an error back from the Slack API (HTTP #{response.code}):\n#{response.body}"
  elsif response.body == ""
    raise SlackMessage::ApiError, "Received empty 200 response from Slack when looking up user info. Check your API key."
  end

  SlackMessage::ErrorHandling.raise_user_lookup_response_errors(response, email, profile)

  payload = JSON.parse(response.body)

  payload["user"]["id"]
end