|
3 | 3 | #
|
4 | 4 | # License: http://jkeyes.mit-license.org/
|
5 | 5 | #
|
| 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 | +""" |
6 | 14 |
|
7 | 15 | from . import Intercom
|
8 | 16 | from . import from_timestamp_property
|
9 | 17 |
|
10 | 18 | class MessageThread(dict):
|
11 |
| - _get = dict.get |
| 19 | + """ An Intercom conversation between an admin and a User. """ |
12 | 20 |
|
13 | 21 | @classmethod
|
14 | 22 | 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") |
15 | 39 | resp = Intercom.get_message_threads(user_id=user_id, email=email,
|
16 | 40 | thread_id=thread_id)
|
17 | 41 | return MessageThread(resp)
|
18 | 42 |
|
19 | 43 | @classmethod
|
20 | 44 | 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 | + """ |
21 | 52 | resp = Intercom.get_message_threads(user_id=user_id, email=email)
|
22 | 53 | return [MessageThread(mt) for mt in resp]
|
23 | 54 |
|
24 | 55 | @classmethod
|
25 | 56 | 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 | + """ |
26 | 68 | resp = Intercom.create_message_thread(user_id=user_id, email=email,
|
27 | 69 | body=body)
|
28 | 70 | return MessageThread(resp)
|
29 | 71 |
|
30 | 72 | @classmethod
|
31 | 73 | def reply(cls, user_id=None, email=None, thread_id=None,
|
32 | 74 | 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 | + """ |
33 | 89 | 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) |
35 | 91 | return MessageThread(resp)
|
36 | 92 |
|
37 | 93 | @property
|
38 | 94 | @from_timestamp_property
|
39 | 95 | def updated_at(self):
|
| 96 | + """ Returns a datetime of when the last update occurred. """ |
40 | 97 | return dict.get(self, 'updated_at', None)
|
41 | 98 |
|
42 | 99 | @property
|
43 | 100 | @from_timestamp_property
|
44 | 101 | def created_at(self):
|
| 102 | + """ Sets a timestamp of when the last update occurred. """ |
45 | 103 | return dict.get(self, 'created_at', None)
|
46 | 104 |
|
47 | 105 | def set_body(self, value):
|
| 106 | + """ Set the body of a reply. """ |
48 | 107 | self['body'] = value
|
49 | 108 | body = property(None, set_body, None)
|
50 | 109 |
|
51 | 110 | @property
|
52 | 111 | def thread_id(self):
|
| 112 | + """ Returns the thread_id of this MessageThread. """ |
53 | 113 | return dict.get(self, 'thread_id', None)
|
54 | 114 |
|
55 | 115 | @thread_id.setter
|
56 | 116 | def thread_id(self, value):
|
| 117 | + """ Sets the thread_id of this MessageThread. """ |
57 | 118 | self['thread_id'] = value
|
58 | 119 |
|
59 | 120 | @property
|
60 | 121 | def read(self):
|
| 122 | + """ Returns whether this thread has been read or not. """ |
61 | 123 | return dict.get(self, 'read', None)
|
62 | 124 |
|
63 | 125 | @read.setter
|
64 | 126 | def read(self, value):
|
| 127 | + """ Sets whether this thread has been read or not. """ |
65 | 128 | self['read'] = value
|
66 | 129 |
|
67 | 130 | @property
|
68 | 131 | def messages(self):
|
| 132 | + """ Returns a list of Messages in this MessageThread. """ |
69 | 133 | messages = dict.get(self, 'messages', None)
|
70 | 134 | if messages:
|
71 | 135 | return [Message(m) for m in messages]
|
72 | 136 |
|
73 | 137 | 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 | + """ |
74 | 150 |
|
75 | 151 | @property
|
76 | 152 | def author(self):
|
| 153 | + """ Returns who authored the message. """ |
77 | 154 | _from = self.get('from', None)
|
78 | 155 | if _from:
|
79 | 156 | return MessageAuthor(_from)
|
80 | 157 |
|
81 | 158 | @property
|
82 | 159 | def html(self):
|
| 160 | + """ Returns the HTML body of the Message. """ |
83 | 161 | return dict.get(self, 'html', None)
|
84 | 162 |
|
85 | 163 | @property
|
86 | 164 | @from_timestamp_property
|
87 | 165 | def created_at(self):
|
| 166 | + """ Returns a datetime for when this Message was created. """ |
88 | 167 | return dict.get(self, 'created_at', None)
|
89 | 168 |
|
90 | 169 | 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 | + """ |
91 | 186 |
|
92 | 187 | @property
|
93 | 188 | def admin(self):
|
| 189 | + """ Returns whether the author is an admin. """ |
94 | 190 | return dict.get(self, 'is_admin', None)
|
95 | 191 |
|
96 | 192 | @property
|
97 | 193 | def email(self):
|
| 194 | + """ Returns the email address of the author. """ |
98 | 195 | return dict.get(self, 'email', None)
|
99 | 196 |
|
100 | 197 | @property
|
101 | 198 | def user_id(self):
|
| 199 | + """ Returns the user_id of the author. """ |
102 | 200 | return dict.get(self, 'user_id', None)
|
103 | 201 |
|
104 | 202 | @property
|
105 | 203 | def avatar_path_50(self):
|
| 204 | + """ Returns a URL to a 50x50 avatar of the author. """ |
106 | 205 | return dict.get(self, 'avatar_path_50', None)
|
107 | 206 |
|
108 | 207 | @property
|
109 | 208 | def name(self):
|
| 209 | + """ Returns the author's name. """ |
110 | 210 | return dict.get(self, 'name', None)
|
111 | 211 |
|
0 commit comments