`)
}
}
})
scrollDown()
})
startNotification()
}
/**
* REALIZA O ENVIO DE MENSAGEM ATRAVEZ DA API
* @param {*} obj
* @returns
*/
const sendMessage = (obj = {}) => {
const sendNumber = localStorage.getItem('session_window')
const myUniqueid = localStorage.getItem('my_uniqueid')
const agent = JSON.parse(localStorage.getItem('obj_status'))
let sendContent = (typeof obj.fileContent === "undefined") ? $('#fieldsendmessage').val() : obj.content
let name = agent.data.nome ? agent.data.nome : 'Atendente'
let uniqueid = localStorage.getItem('session_uniqueid')
let media = obj.media ? obj.media : 'whatsapp'
let type = obj.type ? obj.type : 'text'
let mimetype = obj.mimetype ? obj.mimetype : 'text'
let filename = obj.filename ? obj.filename : Date.now()
if(!sendContent){
return
}
let dataSend = {
"event": {
"type": "mensagem",
"contact": {
"name": name,
"number": myUniqueid,
"matricula": myUniqueid
},
"mensagem": {
"uniqueid": uniqueid,
"dst": sendNumber,
"id_provedor": `${uniqueid}_${Date.now()}`,
"type": type,
"mimetype": mimetype,
"media": media,
"content": sendContent,
"status": "sended"
}
}
}
if(type != 'audio'){
dataSend.event.mensagem.file_name = filename
}
enviarMensagem(dataSend)
let msgContent = type == 'text' ? sendContent : obj.fileContent
$('.chat-window').append(`
${msgContent} ${converdata(Date.now())}
`)
scrollDown()
/** LIMPA O CAMPO DE ENVIO DE MENSAGEM */
$('#fieldsendmessage').val("")
}
/**
* ATUALIZA AS MENSAGEM QUE SÃO RECEBIDAS NA TELA DO ATENDIMENTO
* @param {*} ev
*/
const viewMessage = (ev) => {
const sessionOpen = localStorage.getItem('session_uniqueid')
const datesend = ev.event?.mensagem.datetime ? converdata(ev.event?.mensagem.datetime, true) : 'algumas horas';
if(ev.event?.mensagem.uniqueid == sessionOpen){
marcarMensagemVista(sessionOpen)
switch(ev.event?.mensagem.type){
case 'text':
$('.chat-window').append(`
${ev.event.mensagem.content} ${datesend}
`)
break
case 'finish':
case 're_start':
$('.chat-window').append(`
${ev.event.mensagem.content}
`)
break
}
const mediaDownload = ["image", "voice", "document", "audio", "video", "sticker"]
if(mediaDownload.indexOf(ev.event?.mensagem.type) >= 0){
const sendobj = {
filename: ev.event?.mensagem.file_name,
id_provedor: ev.event?.mensagem.id_provedor,
type: ev.event?.mensagem.type,
mimetype: ev.event?.mensagem.mimetype,
from: 'receiver'
}
messageTypeMedia(sendobj)
}
scrollDown()
}
}
/**
* APRESENTA AS NOVAS NOTIFICACOES DE ATENDIMENTO NA TELA DO ATENDENTE
* -> CADA ATENDIMENTO DEVE POSSUIR APENAS UMA NOTIFICACAO
* @param {*} data
*/
const receiveNotification = (data) => {
let validate = null
switch(data.event?.type){
case "mensagem":
if(data.event.mensagem.uniqueid != localStorage.getItem('session_uniqueid')){
notifyMe(data.event.contact.name, {
body: data.event.mensagem.content,
icon: `images/${data.event.mensagem.media}.png`,
silent: true
})
soundNotification(`integracao/simpleschat/sound/notification.mp3`)
}
/** VALIDA O NUMERO, VERIFICA SE O TEM ALGMA MSG INICIAL, SE JÁ TEVE UM NUMERO NA VERIFICACAO */
listarAtendimentoAgente(localStorage.getItem('my_uniqueid'))
validate = JSON.parse(localStorage.getItem('obj_notification'))
const vald = validate.data.filter((e) => {
return data.event?.mensagem.uniqueid == e.uniqueid
})
if(data.event?.contact.number != localStorage.getItem('session_window')){
alertNotification(data.event.mensagem.uniqueid)
}
if (data.event?.mensagem.uniqueid && data.event?.contact.number && vald.length == 0) {
notifications(
{
uniqueid: data.event?.mensagem.uniqueid,
cliente_id: data.event?.contact.number,
context: data.event?.mensagem.media,
profile_name: data.event?.contact.name,
data_reg: data.event?.mensagem.datetime,
status: 1,
action: 'mensagem',
}
)
}
break
case "actions":
let obj
switch(data.event.mensagem.type){
case 'start':
case 'transfer':
case 'att_status':
obj = {}
break
case 'finish':
case 're_start':
obj = {
uniqueid: data.event?.mensagem.uniqueid,
action: data.event.mensagem.type,
}
break
}
notifications(obj)
break
}
}
/**
* MANTEM TODAS AS MENSAGENS ARMAZENADAS NO LOCALSTORAGE, SEMPRE QUE ENVIA E RECEBE SERÁ GUARDADO
* @param {*} ev
*/
const keepMensage = (ev) => {
let msg = JSON.parse(localStorage.getItem('keep_msg'))
if(!msg){
msg = { data: [] }
}
if(ev.event?.contact && ev.event?.mensagem.content){
msg.data.push(ev)
localStorage.removeItem('keep_msg');
localStorage.setItem('keep_msg', JSON.stringify(msg))
}
}
/**
* FUNÇÃO PARA CAPTURAR O ARQUIVO A SER ENVIADO
*/
const sendMedia = (media = null) => {
let rec
let filename
if($("#footer-content-left audio").length){
if(media.state == 'recording'){
media.stop();
}
let el = $("#footer-content-left audio")[0].src
fileContent = $("#footer-content-left audio")[0].outerHTML
sendMessage({ content: el.replace("data:", "").replace(/^.+,/, ""), type: 'audio', mimetype: 'audio/mpeg', fileContent })
return
} else if($("#uploadfile")[0].files[0]) {
let el = $("#uploadfile")[0].files[0]
rec = new Blob([el], { type : el.type })
let filesent = $("#myImg")
filesent[0].id = Date.now()
imgContent = filesent.css({'max-width': '60px'})[0].outerHTML
fileContent = `${imgContent}`
filename = el.name
} else {
rec = $("#uploadimage")[0].files[0]
let filesent = $("#myImg")
filesent[0].id = Date.now()
fileContent = filesent.css({'max-width': '350px'})[0].outerHTML
$("#myImg").empty()
}
const file = new FileReader();
file.onload = function() {
const typefile = rec.type.split("/")[0].indexOf('image') >= 0 ? rec.type.split("/")[0] : 'document'
sendMessage({ content: file.result.replace("data:", "").replace(/^.+,/, ""), type: typefile, mimetype: rec.type, fileContent, filename })
}
file.readAsDataURL(rec);
}