Created
March 6, 2024 04:19
-
-
Save jeznag/cd005039b60a52f959daf9bcb6e69158 to your computer and use it in GitHub Desktop.
Zoho Desk integration via Zoho Flow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
map check_that_message_can_be_sent(int ticket_id, string sms_content, string send_btn_state, string channel, int desk_org_id) | |
{ | |
try | |
{ | |
if(channel != "SMS via Zoho Flow" || sms_content.isEmpty() || send_btn_state != "true") | |
{ | |
info "stop processing outbound"; | |
info "channel> " + channel; | |
info "sms_content> " + sms_content; | |
info "send_btn_state> " + send_btn_state; | |
return {"error":"Stop processing because of input validation","mobile":""}; | |
} | |
ticket = zoho.desk.getRecordById(desk_org_id,"tickets",ticket_id,"zoho_desk_sms"); | |
if(ticket.isEmpty()) | |
{ | |
info "no ticket found for " + ticket_id; | |
return {"error":"No ticket found","mobile":""}; | |
} | |
info ticket; | |
contact_id = ticket.get("contactId"); | |
contact = zoho.desk.getRecordById(desk_org_id,"contacts",contact_id,"zoho_desk_sms"); | |
contact_mobile = contact.get("mobile"); | |
if(contact_mobile.isEmpty()) | |
{ | |
info "no contact mobile found for> " + contact.get("id"); | |
return {"error":"No mobile found","mobile":""}; | |
} | |
return {"mobile":contact_mobile,"error":""}; | |
} | |
catch (e) | |
{ | |
info e; | |
return {"error":e,"mobile":""}; | |
} | |
return {"error":"something wrong","mobile":""}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void create_comment_and_clear_outbound_value(int ticket_id, string sms_content, int desk_org_id) | |
{ | |
// NB - make sure the zoho.com/zoho.com.au URL matches the data center | |
try | |
{ | |
comment_params = Map(); | |
comment_params.put("content","Outbound SMS: " + sms_content); | |
comment_params.put("contentType","plainText"); | |
comment_params.put("isPublic",true); | |
headerMap = Map(); | |
headerMap.put("orgId",desk_org_id); | |
response = invokeurl | |
[ | |
url :"https://desk.zoho.com.au/api/v1/tickets/" + ticket_id + "/comments" | |
type :POST | |
parameters:comment_params.toString() | |
headers:headerMap | |
connection:"zoho_desk_sms" | |
]; | |
info response; | |
// Go to layout, click edit properties of the field and check the API Name | |
// below is an example payload if you API name is "cf_send_sms" and "cf_sms_content" (note the cf top level key) | |
// ref: https://help.zoho.com/portal/en/community/topic/can-zoho-support-api-retrieve-data-from-my-custom-fields | |
update_value = {"cf":{"cf_send_sms":"false","cf_sms_content_to_send":""}}; | |
update_resp = zoho.desk.update(desk_org_id,"tickets",ticket_id,update_value,"zoho_desk_sms"); | |
info update_resp; | |
} | |
catch (e) | |
{ | |
info e; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void handle_receive_inbound(map webhook_payload, int desk_org_id) | |
{ | |
try | |
{ | |
info webhook_payload; | |
source_number = webhook_payload.get("from_number"); | |
message_content = webhook_payload.get("inbound_message"); | |
message_id = webhook_payload.get("message_id"); | |
// found existing desk contact by mobile value | |
search_value = {"mobile":source_number}; | |
contact_res = zoho.desk.searchRecords(desk_org_id,"contacts",search_value,0,5,"zoho_desk_sms"); | |
info "contact_res" + contact_res; | |
contact = Map(); | |
ticket = Map(); | |
expected_subject = "SMS with " + source_number; | |
if(contact_res.isEmpty()) | |
{ | |
// create new contact | |
contact_map = Map(); | |
contact_map.put("mobile",source_number); | |
contact_map.put("lastName","SMS with " + source_number); | |
contact = zoho.desk.create(desk_org_id,"contacts",contact_map,"zoho_desk_sms"); | |
} | |
else | |
{ | |
contact = contact_res.get("data").get(0); | |
// use get records first because search records returns cached data and won't find recently created tickets (e.g. from the first SMS) | |
recently_created_tickets = zoho.desk.getRecords(desk_org_id,"tickets",0,10,{"sortBy":"-createdTime"},"zoho_desk_sms"); | |
info recently_created_tickets.size(); | |
info recently_created_tickets; | |
if(recently_created_tickets.size() > 0) | |
{ | |
ticket_list = recently_created_tickets.get("data"); | |
for each recent_ticket in ticket_list | |
{ | |
recent_ticket_data = recent_ticket.toMap(); | |
recent_ticket_subject = ifnull(recent_ticket_data.get("Subject"),recent_ticket_data.get("subject")); | |
info "subj " + recent_ticket_subject; | |
if(recent_ticket_subject != null && recent_ticket_subject == expected_subject) | |
{ | |
ticket = recent_ticket; | |
info "recent_ticket matched 41;" + recent_ticket; | |
} | |
} | |
} | |
if(ticket.isEmpty()) | |
{ | |
// find related ticket to this contact | |
ticket_search_map = {"subject":expected_subject}; | |
ticket_res = zoho.desk.searchRecords(desk_org_id,"tickets",ticket_search_map,0,5,"zoho_desk_sms"); | |
if(!ticket_res.isEmpty()) | |
{ | |
ticket_data = zoho.desk.getRecordById(desk_org_id,"tickets",ticket_res.get("data").get(0).get("id"),"zoho_desk_sms"); | |
if(ticket_data.isEmpty() == false) | |
{ | |
info "Found ticket via search records"; | |
ticket = ticket_data; | |
} | |
} | |
} | |
} | |
// info ticket; | |
// create a ticket if none existed | |
if(ticket.isEmpty()) | |
{ | |
// departmentId is required when creating ticket, but which one to use? | |
// for now we pickup the one has is_default. | |
// Customer can also hard code the departmentId as another parameter | |
departments_res = zoho.desk.getRecords(desk_org_id,"departments",1,10,{"":""},"zoho_desk_sms"); | |
department_lists = departments_res.get("data"); | |
default_department = Map(); | |
for each department in department_lists | |
{ | |
if(department.get("isDefault")) | |
{ | |
default_department = department; | |
} | |
} | |
if(default_department.isEmpty()) | |
{ | |
default_department = department_lists.get(0); | |
} | |
ticket_values = Map(); | |
ticket_values.put("subject",expected_subject); | |
ticket_values.put("description",message_content); | |
ticket_values.put("contactId",contact.get("id")); | |
ticket_values.put("channel","SMS via Zoho Flow"); | |
ticket_values.put("departmentId",default_department.get("id")); | |
res = zoho.desk.create(desk_org_id,"tickets",ticket_values,"zoho_desk_sms"); | |
info "created res"; | |
info res; | |
} | |
else | |
{ | |
// append the new inbound as comment to the ticket | |
// get the assigned agent or team zuid for mention feature | |
// find assigned team first | |
mention_content = ""; | |
info "ticket found" + ticket; | |
if(ticket.get("teamId") != null) | |
{ | |
team_id = ticket.get("teamId"); | |
team_name = ticket.get("team").get("name"); | |
mention_content = "zsu[@team:" + team_id + "_" + team_name + "]zsu "; | |
} | |
else if(ticket.get("assigneeId") != null) | |
{ | |
assignee_zuid = ticket.get("assignee").get("zuid"); | |
mention_content = "zsu[@user:" + assignee_zuid + "]zsu "; | |
} | |
// create the comment | |
ticket_id = ticket.get("id"); | |
comment_params = Map(); | |
comment_params.put("content",mention_content + "Inbound message: " + message_content); | |
// comment_params.put("contentType","plainText"); | |
comment_params.put("isPublic",true); | |
headerMap = Map(); | |
headerMap.put("orgId",desk_org_id); | |
response = invokeurl | |
[ | |
url :"https://desk.zoho.com.au/api/v1/tickets/" + ticket_id + "/comments" | |
type :POST | |
parameters:comment_params.toString() | |
headers:headerMap | |
connection:"zoho_desk_sms" | |
]; | |
} | |
} | |
catch (e) | |
{ | |
info e; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment