예제 #1
0
def get_message():
    data = request.get_json()
    print(json.dumps(data))

    if "standby" in data["entry"][0]:
        Event(
            data["entry"][0]["standby"][0]["sender"]["id"], datetime.now(),
            "get_message", "INI", datetime.now(), "New Message from {}".format(
                data["entry"][0]["standby"][0]["sender"]["id"]))
        return "OK", 200

    event = Event(
        data["entry"][0]["messaging"][0]["sender"]["id"], datetime.now(),
        "get_message", "INI", datetime.now(), "New Message from {}".format(
            data["entry"][0]["messaging"][0]["sender"]["id"]))

    # event.update("OK ", datetime.now(), json.dumps(data))
    entry = Entry(**data["entry"][0])
    message = Messaging(**entry.messaging[0])
    if "message" in entry.messaging[0]:
        process_message(message, event)
        event.update("OK ", datetime.now(), "Receive OK!")
        return "OK", 200

    if "postback" in entry.messaging[0]:
        process_postback(message, event)
        event.update("OK ", datetime.now(), "Receive OK!")
        return "OK", 200

    event.update("OK ", datetime.now(), "Receive OK!")
    return "OK", 200
예제 #2
0
def test_get_long_reminders(database, reddit):
    utils.debug_time = utils.parse_datetime_string("2019-01-01 12:00:00")
    user = database.get_or_add_user("Watchful1")
    requested_date = utils.parse_datetime_string("2019-01-01 04:00:00")
    target_date = utils.parse_datetime_string("2019-01-01 04:00:00")
    for i in range(60):
        database.add_reminder(
            Reminder(source=f"https://www.reddit.com/message/messages/XXX{i}",
                     message=f"{i}" * 50,
                     user=user,
                     requested_date=requested_date,
                     target_date=target_date + timedelta(days=1)))

    message = reddit_test.RedditObject(body="MyReminders!", author="Watchful1")
    messages.process_message(message, reddit, database)
    assert len(message.children) == 3
    assert "Click here to delete all your reminders" in message.children[
        0].body
    assert "Click here to delete all your reminders" not in message.children[
        1].body
    assert "Click here to delete all your reminders" not in message.children[
        2].body
    assert "|Source|Message|Date|In|Remove|" in message.children[0].body
    assert "|Source|Message|Date|In|Remove|" in message.children[1].body
    assert "|Source|Message|Date|In|Remove|" in message.children[2].body
    assert "[^(Info)]" not in message.children[0].body
    assert "[^(Info)]" not in message.children[1].body
    assert "[^(Info)]" in message.children[2].body
예제 #3
0
def test_add_recurring_reminder(database, reddit):
    created = utils.datetime_now()
    username = "******"
    keyword = "reminderstring"
    id = reddit_test.random_id()
    message = reddit_test.RedditObject(
        body=f"[{keyword}]\n{static.TRIGGER_RECURRING}! 1 day",
        author=username,
        created=created,
        id=id)

    messages.process_message(message, reddit, database)
    result = message.get_first_child().body

    assert "reminderstring" in result
    assert "and then every `1 day`" in result

    assert "This time has already passed" not in result
    assert "Could not find a time in message" not in result
    assert "Could not parse date" not in result
    assert "Can't use a default for a recurring reminder" not in result
    assert "I got the same date rather than one after it" not in result
    assert "I got a date before that rather than one after it" not in result

    reminders = database.get_all_user_reminders(username)
    assert len(reminders) == 1
    assert reminders[0].user.name == username
    assert reminders[0].message == keyword
    assert reminders[0].source == utils.message_link(id)
    assert reminders[0].requested_date == created
    assert reminders[0].target_date == created + timedelta(hours=24)
    assert reminders[0].id is not None
    assert reminders[0].recurrence == "1 day"
예제 #4
0
def run(reddit):
    inbox = reddit.inbox.unread()

    for message in inbox:
        mark_read = True
        if message.author is None:
            print(f"Message {message.id} is a system notification")
        elif message.author.name == "reddit":
            print(f"Message {message.id} is from reddit, skipping")
        else:
            try:
                messages.process_message(reddit, message)
            except Exception as err:
                mark_read = False
                logging.exception(
                    f"Error processing message: {message.id} : u/{message.author.name}"
                )

        if mark_read:
            try:
                message.mark_read()
            except Exception as err:
                logging.exception(
                    f"Error marking message read: {message.id} : u/{message.author.name}"
                )
예제 #5
0
def test_add_reminder(database, reddit):
    created = utils.datetime_now()
    username = "******"
    keyword = "reminderstring"
    id = utils.random_id()
    message = reddit_test.RedditObject(
        body=f"[{keyword}]\n{static.TRIGGER}! 1 day",
        author=username,
        created=created,
        id=id)

    messages.process_message(message, reddit, database)
    result = message.get_first_child().body

    assert "reminderstring" in result

    assert "This time has already passed" not in result
    assert "Could not find a time in message" not in result
    assert "Could not parse date" not in result

    reminders = database.get_user_reminders(username)
    assert len(reminders) == 1
    assert reminders[0].user == username
    assert reminders[0].message == keyword
    assert reminders[0].source == utils.message_link(id)
    assert reminders[0].requested_date == created
    assert reminders[0].target_date == created + timedelta(hours=24)
    assert reminders[0].db_id is not None
예제 #6
0
def test_remove_all_reminders(database, reddit):
    utils.debug_time = utils.parse_datetime_string("2019-01-01 12:00:00")
    message = reddit_test.RedditObject(body=f"RemoveAll!", author="Watchful1")
    messages.process_message(message, reddit, database)
    assert "Deleted" not in message.get_first_child().body

    reminder1 = Reminder(
        source="https://www.reddit.com/message/messages/XXXXX",
        message="KKKKK",
        user=database.get_or_add_user("Watchful1"),
        requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"),
        target_date=utils.parse_datetime_string("2019-01-04 05:00:00"))
    reminder2 = Reminder(
        source="https://www.reddit.com/message/messages/YYYYY",
        message="FFFFF",
        user=database.get_or_add_user("Watchful1"),
        requested_date=utils.parse_datetime_string("2019-02-02 06:00:00"),
        target_date=utils.parse_datetime_string("2019-02-05 07:00:00"))
    reminder3 = Reminder(
        source="https://www.reddit.com/message/messages/ZZZZZ",
        message="JJJJJ",
        user=database.get_or_add_user("Watchful2"),
        requested_date=utils.parse_datetime_string("2019-03-02 06:00:00"),
        target_date=utils.parse_datetime_string("2019-03-05 07:00:00"))
    database.add_reminder(reminder1)
    database.add_reminder(reminder2)
    database.add_reminder(reminder3)

    message = reddit_test.RedditObject(body=f"RemoveAll!", author="Watchful1")
    messages.process_message(message, reddit, database)
    body = message.get_first_child().body
    assert "Deleted **2** reminders." in body

    assert len(database.get_all_user_reminders("Watchful1")) == 0
    assert len(database.get_all_user_reminders("Watchful2")) == 1
예제 #7
0
def process_unsecuredmsg(addr,packet):
    peer = peer_by_uid(packet[1:25])
    print("Unsec msg incoming")
    if(peer in lan_contacts):
        #Attributes of received message
        Msg = contacts.Message_in(lan_contacts[peer].maincontact,packet[27:],"lan")
        Msg.time_received = time.time()
        Msg.timeout = 255
        Msg.security = 0
        Msg.seqid = struct.unpack("H",packet[25:27])[0]
        print(Msg.seqid)
        messages.process_message(Msg)
    else: print("[BOGUS]: Message received from unknown peer")
예제 #8
0
def test_add_cakeday_exists(database, reddit):
    username = "******"
    created = utils.parse_datetime_string("2015-05-05 15:25:17")
    user = reddit_test.User(username, created.timestamp())
    message = reddit_test.RedditObject(body="Cakeday!", author=user)
    messages.process_message(message, reddit, database)

    message2 = reddit_test.RedditObject(body="Cakeday!", author=user)
    messages.process_message(message2, reddit, database)

    result = message2.get_first_child().body

    assert "It looks like you already have a cakeday reminder set." in result
예제 #9
0
def test_set_clock(database, reddit):
    username = "******"
    message = reddit_test.RedditObject(body="Clock! ", author=username)
    messages.process_message(message, reddit, database)
    result = message.get_last_child().body
    assert "I couldn't find a clock type in your message." in result

    message.body = "Clock! 22"
    messages.process_message(message, reddit, database)
    result = message.get_last_child().body
    assert "22 is not a valid clock type." in result

    message.body = "Clock! 12"
    messages.process_message(message, reddit, database)
    result = message.get_last_child().body
    assert "Updated your clock type to a 12 hour clock" in result
    user = database.get_or_add_user(username)
    assert user.time_format == "12"

    message.body = "Clock! 24"
    messages.process_message(message, reddit, database)
    result = message.get_last_child().body
    assert "Reset your clock type to the default 24 hour clock" in result
    user = database.get_or_add_user(username)
    assert user.time_format is None
예제 #10
0
def test_set_timezone(database, reddit):
    username = "******"
    message = reddit_test.RedditObject(body="Timezone! ", author=username)
    messages.process_message(message, reddit, database)
    result = message.get_last_child().body
    assert "I couldn't find a timezone in your message." in result

    message.body = "Timezone! EST"
    messages.process_message(message, reddit, database)
    result = message.get_last_child().body
    assert "EST is not a valid timezone." in result

    message.body = "Timezone! America/Los_Angeles"
    messages.process_message(message, reddit, database)
    result = message.get_last_child().body
    assert "Updated your timezone to America/Los_Angeles" in result
    user_settings = database.get_settings(username)
    assert user_settings.timezone == "America/Los_Angeles"

    message.body = "Timezone! UTC"
    messages.process_message(message, reddit, database)
    result = message.get_last_child().body
    assert "Reset your timezone to the default" in result
    user_settings = database.get_settings(username)
    assert user_settings.timezone is None
예제 #11
0
def process_unsecuredmsg(addr,packet):
    peer = peer_by_lan_ipport(addr[0],addr[1])
    if(peer == -1):
        logging.warning("Message received for peer not on contact list")
        return 0

    logging.info("LAN: Unencrypted message from " + str(addr[0]) +  ":"+ str(addr[1]))
    if(peer != -1):
        #Attributes of received message
        Msg = contacts.Message_in(peer,packet[3:],"lan")
        Msg.time_received = time.time()
        Msg.timeout = 255
        Msg.security = 0
        Msg.seqid = struct.unpack("H",packet[1:3])[0]
        print(Msg.seqid)
        messages.process_message(Msg)
    else: print("[BOGUS]: Message received from unknown peer")
예제 #12
0
def test_add_cakeday(database, reddit):
    username = "******"
    created = utils.parse_datetime_string("2015-05-05 15:25:17")
    user = reddit_test.User(username, created.timestamp())
    message = reddit_test.RedditObject(body="Cakeday!", author=user)

    utils.debug_time = utils.parse_datetime_string("2019-01-05 12:00:00")
    messages.process_message(message, reddit, database)
    result = message.get_first_child().body

    assert "to remind you of your cakeday" in result

    cakeday = database.get_cakeday(username)
    assert cakeday is not None
    assert cakeday.user == username
    assert cakeday.date_time == utils.parse_datetime_string(
        "2019-05-05 15:25:17")
    assert cakeday.db_id is not None
예제 #13
0
def test_reset_recurring_reminder_limit(database, reddit):
    reminder = Reminder(
        source="https://www.reddit.com/message/messages/XXXXX",
        message="KKKKK",
        user=database.get_or_add_user("Watchful1"),
        requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"),
        target_date=utils.parse_datetime_string("2019-01-05 05:00:00"),
        recurrence="one day")
    database.add_reminder(reminder)

    utils.debug_time = utils.parse_datetime_string("2019-01-05 12:00:00")
    notifications.send_reminders(reddit, database)
    assert database.get_or_add_user("Watchful1").recurring_sent == 1

    message = reddit_test.RedditObject(body="MyReminders!", author="Watchful1")
    messages.process_message(message, reddit, database)

    assert database.get_or_add_user("Watchful1").recurring_sent == 0
예제 #14
0
def test_timezone_reminder_message(database, reddit):
    user = database.get_or_add_user(user_name="Watchful1")
    user.timezone = "America/Los_Angeles"

    created = utils.datetime_now()
    target = created + timedelta(hours=24)
    username = "******"
    message = reddit_test.RedditObject(
        body=
        f"{static.TRIGGER}! {utils.get_datetime_string(utils.datetime_as_timezone(target, user.timezone))}",
        author=username,
        created=created)

    messages.process_message(message, reddit, database)

    reminders = database.get_all_user_reminders(username)
    assert len(reminders) == 1
    assert reminders[0].requested_date == created
    assert reminders[0].target_date == utils.datetime_as_utc(
        pytz.timezone(user.timezone).localize(target.replace(tzinfo=None)))
예제 #15
0
def test_add_cakeday(database, reddit):
    username = "******"
    created = utils.parse_datetime_string("2015-05-05 15:25:17")
    user = reddit_test.User(username, created.timestamp())
    message = reddit_test.RedditObject(body="Cakeday!", author=user)

    utils.debug_time = utils.parse_datetime_string("2019-01-05 12:00:00")
    messages.process_message(message, reddit, database)
    result = message.get_first_child().body

    assert "to remind you of your cakeday" in result

    reminders = database.get_all_user_reminders(username)
    assert len(reminders) == 1
    assert reminders[0].user.name == username
    assert reminders[0].target_date == utils.parse_datetime_string(
        "2019-05-05 15:25:17")
    assert reminders[0].id is not None
    assert reminders[0].recurrence == "1 year"
    assert reminders[0].message == "Happy Cakeday!"
예제 #16
0
def test_delete_comment(database, reddit):
    db_comment = DbComment(thread_id="XXXXX",
                           comment_id="ZZZZZ",
                           reminder_id="YYYYY",
                           user="******",
                           source="www.reddit.com/r/test/comments/XXXXX",
                           current_count=1)
    database.save_comment(db_comment)
    comment = reddit_test.RedditObject(body="Click here for a reminder!",
                                       author=static.ACCOUNT_NAME,
                                       id="ZZZZZ")
    reddit.add_comment(comment, True)

    message = reddit_test.RedditObject(body=f"Delete! SSSSSS",
                                       author="Watchful1")
    messages.process_message(message, reddit, database)
    assert "This comment doesn't exist or was already deleted." in message.get_first_child(
    ).body

    message = reddit_test.RedditObject(body=f"Delete! XXXXX",
                                       author="Watchful2")
    messages.process_message(message, reddit, database)
    assert "It looks like the bot wasn't replying to you." in message.get_first_child(
    ).body

    message = reddit_test.RedditObject(body=f"Delete! XXXXX",
                                       author="Watchful1")
    messages.process_message(message, reddit, database)
    assert "Comment deleted." in message.get_first_child().body
    assert comment.id not in reddit.all_comments
예제 #17
0
def test_remove_cakeday(database, reddit):
    cakeday1 = Cakeday(
        user="******",
        date_time=utils.parse_datetime_string("2015-05-05 15:25:17"))
    cakeday2 = Cakeday(
        user="******",
        date_time=utils.parse_datetime_string("2015-05-05 15:25:17"))
    database.add_cakeday(cakeday1)
    database.add_cakeday(cakeday2)

    message = reddit_test.RedditObject(body=f"Remove! cakeday",
                                       author="Watchful3")
    messages.process_message(message, reddit, database)
    assert "You don't have a cakeday reminder set." in message.get_first_child(
    ).body

    message = reddit_test.RedditObject(body=f"Remove! cakeday",
                                       author="Watchful1")
    messages.process_message(message, reddit, database)
    assert "Cakeday reminder deleted." in message.get_first_child().body
    assert database.get_cakeday("Watchful1") is None
    assert database.get_cakeday("Watchful2") is not None
예제 #18
0
def test_add_reminder_no_date(database, reddit):
    created = utils.datetime_now()
    username = "******"
    id = reddit_test.random_id()
    message = reddit_test.RedditObject(
        body=f"{static.TRIGGER}! \"error test\"",
        author=username,
        created=created,
        id=id)

    messages.process_message(message, reddit, database)
    result = message.get_first_child().body

    assert "This time has already passed" not in result
    assert "Could not find a time in message, defaulting to one day" in result

    reminders = database.get_all_user_reminders(username)
    assert len(reminders) == 1
    assert reminders[0].user.name == username
    assert reminders[0].message == "error test"
    assert reminders[0].source == utils.message_link(id)
    assert reminders[0].requested_date == created
    assert reminders[0].target_date == created + timedelta(hours=24)
    assert reminders[0].id is not None
예제 #19
0
def test_get_reminders(database, reddit):
    utils.debug_time = utils.parse_datetime_string("2019-01-01 12:00:00")
    message = reddit_test.RedditObject(body="MyReminders!", author="Watchful1")
    messages.process_message(message, reddit, database)
    result = message.get_first_child().body
    assert "You don't have any reminders." in result

    reminders = [
        Reminder(
            source="https://www.reddit.com/message/messages/XXXXX",
            message="KKKKK",
            user=database.get_or_add_user("Watchful1"),
            requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"),
            target_date=utils.parse_datetime_string("2019-01-04 05:00:00")),
        Reminder(
            source="https://www.reddit.com/message/messages/YYYYY",
            message="FFFFF",
            user=database.get_or_add_user("Watchful1"),
            requested_date=utils.parse_datetime_string("2019-02-02 06:00:00"),
            target_date=utils.parse_datetime_string("2019-02-05 07:00:00")),
        Reminder(
            source="https://www.reddit.com/message/messages/ZZZZZ",
            message="GGGGG",
            user=database.get_or_add_user("Watchful1"),
            requested_date=utils.parse_datetime_string("2019-02-02 06:00:00"),
            target_date=utils.parse_datetime_string("2019-02-05 07:00:00"),
            recurrence="one day")
    ]
    for reminder in reminders:
        database.add_reminder(reminder)

    message = reddit_test.RedditObject(body="MyReminders!", author="Watchful1")
    messages.process_message(message, reddit, database)
    result = message.get_first_child().body

    assert "Click here to delete all your reminders" in result
    assert "|Source|Message|Date|In|Repeat|Remove|" in result
    assert "|Source|Message|Date|In|Remove|" in result

    assert reminders[0].source in result
    assert reminders[0].message in result
    assert "01-04 05" in result

    assert reminders[1].source in result
    assert reminders[1].message in result
    assert "02-05 07" in result

    assert reminders[2].recurrence in result

    user = database.get_or_add_user(user_name="Watchful1")
    user.timezone = "America/Los_Angeles"
    messages.process_message(message, reddit, database)
    result = message.get_last_child().body
    assert "Your timezone is currently set to: `America/Los_Angeles`" in result
    assert "01-03 21" in result
    assert "02-04 23" in result
예제 #20
0
def test_get_reminders(database, reddit):
    utils.debug_time = utils.parse_datetime_string("2019-01-01 12:00:00")
    message = reddit_test.RedditObject(body="MyReminders!", author="Watchful1")
    messages.process_message(message, reddit, database)
    result = message.get_first_child().body
    assert "You don't have any reminders." in result

    reminder1 = Reminder(
        source="https://www.reddit.com/message/messages/XXXXX",
        message="KKKKK",
        user="******",
        requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"),
        target_date=utils.parse_datetime_string("2019-01-04 05:00:00"))
    reminder2 = Reminder(
        source="https://www.reddit.com/message/messages/YYYYY",
        message="FFFFF",
        user="******",
        requested_date=utils.parse_datetime_string("2019-02-02 06:00:00"),
        target_date=utils.parse_datetime_string("2019-02-05 07:00:00"))
    database.save_reminder(reminder1)
    database.save_reminder(reminder2)
    cakeday = Cakeday(
        user="******",
        date_time=utils.parse_datetime_string("2019-05-05 15:25:17"))
    database.add_cakeday(cakeday)

    message = reddit_test.RedditObject(body="MyReminders!", author="Watchful1")
    messages.process_message(message, reddit, database)
    result = message.get_first_child().body

    assert "Click here to delete all your reminders" in result

    assert "Happy cakeday!" in result

    assert reminder1.source in result
    assert reminder1.message in result
    assert "01-04 05" in result

    assert reminder2.source in result
    assert reminder2.message in result
    assert "02-05 07" in result

    database.save_settings(
        UserSettings(user="******", timezone="America/Los_Angeles"))
    messages.process_message(message, reddit, database)
    result = message.get_last_child().body
    assert "Your timezone is currently set to: `America/Los_Angeles`" in result
    assert "01-03 21" in result
    assert "02-04 23" in result
예제 #21
0
def test_remove_reminder(database, reddit):
    reminder1 = Reminder(
        source="https://www.reddit.com/message/messages/XXXXX",
        message="KKKKK",
        user="******",
        requested_date=utils.parse_datetime_string("2019-01-01 04:00:00"),
        target_date=utils.parse_datetime_string("2019-01-04 05:00:00"))
    reminder2 = Reminder(
        source="https://www.reddit.com/message/messages/YYYYY",
        message="FFFFF",
        user="******",
        requested_date=utils.parse_datetime_string("2019-02-02 06:00:00"),
        target_date=utils.parse_datetime_string("2019-02-05 07:00:00"))
    reminder3 = Reminder(
        source="https://www.reddit.com/message/messages/ZZZZZ",
        message="JJJJJ",
        user="******",
        requested_date=utils.parse_datetime_string("2019-03-02 06:00:00"),
        target_date=utils.parse_datetime_string("2019-03-05 07:00:00"))
    database.save_reminder(reminder1)
    database.save_reminder(reminder2)
    database.save_reminder(reminder3)

    message = reddit_test.RedditObject(body=f"Remove! test",
                                       author="Watchful2")
    messages.process_message(message, reddit, database)
    assert "I couldn't find a reminder id to remove." in message.get_first_child(
    ).body

    message = reddit_test.RedditObject(body=f"Remove! {reminder1.db_id}",
                                       author="Watchful2")
    messages.process_message(message, reddit, database)
    assert "It looks like you don't own this reminder or it doesn't exist." in message.get_first_child(
    ).body

    message = reddit_test.RedditObject(body=f"Remove! {reminder1.db_id}",
                                       author="Watchful1")
    messages.process_message(message, reddit, database)
    assert "Reminder deleted." in message.get_first_child().body

    assert len(database.get_user_reminders("Watchful1")) == 1
    assert len(database.get_user_reminders("Watchful2")) == 1
예제 #22
0
    def process_data(self, data):
        self.dump_data(data, "(I) ")

        common_data = struct_common.parse(data)
        messages.process_message(common_data, self)
예제 #23
0
    def process_data(self, data):
        self.dump_data(data, "(I) ")

        common_data = struct_common.parse(data)
        messages.process_message(common_data, self)