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