def process_user_schedules(redis_client, recipient_id, metadata, current_mt): user = redis_client.get_user(recipient_id) schedules = user["schedules"] for schedule_type, mt in list(schedules.items()): if schedule_type == "morning_offerings_mt" and mt < current_mt: if mt <= 0 or (current_mt - mt < 3600): # 1 hour limit or skip it send_offerings( recipient_id, metadata["morning_offering_text"].format( user.get("nickname", DEFAULT_NICKNAME)), metadata["morning_offering_image"]) logging.info("Morning offerings for {} - {} is sent!".format( recipient_id, user.get("nickname", DEFAULT_NICKNAME))) next_mt = time_to_next_utc_mt(user["night_time"]) next_mt += metadata.get("night_offering_mt_offset", 0) del user["schedules"][ "morning_offerings_mt"] # Remove current item user["schedules"]["night_offerings_mt"] = next_mt redis_client.set_user(recipient_id, user) elif schedule_type == "night_offerings_mt" and mt < current_mt: if mt <= 0 or (current_mt - mt < 3600): # 1 hour limit or skip it send_offerings( recipient_id, metadata["night_offering_text"].format( user.get("nickname", DEFAULT_NICKNAME)), metadata["night_offering_image"]) logging.info("Night offerings for {} - {} is sent!".format( recipient_id, user.get("nickname", DEFAULT_NICKNAME))) next_mt = time_to_next_utc_mt(user["morning_time"]) next_mt += metadata.get("morning_offering_mt_offset", 0) del user["schedules"]["night_offerings_mt"] # Remove current item user["schedules"]["morning_offerings_mt"] = next_mt redis_client.set_user(recipient_id, user) elif user.get( "japanese_status", "" ) == "subscribed" and schedule_type == "japanese_lesson_mt" and mt < current_mt: level = user["kanji_level"].lower() try: message = get_japanese_message( metadata["kanji_{}".format(level)], metadata["vocabulary"]) send_text_message(ACCESS_TOKEN, recipient_id, message, passive=True) except Exception: pass while user["schedules"]["japanese_lesson_mt"] < int(time.time()): user["schedules"]["japanese_lesson_mt"] += 86400 redis_client.set_user(recipient_id, user) logging.info("Japanese scheduler for {} - {} is executed!".format( recipient_id, user.get("nickname", DEFAULT_NICKNAME)))
def process_user_schedules(redis_client, recipient_id, metadata, current_mt): user = redis_client.get_user(recipient_id) schedules = user["schedules"] for schedule_type, mt in schedules.items(): if schedule_type == "morning_offerings_mt" and mt < current_mt: send_offerings( recipient_id, metadata["morning_offering_text"].format( user.get("nickname", DEFAULT_NICKNAME)), metadata["morning_offering_image"]) next_mt = time_to_next_utc_mt(user["night_time"]) next_mt += metadata.get("night_offering_mt_offset", 0) del user["schedules"][ "morning_offerings_mt"] # Remove current item user["schedules"]["night_offerings_mt"] = next_mt redis_client.set_user(recipient_id, user) logging.info("Morning offerings for {} - {} is sent!".format( recipient_id, user.get("nickname", DEFAULT_NICKNAME))) elif schedule_type == "night_offerings_mt" and mt < current_mt: send_offerings( recipient_id, metadata["night_offering_text"].format( user.get("nickname", DEFAULT_NICKNAME)), metadata["night_offering_image"]) next_mt = time_to_next_utc_mt(user["morning_time"]) next_mt += metadata.get("morning_offering_mt_offset", 0) del user["schedules"]["night_offerings_mt"] # Remove current item user["schedules"]["morning_offerings_mt"] = next_mt redis_client.set_user(recipient_id, user) logging.info("Night offerings for {} - {} is sent!".format( recipient_id, user.get("nickname", DEFAULT_NICKNAME))) elif schedule_type == "japanese_lesson_mt" and mt < current_mt: level = user["kanji_level"].lower() message = get_japanese_message(metadata["kanji_{}".format(level)], metadata["vocabulary"]) bot.send_text_message(recipient_id, message) user["schedules"]["japanese_lesson_mt"] += 86400 redis_client.set_user(recipient_id, user) logging.info("Japanese scheduler for {} - {} is executed!".format( recipient_id, user.get("nickname", DEFAULT_NICKNAME)))
def process_subscribe_japanese(redis_client, recipient_id): user = redis_client.get_user(recipient_id) if user["japanese_status"] == "subscribed": return "You have been subscribed to Japanese lesson, {}!".format( user.get("nickname", DEFAULT_NICKNAME)) # Update DB information user["japanese_status"] = "subscribed" user["schedules"]["japanese_lesson_mt"] = time_to_next_utc_mt( Constants.DEFAULT_JAPANESE_TIME) redis_client.set_user(recipient_id, user) # Ask for user's preference message = "Thanks for subscribing to Maid-chan Japanese lessons <3\n" redis_client.set_active_question(recipient_id, 3) message += Constants.QUESTIONS[3].format( user.get("nickname", DEFAULT_NICKNAME)) return message
def process_night_question(redis_client, recipient_id, query): user = redis_client.get_user(recipient_id) match = re.search(r'^([01]?\d|2[0-3]):([0-5]?\d)$', query) if not match: user["night_time"] = Constants.DEFAULT_NIGHT_TIME message = "Since I couldn't understand your message, " message += "I set your night time to {} UTC+9, sorry :(\n".format( Constants.DEFAULT_NIGHT_TIME) else: user["night_time"] = match.group(0) message = "Thank you for answering Maid-chan question!\n" metadata = redis_client.get_schedules() next_mt = time_to_next_utc_mt(user["night_time"]) next_mt += metadata.get("night_offering_mt_offset", 0) morning_mt = user["schedules"].get("morning_offerings_mt") if not morning_mt or next_mt < morning_mt: if morning_mt: # Replace del user["schedules"]["morning_offerings_mt"] user["schedules"]["night_offerings_mt"] = next_mt redis_client.set_user(recipient_id, user) message += "Your information for my offerings has been updated <3" return message
def process_morning_question(redis_client, recipient_id, query): user = redis_client.get_user(recipient_id) match = re.search(r'^([01]?\d|2[0-3]):([0-5]?\d)$', query) if not match: user["morning_time"] = Constants.DEFAULT_MORNING_TIME message = "Since I couldn't understand your message, " message += "I set your morning time to {} UTC+9, sorry :(\n".format( Constants.DEFAULT_MORNING_TIME) else: user["morning_time"] = match.group(0) message = "Thank you!\n" metadata = redis_client.get_schedules() next_mt = time_to_next_utc_mt(user["morning_time"]) next_mt += metadata.get("morning_offering_mt_offset", 0) night_mt = user["schedules"].get("night_offerings_mt") if not night_mt or next_mt < night_mt: if night_mt: # Replace del user["schedules"]["night_offerings_mt"] user["schedules"]["morning_offerings_mt"] = next_mt redis_client.set_user(recipient_id, user) redis_client.set_active_question(recipient_id, 2) message += Constants.QUESTIONS[2].format( user.get("nickname", DEFAULT_NICKNAME)) return message