def test_stop_start(self, recipients): sms_body = "stop " k_obj = Keyword.match(sms_body) reply_to_incoming(recipients['calvin'], recipients['calvin'].number, sms_body, k_obj) assert recipients['calvin'].is_blocking sms_body = "start" k_obj = Keyword.match(sms_body) reply_to_incoming(recipients['calvin'], recipients['calvin'].number, sms_body, k_obj) assert recipients['calvin'].is_blocking is False
def log_msg_in(p, t, from_pk): """Log incoming message.""" from apostello.models import Keyword, SmsInbound, Recipient from_ = Recipient.objects.get(pk=from_pk) matched_keyword = Keyword.match(p['Body'].strip()) SmsInbound.objects.create(sid=p['MessageSid'], content=p['Body'], time_received=t, sender_name=str(from_), sender_num=p['From'], matched_keyword=str(matched_keyword), matched_colour=Keyword.lookup_colour( p['Body'].strip())) # check log is consistent: async ('apostello.tasks.check_incoming_log')
def __init__(self, msg_params): self.msg_params = msg_params self.contact_number = msg_params['From'] self.sms_body = msg_params['Body'].strip() # match keyword: self.keyword = Keyword.match(self.sms_body) # look up contact and determine if we need to ask for their name: self.contact, self.send_name_sms = self.lookup_contact() # construct reply sms self.reply = self.construct_reply() # add contact to keyword linked groups: try: self.keyword.add_contact_to_groups(self.contact) except AttributeError: # not a custom keyword pass
def handle_incoming_sms(msg): """Add incoming sms to log.""" sms, created = SmsInbound.objects.get_or_create(sid=msg.sid) if created: check_next_page = True sender, s_created = Recipient.objects.get_or_create(number=msg.from_) if s_created: sender.first_name = 'Unknown' sender.last_name = 'Person' sender.save() sms.content = msg.body sms.time_received = timezone.make_aware( msg.date_created, timezone.get_current_timezone()) sms.sender_name = str(sender) sms.sender_num = msg.from_ matched_keyword = Keyword.match(msg.body) sms.matched_keyword = str(matched_keyword) sms.matched_colour = Keyword.lookup_colour(msg.body) sms.matched_link = Keyword.get_log_link(matched_keyword) sms.save() return check_next_page
def sms(request): """ Handle all incoming messages from Twilio. This is the start of the message processing pipeline. """ r = twiml.Response() params = request.POST from_ = params['From'] sms_body = params['Body'].strip() keyword_obj = Keyword.match(sms_body) # get person object and optionally ask for their name person_from = get_person_or_ask_for_name(from_, sms_body, keyword_obj) log_msg_in.delay(params, timezone.now(), person_from.pk) sms_to_slack.delay(sms_body, person_from, keyword_obj) reply = reply_to_incoming(person_from, from_, sms_body, keyword_obj) config = SiteConfiguration.get_solo() if not config.disable_all_replies: r.message(reply) return r
def test_no_match(self, keywords): assert Keyword.match("nope") == 'No Match'
def test_match(self, keywords): keyword_ = Keyword.match("test matching") assert str(keyword_) == "test" assert type(keyword_) == Keyword
def test_empty(self): assert Keyword.match("") == "No Match"
def test_name(self): assert Keyword.match("name John Calvin") == 'name'
def test_info(self): for x in ["help", "info"]: assert Keyword.match("{0}".format(x)) == 'info'
def test_start(self): for x in ["start", "yes"]: assert Keyword.match("{0}".format(x)) == 'start'
def test_stop(self): assert Keyword.match("Stop it!") == 'stop' assert Keyword.match("stop ") == 'stop' assert Keyword.match("\nSTOP ") == 'stop' for x in ["stopall", "unsubscribe", "cancel", "end", "quit"]: assert Keyword.match("{0}".format(x)) == 'stop'
def test_other(self, recipients): sms_body = "test message" k_obj = Keyword.match(sms_body) r_new = reply_to_incoming(recipients['calvin'], recipients['calvin'].number, sms_body, k_obj) assert "" in str(r_new)
def test_only_one_name(self, recipients): sms_body = "name JohnCalvin" k_obj = Keyword.match(sms_body) r_new = reply_to_incoming(recipients['calvin'], recipients['calvin'].number, sms_body, k_obj) assert "Something went wrong" in str(r_new)
def test_name(self, recipients): sms_body = "name John Calvin" k_obj = Keyword.match(sms_body) reply = reply_to_incoming(recipients['calvin'], recipients['calvin'].number, sms_body, k_obj) assert "John" in str(reply)