Skip to content

Commit 8bcabb1

Browse files
committed
docstrings and doctests for message_thread
Fixed a bug in MessageThread.find (I wasn't passing thread_id parameter to the API).
1 parent 85e05d9 commit 8bcabb1

File tree

3 files changed

+107
-3
lines changed

3 files changed

+107
-3
lines changed

intercom/intercom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def create_impression(cls, user_id=None, email=None, user_ip=None,
122122

123123
@classmethod
124124
def get_message_threads(cls, user_id=None, email=None, thread_id=None):
125-
params = { 'email': email, 'user_id': user_id }
125+
params = { 'email': email, 'user_id': user_id, 'thread_id': thread_id }
126126
msg_dict = Intercom._call('GET', Intercom.api_endpoint + 'users/message_threads',
127127
params=params)
128128
return msg_dict

intercom/message_thread.py

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,109 +3,209 @@
33
#
44
# License: http://jkeyes.mit-license.org/
55
#
6+
""" message_thread module.
7+
8+
>>> from intercom import Intercom
9+
>>> Intercom.app_id = 'dummy-app-id'
10+
>>> Intercom.api_key = 'dummy-api-key'
11+
>>> from intercom import MessageThread
12+
13+
"""
614

715
from . import Intercom
816
from . import from_timestamp_property
917

1018
class MessageThread(dict):
11-
_get = dict.get
19+
""" An Intercom conversation between an admin and a User. """
1220

1321
@classmethod
1422
def find(cls, user_id=None, email=None, thread_id=None):
23+
""" Finds a particular conversation for a particular user.
24+
25+
>>> message_thread = MessageThread.find(email="somebody@example.com")
26+
Traceback (most recent call last):
27+
...
28+
ValueError: No thread_id specified
29+
>>> message_thread = MessageThread.find(email="somebody@example.com", thread_id=5591)
30+
>>> len(message_thread.messages)
31+
3
32+
>>> message = message_thread.messages[0]
33+
>>> type(message)
34+
<class 'intercom.message_thread.Message'>
35+
36+
"""
37+
if thread_id is None:
38+
raise ValueError("No thread_id specified")
1539
resp = Intercom.get_message_threads(user_id=user_id, email=email,
1640
thread_id=thread_id)
1741
return MessageThread(resp)
1842

1943
@classmethod
2044
def find_all(cls, user_id=None, email=None):
45+
""" Finds all Messages for a particular user.
46+
47+
>>> message_threads = MessageThread.find_all(email="somebody@example.com")
48+
>>> len(message_threads)
49+
1
50+
51+
"""
2152
resp = Intercom.get_message_threads(user_id=user_id, email=email)
2253
return [MessageThread(mt) for mt in resp]
2354

2455
@classmethod
2556
def create(cls, user_id=None, email=None, body=None):
57+
""" Creates a new converstion.
58+
59+
>>> email = "somebody@example.com"
60+
>>> body = "Hi everybody, I'm Doctor Nick"
61+
>>> message_thread = MessageThread.create(email=email, body=body)
62+
>>> message_thread.thread_id
63+
5591
64+
>>> len(message_thread.messages)
65+
1
66+
67+
"""
2668
resp = Intercom.create_message_thread(user_id=user_id, email=email,
2769
body=body)
2870
return MessageThread(resp)
2971

3072
@classmethod
3173
def reply(cls, user_id=None, email=None, thread_id=None,
3274
body=None, read=None):
75+
""" Reply to an existing conversation.
76+
77+
>>> email = "somebody@example.com"
78+
>>> thread_id = 5591
79+
>>> body = "Are you talking to me?"
80+
>>> message_thread = MessageThread.reply(email=email, thread_id=thread_id, body=body)
81+
>>> len(message_thread.messages)
82+
2
83+
>>> message_thread.messages[0].html
84+
u'<p>Hey Intercom, What is up?</p>'
85+
>>> message_thread.messages[1].html
86+
u'<p>Are you talking to me?</p>'
87+
88+
"""
3389
resp = Intercom.reply_message_thread(user_id=user_id, email=email,
34-
thread_id=thread_id, read=None, body=body)
90+
thread_id=thread_id, read=read, body=body)
3591
return MessageThread(resp)
3692

3793
@property
3894
@from_timestamp_property
3995
def updated_at(self):
96+
""" Returns a datetime of when the last update occurred. """
4097
return dict.get(self, 'updated_at', None)
4198

4299
@property
43100
@from_timestamp_property
44101
def created_at(self):
102+
""" Sets a timestamp of when the last update occurred. """
45103
return dict.get(self, 'created_at', None)
46104

47105
def set_body(self, value):
106+
""" Set the body of a reply. """
48107
self['body'] = value
49108
body = property(None, set_body, None)
50109

51110
@property
52111
def thread_id(self):
112+
""" Returns the thread_id of this MessageThread. """
53113
return dict.get(self, 'thread_id', None)
54114

55115
@thread_id.setter
56116
def thread_id(self, value):
117+
""" Sets the thread_id of this MessageThread. """
57118
self['thread_id'] = value
58119

59120
@property
60121
def read(self):
122+
""" Returns whether this thread has been read or not. """
61123
return dict.get(self, 'read', None)
62124

63125
@read.setter
64126
def read(self, value):
127+
""" Sets whether this thread has been read or not. """
65128
self['read'] = value
66129

67130
@property
68131
def messages(self):
132+
""" Returns a list of Messages in this MessageThread. """
69133
messages = dict.get(self, 'messages', None)
70134
if messages:
71135
return [Message(m) for m in messages]
72136

73137
class Message(dict):
138+
""" Object representing a Message in a MessageThread.
139+
140+
>>> message_thread = MessageThread.find(email="somebody@example.com", thread_id=5591)
141+
>>> message = message_thread.messages[0]
142+
>>> type(message.author)
143+
<class 'intercom.message_thread.MessageAuthor'>
144+
>>> message.html
145+
u'<p>Hey Intercom, What is up?</p>'
146+
>>> type(message.created_at)
147+
<type 'datetime.datetime'>
148+
149+
"""
74150

75151
@property
76152
def author(self):
153+
""" Returns who authored the message. """
77154
_from = self.get('from', None)
78155
if _from:
79156
return MessageAuthor(_from)
80157

81158
@property
82159
def html(self):
160+
""" Returns the HTML body of the Message. """
83161
return dict.get(self, 'html', None)
84162

85163
@property
86164
@from_timestamp_property
87165
def created_at(self):
166+
""" Returns a datetime for when this Message was created. """
88167
return dict.get(self, 'created_at', None)
89168

90169
class MessageAuthor(dict):
170+
""" Object represting the author of a Message.
171+
172+
>>> message_thread = MessageThread.find(email="somebody@example.com", thread_id=5591)
173+
>>> author = message_thread.messages[0].author
174+
>>> author.admin
175+
False
176+
>>> author.email
177+
u'bob@example.com'
178+
>>> author.user_id
179+
u'456'
180+
>>> author.avatar_path_50
181+
182+
>>> author.name
183+
u'Bob'
184+
185+
"""
91186

92187
@property
93188
def admin(self):
189+
""" Returns whether the author is an admin. """
94190
return dict.get(self, 'is_admin', None)
95191

96192
@property
97193
def email(self):
194+
""" Returns the email address of the author. """
98195
return dict.get(self, 'email', None)
99196

100197
@property
101198
def user_id(self):
199+
""" Returns the user_id of the author. """
102200
return dict.get(self, 'user_id', None)
103201

104202
@property
105203
def avatar_path_50(self):
204+
""" Returns a URL to a 50x50 avatar of the author. """
106205
return dict.get(self, 'avatar_path_50', None)
107206

108207
@property
109208
def name(self):
209+
""" Returns the author's name. """
110210
return dict.get(self, 'name', None)
111211

tests/integration/test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ def test_message_threads(self):
4141
threads = MessageThread.find_all(email='somebody@example.com')
4242
self.assertEqual(1, len(threads))
4343

44+
def test_message_thread(self):
45+
thread = MessageThread.find(email='somebody@example.com', thread_id=5591)
46+
self.assertEqual(5591, thread.thread_id)
47+
4448
def test_impression(self):
4549
impression = Impression.create(email='somebody@example.com')
4650
self.assertEqual(1, impression.unread_messages)

0 commit comments

Comments
 (0)