def reply_to_name(self): """Handle the "name" keyword.""" try: # update person's name: self.contact.first_name = self.sms_body.split()[1].strip() self.contact.last_name = " ".join(self.sms_body.split()[2:]).strip( ) if not self.contact.last_name: raise ValidationError('No last name') self.contact.save() # update old messages with this person's name async('apostello.tasks.update_msgs_name', self.contact.pk) # thank person async( 'apostello.tasks.notify_office_mail', '[Apostello] New Signup!', 'SMS:\n\t{0}\nFrom:\n\t{1} ({2})\n'.format( self.sms_body, str(self.contact), self.contact_number, ), ) # TODO update to use .format() and add help text to model return fetch_default_reply( 'name_update_reply' ) % self.contact.first_name except (ValidationError, IndexError): async( 'apostello.tasks.notify_office_mail', '[Apostello] New Signup - FAILED!', 'SMS:\n\t{0}\nFrom:\n\t{1}\n'.format( self.sms_body, self.contact_number ), ) return fetch_default_reply('name_failure_reply')
def reply_to_name(self): """Handle the "name" keyword.""" try: # update person's name: self.contact.first_name = self.sms_body.split()[1].strip() last_name = " ".join(self.sms_body.split()[2:]).strip() if not last_name: raise ValidationError('No last name') last_name = last_name.split('\n')[0] last_name = last_name[0:40] # truncate last name self.contact.last_name = last_name self.contact.save() # update old messages with this person's name async ('apostello.tasks.update_msgs_name', self.contact.pk) # thank person async ( 'apostello.tasks.notify_office_mail', '[Apostello] New Signup!', 'SMS:\n\t{0}\nFrom:\n\t{1} ({2})\n'.format( self.sms_body, str(self.contact), self.contact_number, ), ) # TODO update to use .format() and add help text to model return fetch_default_reply( 'name_update_reply') % self.contact.first_name except (ValidationError, IndexError): async ( 'apostello.tasks.notify_office_mail', '[Apostello] New Signup - FAILED!', 'SMS:\n\t{0}\nFrom:\n\t{1}\n'.format(self.sms_body, self.contact_number), ) return fetch_default_reply('name_failure_reply')
def reply_to_incoming(person_from, from_, sms_body, keyword): """Construct appropriate reply.""" # update outgoing log 1 minute from now: check_outgoing_log.apply_async(countdown=60) if keyword == "start": person_from.is_blocking = False person_from.save() return fetch_default_reply('start_reply') elif keyword == "stop": person_from.is_blocking = True person_from.save() warn_on_blacklist.delay(person_from.id) return '' elif keyword == "name": warn_on_blacklist_receipt.delay(person_from.id, sms_body) try: # update person's name: person_from.first_name = sms_body.split()[1].strip() person_from.last_name = " ".join(sms_body.split()[2:]).strip() if not person_from.last_name: raise ValidationError('No last name') person_from.save() # update old messages with this person's name update_msgs_name.delay(person_from.id) # thank person notify_office_mail.delay( '[Apostello] New Signup!', 'SMS:\n\t{0}\nFrom:\n\t{1}\n'.format( sms_body, from_, ), ) # TODO update to use .format() and add help text to model return fetch_default_reply( 'name_update_reply' ) % person_from.first_name except (ValidationError, IndexError): notify_office_mail.delay( '[Apostello] New Signup - FAILED!', 'SMS:\n\t{0}\nFrom:\n\t{1}\n'.format( sms_body, from_ ), ) return fetch_default_reply('name_failure_reply') else: # otherwise construct reply warn_on_blacklist_receipt.delay(person_from.id, sms_body) return keyword_replier(keyword, person_from)
def get_person_or_ask_for_name(from_, sms_body, keyword_obj): try: person_from = Recipient.objects.get(number=from_) except Recipient.DoesNotExist: person_from = Recipient.objects.create(number=from_, first_name='Unknown', last_name='Person') person_from.save() if keyword_obj == "name": pass else: from apostello.models import SiteConfiguration config = SiteConfiguration.get_solo() if not config.disable_all_replies: person_from.send_message( content=fetch_default_reply('auto_name_request'), sent_by="auto name request" ) notify_office_mail.delay( '[Apostello] Unknown Contact!', 'SMS: {0}\nFrom: {1}\n\n\nThis person is unknown and has been asked for their name.'.format( sms_body, from_ ), ) return person_from
def test_early(self, recipients, keywords): assert keywords['test_early'].construct_reply( recipients['calvin'] ) == recipients['calvin'].personalise( fetch_default_reply('default_no_keyword_not_live') .replace("%keyword%", str(keywords['test_early'])) )
def get_person_or_ask_for_name(from_, sms_body, keyword_obj): """ Return the Recipient object for the sender of the message. Perform a look up on the sender of the message. If they exist in the system, they are returned. Otherwise a message is queued to ask them for their name. """ try: person_from = Recipient.objects.get(number=from_) except Recipient.DoesNotExist: person_from = Recipient.objects.create(number=from_, first_name='Unknown', last_name='Person') person_from.save() if keyword_obj == "name": pass else: from site_config.models import SiteConfiguration config = SiteConfiguration.get_solo() if not config.disable_all_replies: person_from.send_message( content=fetch_default_reply('auto_name_request'), sent_by="auto name request") notify_office_mail.delay( '[Apostello] Unknown Contact!', 'SMS: {0}\nFrom: {1}\n\n\nThis person is unknown and has been asked for their name.' .format(sms_body, from_), ) return person_from
def reply_to_incoming(person_from, from_, sms_body, keyword): """Construct appropriate reply.""" # update outgoing log 1 minute from now: check_outgoing_log.apply_async(countdown=60) if keyword == "start": person_from.is_blocking = False person_from.save() return fetch_default_reply('start_reply') elif keyword == "stop": person_from.is_blocking = True person_from.save() warn_on_blacklist.delay(person_from.id) return '' elif keyword == "name": warn_on_blacklist_receipt.delay(person_from.id, sms_body) try: # update person's name: person_from.first_name = sms_body.split()[1].strip() person_from.last_name = " ".join(sms_body.split()[2:]).strip() if not person_from.last_name: raise ValidationError('No last name') person_from.save() # update old messages with this person's name update_msgs_name.delay(person_from.id) # thank person notify_office_mail.delay( '[Apostello] New Signup!', 'SMS:\n\t{0}\nFrom:\n\t{1}\n'.format( sms_body, from_, ), ) # TODO update to use .format() and add help text to model return fetch_default_reply( 'name_update_reply') % person_from.first_name except (ValidationError, IndexError): notify_office_mail.delay( '[Apostello] New Signup - FAILED!', 'SMS:\n\t{0}\nFrom:\n\t{1}\n'.format(sms_body, from_), ) return fetch_default_reply('name_failure_reply') else: # otherwise construct reply warn_on_blacklist_receipt.delay(person_from.id, sms_body) return keyword_replier(keyword, person_from)
def test_no_existing_keyword(self, recipients): msg = InboundSms({ "From": str(recipients["calvin"].number), "Body": "nope" }) reply = msg.construct_reply() assert reply == fetch_default_reply("keyword_no_match").replace( "%name%", "John")
def test_no_existing_keyword(self, recipients): msg = InboundSms({ 'From': str(recipients['calvin'].number), 'Body': 'nope' }) reply = msg.construct_reply() assert reply == fetch_default_reply('keyword_no_match').replace( "%name%", "John")
def test_no_existing_keyword(self, recipients): assert keyword_replier( None, recipients[ 'calvin' ] ) == fetch_default_reply('keyword_no_match').replace( "%name%", "John" )
def keyword_replier(k, person_from): try: reply = k.construct_reply(person_from) except AttributeError: reply = fetch_default_reply('keyword_no_match') reply = person_from.personalise(reply) return reply
def current_response(self): """Currently active response""" if not self.is_live: if self.deactivated_response != '' and self.has_ended: reply = self.deactivated_response elif self.too_early_response != '' and self.has_not_started: reply = self.too_early_response else: reply = fetch_default_reply('default_no_keyword_not_live') else: # keyword is active if self.custom_response == '': # no custom response, use generic form reply = fetch_default_reply('default_no_keyword_auto_reply') else: # use custom response reply = self.custom_response return reply.replace("%keyword%", self.keyword)
def test_no_custom_reply(self, recipients, keywords): assert keywords['test2'].construct_reply( recipients[ 'calvin' ] ) == recipients['calvin'].personalise( fetch_default_reply( 'default_no_keyword_auto_reply' ) )
def construct_reply(self, sender): """Makes reply to an incoming message""" # check if keyword is active if not self.is_live(): if self.deactivated_response != '' and timezone.now() > self.deactivate_time: reply = sender.personalise(self.deactivated_response) elif self.too_early_response != '' and timezone.now() < self.activate_time: reply = sender.personalise(self.too_early_response) else: reply = sender.personalise(fetch_default_reply('default_no_keyword_not_live')).replace("%keyword%", self.keyword) else: # keyword is active if self.custom_response == '': # no custom response, use generic form reply = sender.personalise(fetch_default_reply('default_no_keyword_auto_reply')) else: # use custom response reply = sender.personalise(self.custom_response) return reply
def test_no_existing_keyword(self, recipients): msg = InboundSms( { 'From': str(recipients['calvin'].number), 'Body': 'nope' } ) reply = msg.construct_reply() assert reply == fetch_default_reply('keyword_no_match').replace( "%name%", "John" )
def get_current_response(self, recipient=None): if self.disable_all_replies: return '' if not self.is_live: if self.deactivated_response != '' and self.has_ended: reply = self.deactivated_response elif self.too_early_response != '' and self.has_not_started: reply = self.too_early_response else: reply = fetch_default_reply('default_no_keyword_not_live') else: # keyword is active reply = self.custom_response or fetch_default_reply('default_no_keyword_auto_reply') if recipient is not None: # check if user is unknown if recipient.first_name == 'Unknown': reply = self.custom_response_new_person or reply return reply.replace("%keyword%", self.keyword)
def get_current_response(self, recipient=None): if self.disable_all_replies: return '' if not self.is_live: if self.deactivated_response != '' and self.has_ended: reply = self.deactivated_response elif self.too_early_response != '' and self.has_not_started: reply = self.too_early_response else: reply = fetch_default_reply('default_no_keyword_not_live') else: # keyword is active reply = self.custom_response or fetch_default_reply( 'default_no_keyword_auto_reply') if recipient is not None: # check if user is unknown if recipient.first_name == 'Unknown': reply = self.custom_response_new_person or reply return reply.replace("%keyword%", self.keyword)
def keyword_replier(k, person_from): """ Construct reply to message. Attempts to use the keyword's reply. If not a valid keyword, then the no match reply is used. """ try: reply = k.construct_reply(person_from) except AttributeError: reply = fetch_default_reply('keyword_no_match') reply = person_from.personalise(reply) return reply
def ask_for_name(person_from_pk, sms_body, ask_for_name): """Asks a contact to provide their name.""" if not ask_for_name: return from site_config.models import SiteConfiguration config = SiteConfiguration.get_solo() if not config.disable_all_replies: from apostello.models import Recipient contact = Recipient.objects.get(pk=person_from_pk) contact.send_message(content=fetch_default_reply('auto_name_request'), sent_by="auto name request") async ( 'apostello.tasks.notify_office_mail', '[Apostello] Unknown Contact!', 'SMS: {0}\nFrom: {1}\n\n\nThis person is unknown and has been' ' asked for their name.'.format(sms_body, str(contact)), )
def ask_for_name(person_from_pk, sms_body, ask_for_name): """Asks a contact to provide their name.""" if not ask_for_name: return from site_config.models import SiteConfiguration config = SiteConfiguration.get_solo() if not config.disable_all_replies: from apostello.models import Recipient contact = Recipient.objects.get(pk=person_from_pk) contact.send_message( content=fetch_default_reply('auto_name_request'), sent_by="auto name request" ) async( 'apostello.tasks.notify_office_mail', '[Apostello] Unknown Contact!', 'SMS: {0}\nFrom: {1}\n\n\nThis person is unknown and has been' ' asked for their name.'.format(sms_body, str(contact)), )
def construct_reply(self): """Construct appropriate reply.""" if self.contact.do_not_reply: return '' if self.keyword == "start": reply = self.reply_to_start() elif self.keyword == "stop": reply = self.reply_to_stop() elif self.keyword == "name": reply = self.reply_to_name() else: # otherwise construct reply try: reply = self.keyword.construct_reply(self.contact) except AttributeError: reply = fetch_default_reply('keyword_no_match') reply = self.contact.personalise(reply) if self.contact.is_blocking: return '' else: return reply
def test_no_custom_reply(self, recipients, keywords): assert keywords['test2'].construct_reply( recipients['calvin']) == recipients['calvin'].personalise( fetch_default_reply('default_no_keyword_auto_reply'))
def test_early(self, recipients, keywords): assert keywords['test_early'].construct_reply( recipients['calvin']) == recipients['calvin'].personalise( fetch_default_reply('default_no_keyword_not_live').replace( "%keyword%", str(keywords['test_early'])))
def test_existing_keyword_new_contact(self, keywords): msg = InboundSms({"From": "+447927401749", "Body": "2test msg"}) reply = msg.construct_reply() assert reply == fetch_default_reply( "default_no_keyword_auto_reply").replace("%name%", "Unknown")
def test_existing_keyword_new_contact(self, keywords): msg = InboundSms({'From': '+447927401749', 'Body': '2test msg'}) reply = msg.construct_reply() assert reply == fetch_default_reply( 'default_no_keyword_auto_reply').replace('%name%', 'Unknown')
def test_early(self, recipients, keywords): assert keywords["test_early"].construct_reply( recipients["calvin"]) == recipients["calvin"].personalise( fetch_default_reply("default_no_keyword_not_live").replace( "%keyword%", str(keywords["test_early"])))
def test_no_existing_keyword(self, recipients): assert keyword_replier(None, recipients['calvin']) == fetch_default_reply( 'keyword_no_match').replace( "%name%", "John")
def reply_to_start(self): """Reply to the "start" keyword.""" self.contact.is_blocking = False self.contact.save() return fetch_default_reply('start_reply')
def test_existing_keyword_new_contact(self, keywords): msg = InboundSms({'From': '+447927401749', 'Body': '2test msg'}) reply = msg.construct_reply() assert reply == fetch_default_reply('default_no_keyword_auto_reply').replace('%name%', 'Unknown')
def test_no_custom_reply(self, recipients, keywords): assert keywords["test2"].construct_reply( recipients["calvin"]) == recipients["calvin"].personalise( fetch_default_reply("default_no_keyword_auto_reply"))