Skip to content

Commit d9fdde9

Browse files
committed
Adding support for last_request_at to User, create_user, and update_user.
Fixes intercom#16
1 parent 42cd4d9 commit d9fdde9

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

intercom/intercom.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,34 +155,53 @@ def get_user(cls, email=None, user_id=None):
155155
return user_dict
156156

157157
@classmethod
158-
def create_user(
159-
cls, user_id=None, email=None, name=None, created_at=None,
160-
custom_data=None, last_seen_ip=None, last_seen_user_agent=None):
161-
""" Create a user from the available parameters.
158+
def create_user(cls, **kwargs):
159+
""" Creates a user.
160+
161+
N.B. Social and geo location data is fetched asynchronously, so a
162+
secondary call to users will be required to fetch it.
163+
164+
**Arguments**
165+
166+
- ``user_id``: required (if no email) — a unique string identifier
167+
for the user
168+
- ``email``: required (if no user_id) — the user's email address
169+
- ``name``: The user's full name
170+
- ``created_at``: A UNIX timestamp representing the date the user was
171+
created
172+
- ``custom_data``: A hash of key/value pairs containing any other data
173+
about the user you want Intercom to store.
174+
- ``last_seen_ip``: An ip address (e.g. "1.2.3.4") representing the
175+
last ip address the user visited your application from. (Used for
176+
updating location_data)
177+
- ``last_seen_user_agent``: The user agent the user last visited your
178+
application with.
179+
- ``companies``: An array of hashes describing the companies this user
180+
belongs to. Currently companies are not returned in the response.
181+
- ``last_request_at or last_impression_at``: A UNIX timestamp
182+
representing the date the user last visited your application.
183+
- ``unsubscribed_from_emails``: A boolean value representing the users
184+
unsubscribed status.
185+
162186
163187
>>> from datetime import datetime
164188
>>> import time
165189
>>> now = time.mktime(datetime.now().timetuple())
166190
>>> user = Intercom.create_user(user_id='987', email='joe@example.com',
167191
... name='Joe Example', created_at=now, last_seen_ip='10.10.10.10',
168-
... custom_data={ 'job_type': 'smuggler'})
192+
... custom_data={ 'job_type': 'smuggler'}, last_request_at=1350000000)
169193
>>> user['name']
170194
u'Joe Example'
171195
>>> user['custom_data']['job_type']
172196
u'smuggler'
197+
>>> user['last_impression_at']
198+
1350000000
173199
174200
"""
175-
return Intercom._create_or_update_user(
176-
'POST', user_id=user_id, email=email, name=name,
177-
created_at=created_at, custom_data=custom_data,
178-
last_seen_ip=last_seen_ip,
179-
last_seen_user_agent=last_seen_user_agent)
201+
return Intercom._create_or_update_user('POST', **kwargs)
180202

181203
@classmethod
182-
def update_user(
183-
cls, user_id=None, email=None, name=None, created_at=None,
184-
custom_data=None, last_seen_ip=None, last_seen_user_agent=None,
185-
unsubscribed_from_emails=None):
204+
def update_user(cls, **kwargs):
186205
""" Update a user with the available parameters.
187206
188207
>>> user = Intercom.get_user(user_id='123')
@@ -193,12 +212,7 @@ def update_user(
193212
u'Han'
194213
195214
"""
196-
return Intercom._create_or_update_user(
197-
'PUT', user_id=user_id, email=email, name=name,
198-
created_at=created_at, custom_data=custom_data,
199-
last_seen_ip=last_seen_ip,
200-
last_seen_user_agent=last_seen_user_agent,
201-
unsubscribed_from_emails=unsubscribed_from_emails)
215+
return Intercom._create_or_update_user('PUT', **kwargs)
202216

203217
@classmethod
204218
def delete_user(cls, user_id=None, email=None):

intercom/user.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ class User(UserId):
4949

5050
attributes = (
5151
'user_id', 'email', 'name', 'created_at', 'custom_data',
52-
'last_seen_ip', 'last_seen_user_agent', 'unsubscribed_from_emails')
52+
'last_seen_ip', 'last_seen_user_agent', 'last_impression_at',
53+
'last_request_at', 'unsubscribed_from_emails')
5354

5455
@classmethod
5556
def find(cls, user_id=None, email=None):
@@ -99,20 +100,17 @@ def find_by_user_id(cls, user_id):
99100
return cls(resp)
100101

101102
@classmethod
102-
def create(
103-
cls, user_id=None, email=None, name=None, created_at=None,
104-
custom_data=None, last_seen_ip=None, last_seen_user_agent=None):
103+
def create(cls, **kwargs):
105104
""" Create or update a user.
106105
107-
>>> user = User.create(email="somebody@example.com")
106+
>>> user = User.create(email="somebody@example.com",last_impression_at=1400000000)
108107
>>> user.name
109108
u'Somebody'
109+
>>> user.last_impression_at.year
110+
2014
110111
111112
"""
112-
resp = Intercom.create_user(
113-
user_id=user_id, email=email, name=name, created_at=created_at,
114-
custom_data=custom_data, last_seen_ip=last_seen_ip,
115-
last_seen_user_agent=last_seen_user_agent)
113+
resp = Intercom.create_user(**kwargs)
116114
return cls(resp)
117115

118116
@classmethod
@@ -199,6 +197,18 @@ def last_seen_user_agent(self, last_seen_user_agent):
199197
""" Sets the last seen User Agent. """
200198
self['last_seen_user_agent'] = last_seen_user_agent
201199

200+
@property
201+
def last_request_at(self):
202+
""" Get last time this User interacted with your application. """
203+
return dict.get(self, 'last_request_at', None)
204+
205+
@last_request_at.setter
206+
@to_timestamp_property
207+
def last_request_at(self, last_request_at):
208+
""" Set time at which this User last made a request your application.
209+
"""
210+
self['last_request_at'] = last_request_at
211+
202212
@property
203213
def relationship_score(self):
204214
""" Returns the relationship score. """
@@ -216,6 +226,13 @@ def last_impression_at(self):
216226
""" Returns the datetime this User last used your application. """
217227
return dict.get(self, 'last_impression_at', None)
218228

229+
@last_impression_at.setter
230+
@to_timestamp_property
231+
def last_impression_at(self, last_impression_at):
232+
""" Set time at which this User last made a request your application.
233+
"""
234+
self['last_impression_at'] = last_impression_at
235+
219236
@property
220237
@from_timestamp_property
221238
def created_at(self):

0 commit comments

Comments
 (0)