Example #1
0
    def send_response_to(self, text, in_reply_to=None):
        """
        Use to send a response to a received message.
        
        - in_reply_to -- the msg you are respoding to. This uses the
                 associated CommunicationChannel to know
                 how to send the reply. E.g. if the Contact
                 has CommConnections to multiple cell phone
                 companies or email acconts--that is they have
                 multiple phone numbers or email addresses--
                 this will make sure the response goes to the
                 number/address that the user used to send in 
                 the original message.

        """
        if in_reply_to is not None:
            cc = connection_from_message(in_reply_to)
        else:
            cc = self.connection_created_from

        self.send_to(text, cc)
Example #2
0
    def get_signature(self, max_len=None, for_message=None):
        """
        Return a string suitable for signing an SMS.
        
        - max_len -- max length in characters. Watch out! It may be
                     '' (blank str) if max_len is too small to write
                     anything meaningful

        - for_message -- if the sig is used on a response to a message,
                     or for a new one, pass that message in and the 
                     sig will include the associated id--e.g. if you
                     received a message on a pygsm backend talking to Orange
                     and you pass that message, you get the user's Orange number
                     in the sig
                     
        """

        # First try to make a name portion in this order
        # of what values are set:
        #
        # 1. common_name
        # 2. given_name family_name
        #
        # Then see if name_part + identity fit under
        # max.
        #
        # If not, see if identifier alone fits under and
        # truncate name part.
        #
        # If identifier alone doesn't fit center ltruncate it
        # so +14155551212 => ...1212
        # If that doesn't fit, return ''

        name_part = None if utils.empty_str(self.common_name) else self.common_name.strip()

        if utils.empty_str(name_part):
            gn = None if utils.empty_str(self.reporter.first_name) else self.reporter.first_name.strip()
            fn = None if utils.empty_str(self.reporter.last_name) else self.reporter.last_name.strip()
            if gn is not None and fn is not None:
                name_part = u"%s %s" % (gn, fn)
            elif gn is not None:
                name_part = gn
            else:
                name_part = fn

        # default to DB id so you can at least look 'em up
        identity = str(self.id)
        # try to get phone number from a channel connection
        if for_message is not None:
            cc = connection_from_message(for_message, False)
        else:
            # take first one
            ccs = self.reporter.connections.all()
            if len(ccs) == 0:
                # hmmm, user exists only in DB. Has
                # never contacted system...
                cc = None
            else:
                cc = ccs[0]
        if cc is not None:
            identity = cc.identity

        # make sig
        if not utils.empty_str(name_part):
            sig = ": ".join([name_part, identity])
        else:
            sig = identity
        if max_len is not None:
            # adjust for max len
            if len(sig) > max_len:
                sig = identity
            if len(sig) > max_len:
                sig = "...%s" % identity[-4:]
            if len(sig) > max_len:
                sig = ""
        return sig