intercom / python-intercom

Python wrapper for the Intercom API.

Home Page:https://keyes.ie/things/python-intercom/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot save custom attribute of a User

kccheung opened this issue · comments

I cannot save a custom attribute of a User object using logic similar to below:

# Update custom_attributes for a user
user.custom_attributes["average_monthly_spend"] = 1234.56
intercom.users.save(user)

with error saying like intercom.errors.BadRequestError: bad 'users' parameter or bad 'limited' parameter.

The error occurs at

    def _execute_request(self, request, params):
        result = request.execute(self.base_url, self._auth, params) ...
        self.rate_limit_details = request.rate_limit_details
        return result

with params like the below:

{'custom_attributes': {'how_did_you_learn': 'referral'},
 'limited': False,
 'pages': <intercom.utils.Pages object at 0x104a62b70>,
 'total_count': 34,
 'users': [{'anonymous': False,
            'app_id': 'XXXXXXX',
            'avatar': {'image_url': None, 'type': 'avatar'},
            'companies': {'companies': [], 'type': 'company.list'},
            'created_at': 1520993591,
            'custom_attributes': {'how_did_you_learn': 'search-engine'},
            'email': None,
            'has_hard_bounced': False,
            'id': '5aa8853727bd5333a77f4a3f',
            'last_request_at': None,
            'last_seen_ip': None,
            'location_data': {'city_name': None,
                              'continent_code': None,
                              'country_code': None,
                              'country_name': None,
                              'latitude': None,
                              'longitude': None,
                              'postal_code': None,
                              'region_name': None,
                              'timezone': None,
                              'type': 'location_data'},
            'marked_email_as_spam': False,

P.S. I am using the latest master version and my python version is 3.5.3

Update: I found the culprit causing the problem. I used wrong way to search for a user using find as it only accepts email or user_id. I used only name which is wrong.

Hello @kccheung , I know this was a while ago, but I can't get it to work even when I look up the user by their email... Any ideas of what I'm doing wrong?

user = intercom.users.find(email="bob@example.com")
user.custom_attributes['Lastname'] = 'Test'
intercom.users.save(user)

Gives me the BadRequestError: bad 'pages' parameter

Hello @kccheung , I know this was a while ago, but I can't get it to work even when I look up the user by their email... Any ideas of what I'm doing wrong?

user = intercom.users.find(email="bob@example.com")
user.custom_attributes['Lastname'] = 'Test'
intercom.users.save(user)

Gives me the BadRequestError: bad 'pages' parameter

Do you know what was the issue?

@aidanak no, this was never resolved.

When you search for a user by email and user is not found
i.e.
user = intercom.users.find(email="bob@example.com")
user is:
{'pages': <intercom.utils.define_lightweight_class..DynamicClass object at 0x1092e3150>, 'total_count': 0, 'limited': False, 'users': [], 'custom_attributes': {}}

When you search for a user by id and user is not found
i.e.
user = intercom.users.find(email="12345df")
user is None

Shouldn't these return None in both cases?

This is what I had to do to get this to work as expected:

@with_client
def find_or_create_user(client, user):
    intercom_user = None
    try:
        intercom_user = client.users.find(user_id=str(user.id))
    except ResourceNotFound:
        try:
            intercom_user = client.users.find(email=str(user.email))
            if intercom_user is None or (hasattr(intercom_user, 'total_count') and intercom_user.total_count == 0):
                intercom_user = client.users.create(
                    email=user.email,
                    name=user.get_full_name(),
                    user_id=str(user.id),
                    signed_up_at=user.date_joined.timestamp(),
                )
        except ResourceNotFound:
            try:
                intercom_user = client.users.create(
                    email=user.email,
                    name=user.get_full_name(),
                    user_id=str(user.id),
                    signed_up_at=user.date_joined.timestamp(),
                )
            except Exception as error:
                capture_exception(error)

    return intercom_user