-
How we can upload the chat file exported from WhatsApp using an API?
-
Whether channel id will be same for each conversation, irrespective of different channel like FB, WhatsApp?
-
In Post API, we found one array for user, do we need to always check it with ID or we can use another parameter?
-
Does actor_type and actor_id is mandatory for creating conversation using an Post API?
-
What are different types of message_type we can send?
Hi,
- Can you try using the option of JS or Python anf try creating a script to loop the conversations in a loop? You need to provide a path on your system.
- Every channel will have a unique channel ID, you need to get your existing whatsapp conversation, if you are trying to import with same channel then you can use this.
- Users are from the chat agent, they will always be mapped as ID. I assume you are trying to import older chats, irrespective of the data, you can proceed to import for a single User, (If you have list of users worked on whatsapp then you need to link them saperately with their ID`s)
- Whatever you GET from the existing one, use that for posting.
- I suggest you to check the dev community / developer API documentation. Freshworks maintains a detailed and crisp API documentation.
FYR - you can use a script similar to this:
This is a Python Code you can try:
import os
import re
import requests
Function to parse WhatsApp .txt files
def parse_whatsapp_file(file_path):
conversations =
with open(file_path, ‘r’, encoding=‘utf-8’) as file:
conversation =
for line in file:
match = re.match(r’[(\d{2}/\d{2}/\d{4}), (\d{2}:\d{2}) - (.+?)]: (.+)', line)
if match:
date, time, person, message = match.groups()
conversation.append({
‘date’: date,
‘time’: time,
‘person’: person,
‘message’: message
})
if conversation:
conversations.append(conversation)
return conversations
Function to transform parsed data into Freshchat’s API format
def transform_to_freshchat_format(conversations):
freshchat_conversations =
for conversation in conversations:
messages =
for msg in conversation:
messages.append({
‘message’: msg[‘message’],
‘timestamp’: f"{msg[‘date’]} {msg[‘time’]}",
‘sender’: msg[‘person’]
})
freshchat_conversations.append({‘messages’: messages})
return freshchat_conversations
Function to upload conversations to Freshchat using API
def upload_to_freshchat(conversations, freshchat_api_token):
url = “https://api.freshchat.com/v2/conversations”
headers = {
‘Authorization’: f’Bearer {freshchat_api_token}',
‘Content-Type’: ‘application/json’
}
for conversation in conversations:
payload = {
“messages”: conversation[‘messages’]
}
response = requests.post(url, json=payload, headers=headers)
if response.status_code == 201:
print(“Conversation uploaded successfully”)
else:
print(f"Failed to upload conversation: {response.text}")
Directory containing the WhatsApp .txt files
directory = ‘/path/to/whatsapp/txt/files’
freshchat_api_token = ‘your_freshchat_api_token’
Process each .txt file in the directory
for filename in os.listdir(directory):
if filename.endswith(‘.txt’):
file_path = os.path.join(directory, filename)
conversations = parse_whatsapp_file(file_path)
freshchat_conversations = transform_to_freshchat_format(conversations)
upload_to_freshchat(freshchat_conversations, freshchat_api_token)