~dongpo-deng/sahana-eden/test

« back to all changes in this revision

Viewing changes to modules/pygsm/message/incoming.py

  • Committer: Deng Dongpo
  • Date: 2010-08-01 09:29:44 UTC
  • Revision ID: dongpo@dhcp-21193.iis.sinica.edu.tw-20100801092944-8t9obt4xtl7otesb
initial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# vim: ai ts=4 sts=4 et sw=4
 
3
 
 
4
 
 
5
import datetime
 
6
import pytz
 
7
 
 
8
 
 
9
class IncomingMessage(object):
 
10
    def __init__(self, device, sender, sent, text):
 
11
 
 
12
        # move the arguments into "private" attrs,
 
13
        # to try to prevent from from being modified
 
14
        self._device = device
 
15
        self._sender = sender
 
16
        self._sent   = sent
 
17
        self._text   = text
 
18
 
 
19
        # assume that the message was
 
20
        # received right now, since we
 
21
        # don't have an incoming buffer
 
22
        self._received = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
 
23
 
 
24
 
 
25
    def __repr__(self):
 
26
        return "<pygsm.IncomingMessage from %s: %r>" %\
 
27
            (self.sender, self.text)
 
28
 
 
29
 
 
30
    def respond(self, text):
 
31
        """Responds to this IncomingMessage by sending a message containing
 
32
           _text_ back to the sender via the modem that created this object."""
 
33
        return self.device.send_sms(self.sender, text)
 
34
 
 
35
 
 
36
    @property
 
37
    def device(self):
 
38
        """Returns the pygsm.GsmModem device which received
 
39
           the SMS, and created this IncomingMessage object."""
 
40
        return self._device
 
41
 
 
42
    @property
 
43
    def sender(self):
 
44
        """Returns the phone number of the originator of this IncomingMessage.
 
45
           It is stored directly as reported by the modem, so no assumptions
 
46
           can be made about it's format."""
 
47
        return self._sender
 
48
 
 
49
    @property
 
50
    def sent(self):
 
51
        """Returns a datetime object containing the date and time that this
 
52
           IncomingMessage was sent, as reported by the modem. Sometimes, a
 
53
           network or modem will not report this field, so it will be None."""
 
54
        return self._sent
 
55
 
 
56
    @property
 
57
    def text(self):
 
58
        """Returns the text contents of this IncomingMessage. It will usually
 
59
           be 160 characters or less, by virtue of being an SMS, but multipart
 
60
           messages can, technically, be up to 39015 characters long."""
 
61
        return self._text
 
62
 
 
63
    @property
 
64
    def received(self):
 
65
        """Returns a datetime object containing the date and time that this
 
66
           IncomingMessage was created, which is a close aproximation of when
 
67
           the SMS was received."""
 
68
        return self._received