Skip to content

Instantly share code, notes, and snippets.

@williamoliveira
Created October 16, 2024 19:55
Show Gist options
  • Save williamoliveira/c8810222340e0df90f04ef385f66914e to your computer and use it in GitHub Desktop.
Save williamoliveira/c8810222340e0df90f04ef385f66914e to your computer and use it in GitHub Desktop.
def get_chat_history(contact_id):
if not contact_id:
return ''
token = os.environ.get('DIGISC_TOKEN')
base_url = "https://ikatec.digisac.chat/api/v1"
headers = {'Authorization': f'Bearer {token}'}
response = requests.get(f'{base_url}/contacts/{contact_id}', headers=headers)
current_ticket_id = response.json()['currentTicketId']
if not current_ticket_id:
return ''
response = requests.get(
f'{base_url}/messages?paginate=false&limit=25&order[0][0]=timestamp&order[0][1]=DESC&where[ticketId]={current_ticket_id}',
headers=headers,
)
history = ""
for message in reversed(response.json()):
if message['type'] == 'ticket':
continue
sender = "Atendente" if message['isFromMe'] else "Contato"
text = message.get('text', f'[Mensagem do tipo "{message['type']}"]')
history += f"[{message['timestamp']}] {sender}: {text}\n"
return history
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment