def check_log_consistent(page_id): from apostello.models import Keyword, Recipient, SmsInbound check_next_page = False for x in twilio_client.messages.list(page=page_id, page_size=50, to=settings.TWILIO_FROM_NUM): sms, created = SmsInbound.objects.get_or_create(sid=x.sid) if created: sender, s_created = Recipient.objects.get_or_create(number=x.from_) if s_created: sender.first_name = 'Unknown' sender.last_name = 'Person' sender.save() sms.content = x.body sms.time_received = timezone.make_aware(x.date_created, timezone.get_current_timezone()) sms.sender_name = str(sender) sms.sender_num = x.from_ sms.matched_keyword = str(Keyword.match(x.body.strip())) sms.matched_colour = Keyword.lookup_colour(x.body.strip()) sms.is_archived = True sms.save() check_next_page = True if check_next_page: check_log_consistent.delay(page_id + 1)
def import_incoming_sms(): """ Loops over all incoming messages in Twilio's logs and adds them to our db. """ try: sms_page = twilio_client.messages.iter(to_=settings.TWILIO_FROM_NUM) for x in sms_page: try: sms, created = SmsInbound.objects.get_or_create( sid=x.sid, time_received=timezone.now() ) if created: sender, s_created = Recipient.objects.get_or_create(number=x.from_) if s_created: sender.first_name = 'Unknown' sender.last_name = 'Person' sender.save() sms.content = x.body sms.time_received = timezone.make_aware(x.date_sent, timezone.get_current_timezone()) sms.sender_name = str(sender) sms.sender_num = x.from_ matched_keyword = Keyword.match(x.body) sms.matched_keyword = str(matched_keyword) sms.matched_colour = Keyword.lookup_colour(x.body) sms.matched_link = Keyword.get_log_link(matched_keyword) sms.save() except Exception as e: print(e) except TwilioRestException as e: if e.code == 20008: return 'test credentials used'
def log_msg_in(p, t, from_pk): 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_link=Keyword.get_log_link(matched_keyword), matched_colour=Keyword.lookup_colour(p['Body'].strip())) # check log is consistent: check_log_consistent.delay(0)
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_link=Keyword.get_log_link(matched_keyword), matched_colour=Keyword.lookup_colour(p['Body'].strip())) # check log is consistent: check_incoming_log.delay()
def sms(request): """ Handles 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) post_to_slack.delay( "{0}\nFrom: {1}\n(matched: {2})".format( sms_body, str(person_from), str(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_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_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_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)
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_task("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()
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 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 __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 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_lookup_colour_none(self): assert Keyword.lookup_colour("nope") == "#B6B6B6"
def test_lookup_colour_name(self): assert Keyword.lookup_colour('name John Calvin') == '#BBDEFB'
def test_empty(self): assert Keyword.match("") == "No Match"
def test_info(self): for x in ["help", "info"]: assert Keyword.match("{0}".format(x)) == 'info'
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_lookup_colour_test(self, keywords): assert Keyword.lookup_colour('test') == '#098f6b'
def test_lookup_colour_stop(self): assert Keyword.lookup_colour('stop') == '#FFCDD2'
def test_name(self): assert Keyword.match("name John Calvin") == 'name'
def test_start(self): for x in ["start", "yes"]: assert Keyword.match("{0}".format(x)) == 'start'
def test_lookup_colour_none(self): assert Keyword.lookup_colour('nope') == '#B6B6B6'
def test_match(self, keywords): keyword_ = Keyword.match("test matching") assert str(keyword_) == "test" assert type(keyword_) == Keyword
def test_lookup_colour_test(self, keywords): assert Keyword.lookup_colour("test") == "#098f6b"
def test_no_match(self, keywords): assert Keyword.match("nope") == 'No Match'
def test_get_log_link_str(self, keywords): assert Keyword.get_log_link('test_no_link') == '#'
def test_get_log_link_keyword(self, keywords): assert Keyword.get_log_link( keywords['test'] ) == '/keyword/responses/{0}/'.format(keywords['test'].pk)
def test_get_log_link_keyword(self, keywords): assert Keyword.get_log_link( keywords['test']) == '/keyword/responses/{0}/'.format( keywords['test'].pk)
def test_lookup_colour_stop(self): assert Keyword.lookup_colour("stop") == "#FFCDD2"
def test_lookup_colour_name(self): assert Keyword.lookup_colour("name John Calvin") == "#BBDEFB"
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)
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_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_get_log_link_keyword(self, keywords): assert Keyword.get_log_link( keywords['test']) == '/keyword/responses/test/'