Skip to main content
Answered

How to parse multiple words in one API request?


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 = []
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([email])

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?

Best answer by Joe Caffrey

Your operator ~ is contains so it may combining them.  Maybe try doing two searches each on separate word then find the conversations in common with both?

You could also try = to see how that functions.  I’m not sure if there is a wild card in the search capability to include the space between the search.

View original
Did this topic help you find an answer to your question?

2 replies

Joe Caffrey
Forum|alt.badge.img
  • Connector
  • 9 replies
  • Answer
  • January 9, 2025

Your operator ~ is contains so it may combining them.  Maybe try doing two searches each on separate word then find the conversations in common with both?

You could also try = to see how that functions.  I’m not sure if there is a wild card in the search capability to include the space between the search.


  • Author
  • New Participant
  • 1 reply
  • January 9, 2025
Joe Caffrey wrote:

Your operator ~ is contains so it may combining them.  Maybe try doing two searches each on separate word then find the conversations in common with both?

You could also try = to see how that functions.  I’m not sure if there is a wild card in the search capability to include the space between the search.

Thank you for your reply, when I search with separate words in one operator “AND”:
data = {
    "query": {
        "operator": "AND",
        "value": [
            {
                "field": "source.body",
                "operator": "~",
                "value": "two"
            },
            {
                "field": "source.body",
                "operator": "~",
                "value": "words"
            }
        ]
    },
    "pagination": {
        "per_page": 150
    }

I get only conversations where these words contains in conversation, but these words are not located next to each other.
According to the “=”, it doesn’t return any conversations. I need to use space between search because it finds conversations where these words are located next to each other.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings