Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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'
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
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
Ejemplo n.º 5
0
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()
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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)
Ejemplo n.º 9
0
 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)
Ejemplo n.º 10
0
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")
Ejemplo n.º 11
0
 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()
Ejemplo n.º 12
0
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
Ejemplo n.º 13
0
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
Ejemplo n.º 14
0
 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
Ejemplo n.º 15
0
 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
Ejemplo n.º 16
0
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
Ejemplo n.º 17
0
 def test_lookup_colour_none(self):
     assert Keyword.lookup_colour("nope") == "#B6B6B6"
Ejemplo n.º 18
0
 def test_lookup_colour_name(self):
     assert Keyword.lookup_colour('name John Calvin') == '#BBDEFB'
Ejemplo n.º 19
0
 def test_empty(self):
     assert Keyword.match("") == "No Match"
Ejemplo n.º 20
0
 def test_info(self):
     for x in ["help", "info"]:
         assert Keyword.match("{0}".format(x)) == 'info'
Ejemplo n.º 21
0
 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'
Ejemplo n.º 22
0
 def test_lookup_colour_test(self, keywords):
     assert Keyword.lookup_colour('test') == '#098f6b'
Ejemplo n.º 23
0
 def test_lookup_colour_name(self):
     assert Keyword.lookup_colour('name John Calvin') == '#BBDEFB'
Ejemplo n.º 24
0
 def test_lookup_colour_stop(self):
     assert Keyword.lookup_colour('stop') == '#FFCDD2'
Ejemplo n.º 25
0
 def test_empty(self):
     assert Keyword.match("") == "No Match"
Ejemplo n.º 26
0
 def test_name(self):
     assert Keyword.match("name John Calvin") == 'name'
Ejemplo n.º 27
0
 def test_info(self):
     for x in ["help", "info"]:
         assert Keyword.match("{0}".format(x)) == 'info'
Ejemplo n.º 28
0
 def test_start(self):
     for x in ["start", "yes"]:
         assert Keyword.match("{0}".format(x)) == 'start'
Ejemplo n.º 29
0
 def test_lookup_colour_none(self):
     assert Keyword.lookup_colour('nope') == '#B6B6B6'
Ejemplo n.º 30
0
 def test_match(self, keywords):
     keyword_ = Keyword.match("test matching")
     assert str(keyword_) == "test"
     assert type(keyword_) == Keyword
Ejemplo n.º 31
0
 def test_lookup_colour_test(self, keywords):
     assert Keyword.lookup_colour("test") == "#098f6b"
Ejemplo n.º 32
0
 def test_no_match(self, keywords):
     assert Keyword.match("nope") == 'No Match'
Ejemplo n.º 33
0
 def test_get_log_link_str(self, keywords):
     assert Keyword.get_log_link('test_no_link') == '#'
Ejemplo n.º 34
0
 def test_get_log_link_keyword(self, keywords):
     assert Keyword.get_log_link(
         keywords['test']
     ) == '/keyword/responses/{0}/'.format(keywords['test'].pk)
Ejemplo n.º 35
0
 def test_start(self):
     for x in ["start", "yes"]:
         assert Keyword.match("{0}".format(x)) == 'start'
Ejemplo n.º 36
0
 def test_match(self, keywords):
     keyword_ = Keyword.match("test matching")
     assert str(keyword_) == "test"
     assert type(keyword_) == Keyword
Ejemplo n.º 37
0
 def test_name(self):
     assert Keyword.match("name John Calvin") == 'name'
Ejemplo n.º 38
0
 def test_get_log_link_keyword(self, keywords):
     assert Keyword.get_log_link(
         keywords['test']) == '/keyword/responses/{0}/'.format(
             keywords['test'].pk)
Ejemplo n.º 39
0
 def test_lookup_colour_stop(self):
     assert Keyword.lookup_colour('stop') == '#FFCDD2'
Ejemplo n.º 40
0
 def test_lookup_colour_stop(self):
     assert Keyword.lookup_colour("stop") == "#FFCDD2"
Ejemplo n.º 41
0
 def test_lookup_colour_none(self):
     assert Keyword.lookup_colour('nope') == '#B6B6B6'
Ejemplo n.º 42
0
 def test_lookup_colour_name(self):
     assert Keyword.lookup_colour("name John Calvin") == "#BBDEFB"
Ejemplo n.º 43
0
 def test_no_match(self, keywords):
     assert Keyword.match("nope") == 'No Match'
Ejemplo n.º 44
0
 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)
Ejemplo n.º 45
0
 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)
Ejemplo n.º 46
0
 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)
Ejemplo n.º 47
0
 def test_lookup_colour_test(self, keywords):
     assert Keyword.lookup_colour('test') == '#098f6b'
Ejemplo n.º 48
0
 def test_get_log_link_str(self, keywords):
     assert Keyword.get_log_link('test_no_link') == '#'
Ejemplo n.º 49
0
 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'
Ejemplo n.º 50
0
 def test_get_log_link_keyword(self, keywords):
     assert Keyword.get_log_link(
         keywords['test']) == '/keyword/responses/test/'