Created
October 16, 2024 19:55
-
-
Save williamoliveira/c8810222340e0df90f04ef385f66914e to your computer and use it in GitHub Desktop.
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
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