I have created a Python code with using API requests to fetching conversations by different words.
However, when I try finding two words that contain in the conversations, it will not response me with any emails.
Code here:
import requests
import csv
# Replace with your Intercom access token
access_token = 'PUT-YOUR-API-HERE'
# URL for searching conversations
url = "https://api.intercom.io/conversations/search"
# Headers for authentication
headers = {
"Intercom-Version": "2.11",
"Authorization": f"Bearer {access_token}",
"Accept": "application/json",
"Content-Type": "application/json"
}
data = {
"query": {
"operator": "AND",
"value":
{
"field": "source.body",
"operator": "~",
"value": "two words"
}
]
},
"pagination": {
"per_page": 150
}
}
# Function to retrieve all pages
def fetch_all_conversations():
conversations = ]
next_page_data = data # Initial data for the first request
while next_page_data:
response = requests.post(url, headers=headers, json=next_page_data)
if response.status_code == 200:
response_data = response.json()
conversations.extend(response_data.get("conversations", _])) # Add conversations
# Update `next_page_data` for the next page
pages = response_data.get("pages", {})
next_starting_after = pages.get("next", {}).get("starting_after")
if next_starting_after:
next_page_data = {
"query": data"query"],
"pagination": {
"per_page": 150,
"starting_after": next_starting_after
}
}
else:
# If there are no more pages, exit the loop
next_page_data = None
else:
print("Error:", response.status_code, response.json())
break
return conversations
# Retrieve all conversations
all_conversations = fetch_all_conversations()
# Collect email addresses
user_emails = r]
for conversation in all_conversations:
author = conversation.get("source", {}).get("author", {})
if author.get("type") == "user":
email = author.get("email")
if email and email not in user_emails: # Avoid duplicates
user_emails.append(email)
# Write emails to a CSV file
with open("user_emails.csv", mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerow("Email"]) # Column name
for email in user_emails:
writer.writerow(eemail])
print(f"User Emails: {len(user_emails)}", user_emails)
print("Email addresses have been saved to the file user_emails.csv")
My response is 0 emails, but I checked these words manually and have a lot of conversation that can be parsed via this code.
How am I supposed to collect information with these two words that should be next to each other and it works?