This repository was archived by the owner on Dec 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathclient.rb
More file actions
107 lines (90 loc) · 2.97 KB
/
client.rb
File metadata and controls
107 lines (90 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
74
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
100
101
102
103
104
105
106
107
require 'net/http'
module Slack
INVITE_PATH = '/api/users.admin.invite'.freeze
POST_MESSAGE_PATH = '/api/chat.postMessage'.freeze
USERS_LIST_PATH = '/api/users.list'.freeze
LOOKUP_BY_EMAIL_PATH = '/api/users.lookupByEmail'.freeze
BOT_USERNAME = 'OpCodeBot'.freeze
class Client
RequestFailed = Class.new(StandardError)
InviteFailed = Class.new(StandardError)
PostMessageFailed = Class.new(StandardError)
FetchUsersListFailed = Class.new(StandardError)
LookupUserEmailFailed = Class.new(StandardError)
attr_reader :domain
def initialize(subdomain:, token:)
@subdomain = subdomain
@token = token
@domain = "#{subdomain}.slack.com"
end
def invite(extra_message:, email:, channels: [])
# unsure if some string expansion is causing an error here
Rails.logger.info 'Inviting slack user user'
Rails.logger.info "Inviting slack user user with email #{email}"
body = send_api_request(
to: INVITE_PATH,
payload: {
email: email,
channels: channels.join(','),
token: @token,
set_active: 'true',
extra_message: extra_message,
_attempts: 1
}
)
if !(body['ok'] || %w(already_in_team already_invited sent_recently).include?(body['error']))
raise InviteFailed.new(body.to_s)
end
true
rescue StandardError => e
Rails.logger.warn "Some Exception occured while inviting slack user #{e}"
# want to reraise the exception so the job retries
raise
end
def post_message_to(channel:, with_text:)
body = send_api_request(
to: POST_MESSAGE_PATH,
payload: {
token: @token,
channel: channel,
text: with_text,
username: BOT_USERNAME
}
)
return true if body['ok'] == true || body['ok'] == 'true'
raise PostMessageFailed.new(body.to_s)
end
def validate_user_email(email:)
body = send_api_request(
to: LOOKUP_BY_EMAIL_PATH,
payload: {
token: @token,
email: email
}
)
raise LookupUserEmailFailed.new(body.to_s) unless body['ok'] == true || body['ok'] == 'true'
end
def fetch_users_list(presence: 0)
body = send_api_request(
to: USERS_LIST_PATH,
payload: {
token: @token,
presence: presence
}
)
return body if body['ok'] == true || body['ok'] == 'true'
raise FetchUsersListFailed.new(body.to_s)
end
private
def send_api_request(to:, payload:)
Rails.logger.info "Sending payload '#{payload}' to '#{to}'"
res = Net::HTTP.start(@domain, 443, use_ssl: true) do |http|
req = Net::HTTP::Post.new("#{to}?t=#{Time.now.to_i}")
req.set_form_data payload
http.request(req)
end
raise RequestFailed.new("HTTP status code: #{res.code}") unless res.is_a?(Net::HTTPSuccess)
JSON.parse(res.body)
end
end
end