-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslack_invite.rb
44 lines (35 loc) · 1 KB
/
slack_invite.rb
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
class SlackInvite < Sinatra::Base
SLACK_API_ENDPOINT = "https://#{ENV['SLACK_TEAM_NAME']}.slack.com/api/users.admin.invite"
set :public_folder => "public", :static => true
get "/invite.json" do
class Invitee
include ActiveModel::Validations
attr_accessor :email
validates :email, :presence => true, :email => true
end
invitee = Invitee.new
invitee.email = params[:email]
if invitee.valid?
post_params = {
email: invitee.email,
channels: ENV['SLACK_CHANNEL'],
set_active: true,
_attempts: 1,
token: ENV['SLACK_TEAM_AUTH_TOKEN']
}
http_response = HTTParty.post(SLACK_API_ENDPOINT, body: post_params)
data = http_response.body
else
data = {'ok': false, 'error': 'invalid_email'}.to_json
end
callback = params.delete('callback')
if callback
content_type :js
response = "#{callback}(#{data})"
else
content_type :json
response = data
end
response
end
end