This repository has been archived by the owner on Sep 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment.rb
48 lines (43 loc) · 1.5 KB
/
assignment.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
45
46
47
48
class Assignment
def initialize(conversation_id)
load_config
@base_url = "https://api.intercom.io/"
@headers = {"Authorization" => "Bearer " + ENV['IC_ACCESS_TOKEN'], "Accept" => "application/json"}
@conversation_id = conversation_id
@assigner_id = ENV['ASSIGNER_ID'] || @config['assigner_id']
@teams = @config['teams']
process
end
def load_config
if file = File.read('config.json')
@config = JSON.parse(file)
else
puts "An error occured, have you created a config file by running init.rb?"
halt 500
end
end
def process
conversation = fetch_conversation
id = find_last_inbox (conversation)
end
def fetch_conversation
conversation = HTTParty.get(URI.encode(@base_url + 'conversations/' + @conversation_id), :headers => @headers)
return conversation
end
def find_last_inbox (conversation)
conversation['conversation_parts']['conversation_parts'].reverse.each_with_index do |part, i|
assigned = part['assigned_to']['id'] unless part['assigned_to'].nil?
if (i == 0) || (i == 1)
next
end
if @teams.include? assigned || assigned.nil?
assign(assigned) unless assigned.nil?
break
end
end
end
def assign (id)
params = { "body": "", "type": "admin", "admin_id": @assigner_id, "assignee_id": id, "message_type": "assignment"}
conversation = HTTParty.post(URI.encode(@base_url + 'conversations/' + @conversation_id + '/reply'), :headers => @headers, :query => params)
end
end