1# coding=utf-8
2r"""
3This code was generated by
4\ / _    _  _|   _  _
5 | (_)\/(_)(_|\/| |(/_  v1.0.0
6      /       /
7"""
8
9from twilio.base import deserialize
10from twilio.base import serialize
11from twilio.base import values
12from twilio.base.instance_context import InstanceContext
13from twilio.base.instance_resource import InstanceResource
14from twilio.base.list_resource import ListResource
15from twilio.base.page import Page
16
17
18class MessageList(ListResource):
19
20    def __init__(self, version, service_sid, channel_sid):
21        """
22        Initialize the MessageList
23
24        :param Version version: Version that contains the resource
25        :param service_sid: The SID of the Service that the resource is associated with
26        :param channel_sid: The SID of the Channel the Message resource belongs to
27
28        :returns: twilio.rest.chat.v2.service.channel.message.MessageList
29        :rtype: twilio.rest.chat.v2.service.channel.message.MessageList
30        """
31        super(MessageList, self).__init__(version)
32
33        # Path Solution
34        self._solution = {'service_sid': service_sid, 'channel_sid': channel_sid, }
35        self._uri = '/Services/{service_sid}/Channels/{channel_sid}/Messages'.format(**self._solution)
36
37    def create(self, from_=values.unset, attributes=values.unset,
38               date_created=values.unset, date_updated=values.unset,
39               last_updated_by=values.unset, body=values.unset,
40               media_sid=values.unset, x_twilio_webhook_enabled=values.unset):
41        """
42        Create the MessageInstance
43
44        :param unicode from_: The Identity of the new message's author
45        :param unicode attributes: A valid JSON string that contains application-specific data
46        :param datetime date_created: The ISO 8601 date and time in GMT when the resource was created
47        :param datetime date_updated: The ISO 8601 date and time in GMT when the resource was updated
48        :param unicode last_updated_by: The Identity of the User who last updated the Message
49        :param unicode body: The message to send to the channel
50        :param unicode media_sid:  The Media Sid to be attached to the new Message
51        :param MessageInstance.WebhookEnabledType x_twilio_webhook_enabled: The X-Twilio-Webhook-Enabled HTTP request header
52
53        :returns: The created MessageInstance
54        :rtype: twilio.rest.chat.v2.service.channel.message.MessageInstance
55        """
56        data = values.of({
57            'From': from_,
58            'Attributes': attributes,
59            'DateCreated': serialize.iso8601_datetime(date_created),
60            'DateUpdated': serialize.iso8601_datetime(date_updated),
61            'LastUpdatedBy': last_updated_by,
62            'Body': body,
63            'MediaSid': media_sid,
64        })
65        headers = values.of({'X-Twilio-Webhook-Enabled': x_twilio_webhook_enabled, })
66
67        payload = self._version.create(method='POST', uri=self._uri, data=data, headers=headers, )
68
69        return MessageInstance(
70            self._version,
71            payload,
72            service_sid=self._solution['service_sid'],
73            channel_sid=self._solution['channel_sid'],
74        )
75
76    def stream(self, order=values.unset, limit=None, page_size=None):
77        """
78        Streams MessageInstance records from the API as a generator stream.
79        This operation lazily loads records as efficiently as possible until the limit
80        is reached.
81        The results are returned as a generator, so this operation is memory efficient.
82
83        :param MessageInstance.OrderType order: The sort order of the returned messages
84        :param int limit: Upper limit for the number of records to return. stream()
85                          guarantees to never return more than limit.  Default is no limit
86        :param int page_size: Number of records to fetch per request, when not set will use
87                              the default value of 50 records.  If no page_size is defined
88                              but a limit is defined, stream() will attempt to read the
89                              limit with the most efficient page size, i.e. min(limit, 1000)
90
91        :returns: Generator that will yield up to limit results
92        :rtype: list[twilio.rest.chat.v2.service.channel.message.MessageInstance]
93        """
94        limits = self._version.read_limits(limit, page_size)
95
96        page = self.page(order=order, page_size=limits['page_size'], )
97
98        return self._version.stream(page, limits['limit'])
99
100    def list(self, order=values.unset, limit=None, page_size=None):
101        """
102        Lists MessageInstance records from the API as a list.
103        Unlike stream(), this operation is eager and will load `limit` records into
104        memory before returning.
105
106        :param MessageInstance.OrderType order: The sort order of the returned messages
107        :param int limit: Upper limit for the number of records to return. list() guarantees
108                          never to return more than limit.  Default is no limit
109        :param int page_size: Number of records to fetch per request, when not set will use
110                              the default value of 50 records.  If no page_size is defined
111                              but a limit is defined, list() will attempt to read the limit
112                              with the most efficient page size, i.e. min(limit, 1000)
113
114        :returns: Generator that will yield up to limit results
115        :rtype: list[twilio.rest.chat.v2.service.channel.message.MessageInstance]
116        """
117        return list(self.stream(order=order, limit=limit, page_size=page_size, ))
118
119    def page(self, order=values.unset, page_token=values.unset,
120             page_number=values.unset, page_size=values.unset):
121        """
122        Retrieve a single page of MessageInstance records from the API.
123        Request is executed immediately
124
125        :param MessageInstance.OrderType order: The sort order of the returned messages
126        :param str page_token: PageToken provided by the API
127        :param int page_number: Page Number, this value is simply for client state
128        :param int page_size: Number of records to return, defaults to 50
129
130        :returns: Page of MessageInstance
131        :rtype: twilio.rest.chat.v2.service.channel.message.MessagePage
132        """
133        data = values.of({
134            'Order': order,
135            'PageToken': page_token,
136            'Page': page_number,
137            'PageSize': page_size,
138        })
139
140        response = self._version.page(method='GET', uri=self._uri, params=data, )
141
142        return MessagePage(self._version, response, self._solution)
143
144    def get_page(self, target_url):
145        """
146        Retrieve a specific page of MessageInstance records from the API.
147        Request is executed immediately
148
149        :param str target_url: API-generated URL for the requested results page
150
151        :returns: Page of MessageInstance
152        :rtype: twilio.rest.chat.v2.service.channel.message.MessagePage
153        """
154        response = self._version.domain.twilio.request(
155            'GET',
156            target_url,
157        )
158
159        return MessagePage(self._version, response, self._solution)
160
161    def get(self, sid):
162        """
163        Constructs a MessageContext
164
165        :param sid: The SID of the Message resource to fetch
166
167        :returns: twilio.rest.chat.v2.service.channel.message.MessageContext
168        :rtype: twilio.rest.chat.v2.service.channel.message.MessageContext
169        """
170        return MessageContext(
171            self._version,
172            service_sid=self._solution['service_sid'],
173            channel_sid=self._solution['channel_sid'],
174            sid=sid,
175        )
176
177    def __call__(self, sid):
178        """
179        Constructs a MessageContext
180
181        :param sid: The SID of the Message resource to fetch
182
183        :returns: twilio.rest.chat.v2.service.channel.message.MessageContext
184        :rtype: twilio.rest.chat.v2.service.channel.message.MessageContext
185        """
186        return MessageContext(
187            self._version,
188            service_sid=self._solution['service_sid'],
189            channel_sid=self._solution['channel_sid'],
190            sid=sid,
191        )
192
193    def __repr__(self):
194        """
195        Provide a friendly representation
196
197        :returns: Machine friendly representation
198        :rtype: str
199        """
200        return '<Twilio.Chat.V2.MessageList>'
201
202
203class MessagePage(Page):
204
205    def __init__(self, version, response, solution):
206        """
207        Initialize the MessagePage
208
209        :param Version version: Version that contains the resource
210        :param Response response: Response from the API
211        :param service_sid: The SID of the Service that the resource is associated with
212        :param channel_sid: The SID of the Channel the Message resource belongs to
213
214        :returns: twilio.rest.chat.v2.service.channel.message.MessagePage
215        :rtype: twilio.rest.chat.v2.service.channel.message.MessagePage
216        """
217        super(MessagePage, self).__init__(version, response)
218
219        # Path Solution
220        self._solution = solution
221
222    def get_instance(self, payload):
223        """
224        Build an instance of MessageInstance
225
226        :param dict payload: Payload response from the API
227
228        :returns: twilio.rest.chat.v2.service.channel.message.MessageInstance
229        :rtype: twilio.rest.chat.v2.service.channel.message.MessageInstance
230        """
231        return MessageInstance(
232            self._version,
233            payload,
234            service_sid=self._solution['service_sid'],
235            channel_sid=self._solution['channel_sid'],
236        )
237
238    def __repr__(self):
239        """
240        Provide a friendly representation
241
242        :returns: Machine friendly representation
243        :rtype: str
244        """
245        return '<Twilio.Chat.V2.MessagePage>'
246
247
248class MessageContext(InstanceContext):
249
250    def __init__(self, version, service_sid, channel_sid, sid):
251        """
252        Initialize the MessageContext
253
254        :param Version version: Version that contains the resource
255        :param service_sid: The SID of the Service to fetch the resource from
256        :param channel_sid: The SID of the Channel the message to fetch belongs to
257        :param sid: The SID of the Message resource to fetch
258
259        :returns: twilio.rest.chat.v2.service.channel.message.MessageContext
260        :rtype: twilio.rest.chat.v2.service.channel.message.MessageContext
261        """
262        super(MessageContext, self).__init__(version)
263
264        # Path Solution
265        self._solution = {'service_sid': service_sid, 'channel_sid': channel_sid, 'sid': sid, }
266        self._uri = '/Services/{service_sid}/Channels/{channel_sid}/Messages/{sid}'.format(**self._solution)
267
268    def fetch(self):
269        """
270        Fetch the MessageInstance
271
272        :returns: The fetched MessageInstance
273        :rtype: twilio.rest.chat.v2.service.channel.message.MessageInstance
274        """
275        payload = self._version.fetch(method='GET', uri=self._uri, )
276
277        return MessageInstance(
278            self._version,
279            payload,
280            service_sid=self._solution['service_sid'],
281            channel_sid=self._solution['channel_sid'],
282            sid=self._solution['sid'],
283        )
284
285    def delete(self, x_twilio_webhook_enabled=values.unset):
286        """
287        Deletes the MessageInstance
288
289        :param MessageInstance.WebhookEnabledType x_twilio_webhook_enabled: The X-Twilio-Webhook-Enabled HTTP request header
290
291        :returns: True if delete succeeds, False otherwise
292        :rtype: bool
293        """
294        headers = values.of({'X-Twilio-Webhook-Enabled': x_twilio_webhook_enabled, })
295
296        return self._version.delete(method='DELETE', uri=self._uri, headers=headers, )
297
298    def update(self, body=values.unset, attributes=values.unset,
299               date_created=values.unset, date_updated=values.unset,
300               last_updated_by=values.unset, from_=values.unset,
301               x_twilio_webhook_enabled=values.unset):
302        """
303        Update the MessageInstance
304
305        :param unicode body: The message to send to the channel
306        :param unicode attributes: A valid JSON string that contains application-specific data
307        :param datetime date_created: The ISO 8601 date and time in GMT when the resource was created
308        :param datetime date_updated: The ISO 8601 date and time in GMT when the resource was updated
309        :param unicode last_updated_by: The Identity of the User who last updated the Message, if applicable
310        :param unicode from_: The Identity of the message's author
311        :param MessageInstance.WebhookEnabledType x_twilio_webhook_enabled: The X-Twilio-Webhook-Enabled HTTP request header
312
313        :returns: The updated MessageInstance
314        :rtype: twilio.rest.chat.v2.service.channel.message.MessageInstance
315        """
316        data = values.of({
317            'Body': body,
318            'Attributes': attributes,
319            'DateCreated': serialize.iso8601_datetime(date_created),
320            'DateUpdated': serialize.iso8601_datetime(date_updated),
321            'LastUpdatedBy': last_updated_by,
322            'From': from_,
323        })
324        headers = values.of({'X-Twilio-Webhook-Enabled': x_twilio_webhook_enabled, })
325
326        payload = self._version.update(method='POST', uri=self._uri, data=data, headers=headers, )
327
328        return MessageInstance(
329            self._version,
330            payload,
331            service_sid=self._solution['service_sid'],
332            channel_sid=self._solution['channel_sid'],
333            sid=self._solution['sid'],
334        )
335
336    def __repr__(self):
337        """
338        Provide a friendly representation
339
340        :returns: Machine friendly representation
341        :rtype: str
342        """
343        context = ' '.join('{}={}'.format(k, v) for k, v in self._solution.items())
344        return '<Twilio.Chat.V2.MessageContext {}>'.format(context)
345
346
347class MessageInstance(InstanceResource):
348
349    class OrderType(object):
350        ASC = "asc"
351        DESC = "desc"
352
353    class WebhookEnabledType(object):
354        TRUE = "true"
355        FALSE = "false"
356
357    def __init__(self, version, payload, service_sid, channel_sid, sid=None):
358        """
359        Initialize the MessageInstance
360
361        :returns: twilio.rest.chat.v2.service.channel.message.MessageInstance
362        :rtype: twilio.rest.chat.v2.service.channel.message.MessageInstance
363        """
364        super(MessageInstance, self).__init__(version)
365
366        # Marshaled Properties
367        self._properties = {
368            'sid': payload.get('sid'),
369            'account_sid': payload.get('account_sid'),
370            'attributes': payload.get('attributes'),
371            'service_sid': payload.get('service_sid'),
372            'to': payload.get('to'),
373            'channel_sid': payload.get('channel_sid'),
374            'date_created': deserialize.iso8601_datetime(payload.get('date_created')),
375            'date_updated': deserialize.iso8601_datetime(payload.get('date_updated')),
376            'last_updated_by': payload.get('last_updated_by'),
377            'was_edited': payload.get('was_edited'),
378            'from_': payload.get('from'),
379            'body': payload.get('body'),
380            'index': deserialize.integer(payload.get('index')),
381            'type': payload.get('type'),
382            'media': payload.get('media'),
383            'url': payload.get('url'),
384        }
385
386        # Context
387        self._context = None
388        self._solution = {
389            'service_sid': service_sid,
390            'channel_sid': channel_sid,
391            'sid': sid or self._properties['sid'],
392        }
393
394    @property
395    def _proxy(self):
396        """
397        Generate an instance context for the instance, the context is capable of
398        performing various actions.  All instance actions are proxied to the context
399
400        :returns: MessageContext for this MessageInstance
401        :rtype: twilio.rest.chat.v2.service.channel.message.MessageContext
402        """
403        if self._context is None:
404            self._context = MessageContext(
405                self._version,
406                service_sid=self._solution['service_sid'],
407                channel_sid=self._solution['channel_sid'],
408                sid=self._solution['sid'],
409            )
410        return self._context
411
412    @property
413    def sid(self):
414        """
415        :returns: The unique string that identifies the resource
416        :rtype: unicode
417        """
418        return self._properties['sid']
419
420    @property
421    def account_sid(self):
422        """
423        :returns: The SID of the Account that created the resource
424        :rtype: unicode
425        """
426        return self._properties['account_sid']
427
428    @property
429    def attributes(self):
430        """
431        :returns: The JSON string that stores application-specific data
432        :rtype: unicode
433        """
434        return self._properties['attributes']
435
436    @property
437    def service_sid(self):
438        """
439        :returns: The SID of the Service that the resource is associated with
440        :rtype: unicode
441        """
442        return self._properties['service_sid']
443
444    @property
445    def to(self):
446        """
447        :returns: The SID of the Channel that the message was sent to
448        :rtype: unicode
449        """
450        return self._properties['to']
451
452    @property
453    def channel_sid(self):
454        """
455        :returns: The SID of the Channel the Message resource belongs to
456        :rtype: unicode
457        """
458        return self._properties['channel_sid']
459
460    @property
461    def date_created(self):
462        """
463        :returns: The RFC 2822 date and time in GMT when the resource was created
464        :rtype: datetime
465        """
466        return self._properties['date_created']
467
468    @property
469    def date_updated(self):
470        """
471        :returns: The RFC 2822 date and time in GMT when the resource was last updated
472        :rtype: datetime
473        """
474        return self._properties['date_updated']
475
476    @property
477    def last_updated_by(self):
478        """
479        :returns: The Identity of the User who last updated the Message
480        :rtype: unicode
481        """
482        return self._properties['last_updated_by']
483
484    @property
485    def was_edited(self):
486        """
487        :returns: Whether the message has been edited since  it was created
488        :rtype: bool
489        """
490        return self._properties['was_edited']
491
492    @property
493    def from_(self):
494        """
495        :returns: The Identity of the message's author
496        :rtype: unicode
497        """
498        return self._properties['from_']
499
500    @property
501    def body(self):
502        """
503        :returns: The content of the message
504        :rtype: unicode
505        """
506        return self._properties['body']
507
508    @property
509    def index(self):
510        """
511        :returns: The index of the message within the Channel
512        :rtype: unicode
513        """
514        return self._properties['index']
515
516    @property
517    def type(self):
518        """
519        :returns: The Message type
520        :rtype: unicode
521        """
522        return self._properties['type']
523
524    @property
525    def media(self):
526        """
527        :returns: A Media object that describes the Message's media if attached; otherwise, null
528        :rtype: dict
529        """
530        return self._properties['media']
531
532    @property
533    def url(self):
534        """
535        :returns: The absolute URL of the Message resource
536        :rtype: unicode
537        """
538        return self._properties['url']
539
540    def fetch(self):
541        """
542        Fetch the MessageInstance
543
544        :returns: The fetched MessageInstance
545        :rtype: twilio.rest.chat.v2.service.channel.message.MessageInstance
546        """
547        return self._proxy.fetch()
548
549    def delete(self, x_twilio_webhook_enabled=values.unset):
550        """
551        Deletes the MessageInstance
552
553        :param MessageInstance.WebhookEnabledType x_twilio_webhook_enabled: The X-Twilio-Webhook-Enabled HTTP request header
554
555        :returns: True if delete succeeds, False otherwise
556        :rtype: bool
557        """
558        return self._proxy.delete(x_twilio_webhook_enabled=x_twilio_webhook_enabled, )
559
560    def update(self, body=values.unset, attributes=values.unset,
561               date_created=values.unset, date_updated=values.unset,
562               last_updated_by=values.unset, from_=values.unset,
563               x_twilio_webhook_enabled=values.unset):
564        """
565        Update the MessageInstance
566
567        :param unicode body: The message to send to the channel
568        :param unicode attributes: A valid JSON string that contains application-specific data
569        :param datetime date_created: The ISO 8601 date and time in GMT when the resource was created
570        :param datetime date_updated: The ISO 8601 date and time in GMT when the resource was updated
571        :param unicode last_updated_by: The Identity of the User who last updated the Message, if applicable
572        :param unicode from_: The Identity of the message's author
573        :param MessageInstance.WebhookEnabledType x_twilio_webhook_enabled: The X-Twilio-Webhook-Enabled HTTP request header
574
575        :returns: The updated MessageInstance
576        :rtype: twilio.rest.chat.v2.service.channel.message.MessageInstance
577        """
578        return self._proxy.update(
579            body=body,
580            attributes=attributes,
581            date_created=date_created,
582            date_updated=date_updated,
583            last_updated_by=last_updated_by,
584            from_=from_,
585            x_twilio_webhook_enabled=x_twilio_webhook_enabled,
586        )
587
588    def __repr__(self):
589        """
590        Provide a friendly representation
591
592        :returns: Machine friendly representation
593        :rtype: str
594        """
595        context = ' '.join('{}={}'.format(k, v) for k, v in self._solution.items())
596        return '<Twilio.Chat.V2.MessageInstance {}>'.format(context)
597