amelia-sendbird-desk-agent
Version:
Unofficial Desk Agent SDK for Sendbird
1 lines • 17.6 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});const tslib_1=require("tslib"),sendbird_1=tslib_1.__importDefault(require("sendbird"));class AmeliaDesk{constructor(t,e){this.DESK_API="",this.GATE_API="https://gate.sendbird.com",this.appId=null,this.region=null,this.deskUser=null,this.token=null,this.remoteAgents=[],this.groups=[],this.ticketFields=[],this.ticketFieldDatas=[],this.customerFields=[],this.customerFieldDatas=[],this.tags=[],this.proactiveChats=[],this.quickRepliesAndCategories=[],this.DESK_API=`https://desk-api-${e}.sendbird.com/api`,this.appId=t,this.region=e,this.initSendbird(this.appId)}init(t,e){this.getToken(this.appId,t,(t,s)=>{this.deskUser=s,this.token=s.token,e(t,this.token)})}getProjets(){return this.deskUser?this.deskUser.pro:null}connectAgentToChat(t,e){if(this.deskUser){for(let s of this.deskUser.agents)if(s.project.sendbirdAppId==t){const t=s.sendbirdId,n=s.sendbirdAccessToken;return void this.connectToSendbird(t,n,(t,s)=>{e(t,s)})}e(new Error("No agent found to connect to chat"),null)}else e(new Error("Invalid token"),null)}addChannelHandler(t){if(this.sb){var e=new this.sb.ChannelHandler;return this.sb.addChannelHandler(t,e),e}return null}getAgentForAppId(t){if(this.deskUser){for(let e of this.deskUser.agents)if(e.project.sendbirdAppId==t)return e;return null}return null}getRemoteAgents(t,e,s,n=null){if(this.deskUser){n||(this.remoteAgents=[]);const i=10,o=n||`${this.DESK_API}/projects/agents/?offset=0&limit=${i}&query=$ { query }`,a={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(o,{method:"GET",headers:a}).then(t=>200!=t.status?null:t.json()).then(n=>{n?(this.remoteAgents.push(...n.results),n.next?this.getRemoteAgents(t,e,s,n.next):s(null,this.remoteAgents)):s(new Error("No data"),this.remoteAgents)}).catch(t=>{s(t,this.remoteAgents)})}else s(new Error("Invalid token"),this.remoteAgents)}getAgent(t,e,s){if(this.deskUser){const n=`${this.DESK_API}/agents/${e}`,i={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(n,{method:"GET",headers:i}).then(t=>200!=t.status?null:t.json()).then(t=>{t?s(null,t):s(new Error("No data"),null)}).catch(t=>{s(t,null)})}else s(new Error("Invalid token"),null)}changeAgentOnlineStatus(t,e,s,n){if(this.deskUser){const i=`${this.DESK_API}/agents/${e}/connection/`,o={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(i,{method:"PATCH",headers:o,body:JSON.stringify({connection:s})}).then(t=>200!=t.status?null:t.json()).then(t=>{t?n(null,!0):n(new Error("No data"),!1)}).catch(t=>{n(t,!1)})}else n(new Error("Invalid token"),!1)}getTickets(t,e,s,n,i,o,a){if(this.deskUser){let r="";for(let t of n)r+="status2="+t+"&";const l=`${this.DESK_API}/projects/tickets/?limit=${s}&offset=${e}&${r}&order=${i}&agent=${o}`,h={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(l,{method:"GET",headers:h}).then(t=>200!=t.status?null:t.json()).then(t=>{t?a(null,t.results):a(new Error("No data"),null)}).catch(t=>{a(t,null)})}else a(new Error("Invalid token"),null)}getTicketsByTag(t,e,s,n,i,o,a,r,l=null){if(this.deskUser){const h=new Date(n),c=new Date(i),d=h.getFullYear()+"-"+h.getMonth()+"-"+h.getDate(),u=c.getFullYear()+"-"+c.getMonth()+"-"+c.getDate();let p=0==o.length?"":"&";for(let t of o)p+="tag="+t+"&";let k=0==a.length?"":"&";for(let t of a)k+="status2="+t+"&";const g=l||`${this.DESK_API}/projects/tickets/?limit=${e}&offset=${s}&order=-created_at&start_date=${d}&end_date=${u}&${p}${k}`,T={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(g,{method:"GET",headers:T}).then(t=>200!=t.status?null:t.json()).then(t=>{t?r(null,t):r(new Error("No data"),null)}).catch(t=>{r(t,null)})}else r(new Error("Invalid token"),null)}getTicketById(t,e,s){if(this.deskUser){const n=`${this.DESK_API}/tickets/${e}`,i={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(n,{method:"GET",headers:i}).then(t=>200!=t.status?null:t.json()).then(t=>{t?s(null,t):s(new Error("No data"),null)}).catch(t=>{s(t,null)})}else s(new Error("Invalid token"),null)}getTicketCountForAgent(t,e,s){if(this.deskUser){const n=`${this.DESK_API}/agents/${e}/ticket_counts/`,i={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(n,{method:"GET",headers:i}).then(t=>200!=t.status?null:t.json()).then(t=>{t?s(null,t):s(new Error("No data"),null)}).catch(t=>{s(t,null)})}else s(new Error("Invalid token"),null)}getTicketFields(t,e,s=null){if(this.deskUser){s||(this.ticketFields=[]);const n=s||`${this.DESK_API}/projects/ticket_fields/?&limit=100&offset=0`,i={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(n,{method:"GET",headers:i}).then(t=>200!=t.status?null:t.json()).then(s=>{s?(this.ticketFields.push(...s.results),s.next?this.getTicketFields(t,e,s.next):e(null,this.ticketFields)):e(new Error("No data"),this.ticketFields)}).catch(t=>{e(t,this.ticketFields)})}else e(new Error("Invalid token"),this.ticketFields)}getTicketFieldData(t,e,s,n=null){if(this.deskUser){n||(this.ticketFieldDatas=[]);const i=n||`${this.DESK_API}/tickets/${e}/ticket_field_datas`,o={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(i,{method:"GET",headers:o}).then(t=>200!=t.status?null:t.json()).then(n=>{n?(this.ticketFieldDatas.push(...n.results),n.next?this.getTicketFieldData(t,e,s,n.next):s(null,this.ticketFieldDatas)):s(new Error("No data"),this.ticketFieldDatas)}).catch(t=>{s(t,this.ticketFieldDatas)})}else s(new Error("Invalid token"),this.ticketFieldDatas)}getCustomerFields(t,e,s=null){if(this.deskUser){s||(this.customerFields=[]);const n=s||`${this.DESK_API}/projects/customer_fields/?&limit=100&offset=0`,i={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(n,{method:"GET",headers:i}).then(t=>200!=t.status?null:t.json()).then(s=>{s?(this.customerFields.push(...s.results),s.next?this.getCustomerFields(t,e,s.next):e(null,this.customerFields)):e(new Error("No data"),this.customerFields)}).catch(t=>{e(t,this.customerFields)})}else e(new Error("Invalid token"),this.customerFields)}getCustomerFieldData(t,e,s,n=null){if(this.deskUser){n||(this.customerFieldDatas=[]);const i=n||`${this.DESK_API}/customers/${e}/customer_field_datas`,o={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(i,{method:"GET",headers:o}).then(t=>200!=t.status?null:t.json()).then(n=>{n?(this.customerFieldDatas.push(...n.results),n.next?this.getCustomerFieldData(t,e,s,n.next):s(null,this.customerFieldDatas)):s(new Error("No data"),this.customerFieldDatas)}).catch(t=>{s(t,this.customerFieldDatas)})}else s(new Error("Invalid token"),this.customerFieldDatas)}getTicketMessages(t,e,s,n,i,o){if(this.deskUser){const a=(new Date).getTime(),r=`${this.GATE_API}/platform/v3/group_channels/${t.channelUrl}/messages?message_ts=${a}&prev_limit=${n}&next_limit=0&presigned_file_url=true&include=false&including_removed=${i}`;fetch(r,{headers:{authorization:s,"content-type":"application/json;charset=UTF-8","app-id":e}}).then(t=>200!=t.status?null:t.json()).then(t=>{t?o(null,t.messages):o(new Error("No data"),null)}).catch(t=>{o(t,null)})}else o(new Error("Invalid token"),this.customerFieldDatas)}createCustomerField(t,e,s,n,i,o,a,r){if(this.deskUser){const l=`${this.DESK_API}/customer_fields`,h={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(l,{method:"POST",headers:h,body:JSON.stringify({description:e,field_type:s,key:n,name:i,options:o,read_only:a})}).then(t=>200!=t.status?null:t.json()).then(t=>{t?r(null,t):r(new Error("No data"),null)}).catch(t=>{r(t,null)})}else r(new Error("Invalid token"),this.customerFieldDatas)}createCustomFieldData(t,e,s,n,i){if(this.deskUser){const o=`${this.DESK_API}/customer_field_datas`,a={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(o,{method:"POST",headers:a,body:JSON.stringify({customer:e,customer_field:s,value:n})}).then(t=>200!=t.status?null:t.json()).then(t=>{t?i(null,t):i(new Error("No data"),null)}).catch(t=>{i(t,null)})}else i(new Error("Invalid token"),this.customerFieldDatas)}createTicketField(t,e,s,n,i,o,a,r){const l=`${this.DESK_API}/ticket_fields`,h={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(l,{method:"POST",headers:h,body:JSON.stringify({description:e,field_type:s,key:n,name:i,options:o,read_only:a})}).then(t=>200!=t.status?null:t.json()).then(t=>{t?r(null,t):r(new Error("No data"),null)}).catch(t=>{r(t,null)})}createTicketFieldData(t,e,s,n,i){if(this.deskUser){const o=`${this.DESK_API}/ticket_field_datas`,a={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(o,{method:"POST",headers:a,body:JSON.stringify({ticket:e,ticket_field:s,value:n})}).then(t=>200!=t.status?null:t.json()).then(t=>{t?i(null,t):i(new Error("No data"),null)}).catch(t=>{i(t,null)})}else i(new Error("Invalid token"),this.customerFieldDatas)}getGroups(t,e=""){if(this.deskUser){e||(this.groups=[]);const s=`${this.DESK_API}/projects/groups/?offset=0&limit=100&token=${e}`,n=this.deskUser.token;fetch(s,{headers:{authorization:"Token "+n,"content-type":"application/json;charset=UTF-8"}}).then(t=>200!=t.status?null:t.json()).then(e=>{e?(this.groups.push(...e.groups),e.next?this.getGroups(t,e.next):t(null,this.groups)):t(new Error("No data"),this.groups)}).catch(e=>{t(e,this.groups)})}else t(new Error("Invalid token"),this.groups)}createTag(t,e,s){if(this.deskUser){const n=`${this.DESK_API}/tags`,i={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(n,{method:"POST",headers:i,body:JSON.stringify({name:e})}).then(t=>200!=t.status?null:t.json()).then(t=>{t?s(null,t):s(new Error("No data"),null)}).catch(t=>{s(t,null)})}else s(new Error("Invalid token"),this.customerFieldDatas)}assignTagsToTicket(t,e,s,n){if(this.deskUser){const i=`${this.DESK_API}/tickets/${e}/add_tag/`,o={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(i,{method:"POST",headers:o,body:JSON.stringify({tag:s})}).then(t=>200!=t.status?null:t.json()).then(t=>{t?n(null,t):n(new Error("No data"),null)}).catch(t=>{n(t,null)})}else n(new Error("Invalid token"),this.customerFieldDatas)}getTags(t,e,s,n,i=null){if(this.deskUser){i||(this.tags=[]);const o=i||`${this.DESK_API}/projects/tags/?limit=100&status=${e}&q=${s}`,a={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(o,{method:"GET",headers:a}).then(t=>200!=t.status?null:t.json()).then(i=>{i?(this.tags.push(...i.results),i.next?this.getTags(t,e,s,n,i.next):n(null,this.tags)):n(new Error("No data"),this.tags)}).catch(t=>{n(t,this.tags)})}else n(new Error("Invalid token"),this.tags)}openTicket(t,e,s){if(this.deskUser){const n=`${this.DESK_API}/tickets/${e}/reopen/`,i={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(n,{method:"PATCH",headers:i}).then(t=>200!=t.status?null:t.json()).then(t=>{t?s(null,t):s(new Error("No data"),null)}).catch(t=>{s(t,null)})}else s(new Error("Invalid token"),null)}setTicketToIdle(t,e,s){if(this.deskUser){const n=`${this.DESK_API}/assignments/${e}`,i={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(n,{method:"PATCH",headers:i,body:JSON.stringify({status:"IDLE"})}).then(t=>200!=t.status?null:t.json()).then(t=>{t?s(null,t):s(new Error("No data"),null)}).catch(t=>{s(t,null)})}else s(new Error("Invalid token"),null)}closeTicket(t,e,s,n){if(this.deskUser){const i=`${this.DESK_API}/tickets/${e}/close/`,o={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(i,{method:"PATCH",headers:o,body:JSON.stringify({closeComment:s})}).then(t=>200!=t.status?null:t.json()).then(t=>{t?n(null,t):n(new Error("No data"),null)}).catch(t=>{n(t,null)})}else n(new Error("Invalid token"),null)}assignTicketToAgent(t,e,s,n){if(this.deskUser){const i=`${this.DESK_API}/tickets/${e}/force_assign/`,o={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(i,{method:"PATCH",headers:o,body:JSON.stringify({agent:s})}).then(t=>200!=t.status?null:t.json()).then(t=>{t?n(null,t):n(new Error("No data"),null)}).catch(t=>{n(t,null)})}else n(new Error("Invalid token"),null)}createQuickReplyCategory(t,e,s){if(this.deskUser){const n=`${this.DESK_API}/quick_reply_categories`,i={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(n,{method:"POST",headers:i,body:JSON.stringify({name:e})}).then(t=>200!=t.status?null:t.json()).then(t=>{t?s(null,t):s(new Error("No data"),null)}).catch(t=>{s(t,null)})}else s(new Error("Invalid token"),null)}createQuickReplyToAllAgents(t,e,s,n){if(this.deskUser){const i=`${this.DESK_API}/quick_replies`,o={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(i,{method:"POST",headers:o,body:JSON.stringify({name:e,message:s,availableType:"AGENT"})}).then(t=>200!=t.status?null:t.json()).then(t=>{t?n(null,t):n(new Error("No data"),null)}).catch(t=>{n(t,null)})}else n(new Error("Invalid token"),null)}createQuickReplyToGroups(t,e,s,n,i){if(this.deskUser){const o=`${this.DESK_API}/quick_replies`,a={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(o,{method:"POST",headers:a,body:JSON.stringify({name:e,message:s,availableType:"GROUP",group:n})}).then(t=>200!=t.status?null:t.json()).then(t=>{t?i(null,t):i(new Error("No data"),null)}).catch(t=>{i(t,null)})}else i(new Error("Invalid token"),null)}createQuickReplyToMeOnly(t,e,s,n,i,o){if(this.deskUser){const a=`${this.DESK_API}/quick_replies`,r={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(a,{method:"POST",headers:r,body:JSON.stringify({name:e,message:s,availableType:"AGENT",availableAgent:n,categories:i})}).then(t=>200!=t.status?null:t.json()).then(t=>{t?o(null,t):o(new Error("No data"),null)}).catch(t=>{o(t,null)})}else o(new Error("Invalid token"),null)}getQuickRepliesAndCategories(t,e,s){if(this.deskUser){s||(this.quickRepliesAndCategories=[]);const n=s||`${this.DESK_API}/quick_reply_categories/?limit=99&offset=0`,i={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(n,{method:"GET",headers:i}).then(t=>200!=t.status?null:t.json()).then(s=>{s?(this.quickRepliesAndCategories.push(...s.results),s.next?this.getQuickRepliesAndCategories(t,e,s.next):e(null,this.quickRepliesAndCategories)):e(new Error("No data"),this.quickRepliesAndCategories)}).catch(t=>{e(t,this.quickRepliesAndCategories)})}else e(new Error("Invalid token"),this.quickRepliesAndCategories)}getQuickReplyCount(t,e){if(this.deskUser){const s=`${this.DESK_API}/quick_replies/count/?limit=0&offset=0&name=`,n={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(s,{method:"GET",headers:n}).then(t=>200!=t.status?null:t.json()).then(t=>{t?e(null,t):e(new Error("No data"),null)}).catch(t=>{e(t,null)})}else e(new Error("Invalid token"),null)}getPractiveChatTickets(t,e,s,n,i=null){if(this.deskUser){i||(this.proactiveChats=[]);const o=i||`${this.DESK_API}/projects/proactive_chat_tickets?limit=${e}&offset=0&order=${s?"-proactive_chat__created_at":"proactive_chat__created_at"}`,a={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(o,{method:"GET",headers:a}).then(t=>200!=t.status?null:t.json()).then(i=>{i?(this.proactiveChats.push(...i.results),i.next?this.getPractiveChatTickets(t,e,s,n,i.next):n(null,this.proactiveChats)):n(new Error("No data"),this.proactiveChats)}).catch(t=>{n(t,this.proactiveChats)})}else n(new Error("Invalid token"),this.proactiveChats)}sendProactiveChat(t,e,s,n){if(this.deskUser){const i=`${this.DESK_API}/projects/proactive_chat_tickets/follow_up_message`,o={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(i,{method:"POST",headers:o,body:JSON.stringify({message:s,ticketId:e})}).then(t=>200!=t.status?null:t.json()).then(t=>{t?n(null,t):n(new Error("No data"),null)}).catch(t=>{n(t,null)})}else n(new Error("Invalid token"),null)}getProactiveChats(t,e,s,n,i){if(this.deskUser){const o=i||`${this.DESK_API}/projects/proactive_chat_tickets?limit=${s}&offset=${e}&order=-proactive_chat__created_at`,a={"content-type":"application/json;charset=UTF-8",authorization:"Token "+this.token,pid:t};fetch(o,{method:"GET",headers:a}).then(t=>200!=t.status?null:t.json()).then(t=>{t?n(null,t):n(new Error("No data"),null)}).catch(t=>{n(t,null)})}else n(new Error("Invalid token"),null)}getAssignmentLogs(){}getRealTimeMonitoring(){}getPerformanceReport(){}getToken(t,e,s){const n=`${this.DESK_API}/auth/standalone/`;fetch(n,{method:"POST",headers:{"content-type":"application/json;charset=UTF-8"},body:JSON.stringify({dashboardToken:e,appId:t})}).then(t=>200!=t.status?null:t.json()).then(t=>{t?s(null,t):s(new Error("No data"),null)}).catch(t=>{s(t,null)})}initSendbird(t){this.sb=new sendbird_1.default({appId:t})}connectToSendbird(t,e,s){this.sb?this.sb.connect(t,e,(t,e)=>{this.sendbirdUser=t,s(e,t)}):s(new Error("SendBird is not present."),null)}}exports.default=AmeliaDesk;