Example #1
0
def add_new_team_to_db(highlight):
    if not football_team_manager.has_football_team(highlight.team1):
        new_football_registration_manager.add_football_registration(
            highlight.team1, highlight.source)

    if not football_team_manager.has_football_team(highlight.team2):
        new_football_registration_manager.add_football_registration(
            highlight.team2, highlight.source)
Example #2
0
def get_highlights_for_team(fb_id, team, highlight_count=10):
    highlights = latest_highlight_manager.get_highlights_for_team(team)

    if highlights == []:
        # Case no highlight found for the team
        return create_quick_text_reply_message(
            NO_HIGHLIGHTS_MESSAGE,
            [SEARCH_AGAIN_HIGHLIGHTS_BUTTON, HELP_BUTTON, CANCEL_BUTTON])

    if not highlights:
        # Case no team name matched
        similar_team_names = football_team_manager.similar_football_team_names(
            team)

        # Register wrong search
        new_football_registration_manager.add_football_registration(
            team, 'user')

        # Check if name of team was not properly written
        if len(similar_team_names) == 0:
            return create_quick_text_reply_message(
                NO_MATCH_FOUND,
                [SEARCH_AGAIN_HIGHLIGHTS_BUTTON, HELP_BUTTON, CANCEL_BUTTON])

        elif len(similar_team_names) == 1:
            # Case where only one team is similar, so send the highlights for this team
            # -> error correction as user might have done a typo
            team = similar_team_names[0]
            highlights = latest_highlight_manager.get_highlights_for_team(team)

        elif len(similar_team_names) >= 2:
            similar_team_names = [
                team_name.title() for team_name in similar_team_names
            ]
            return create_quick_text_reply_message(
                NO_MATCH_FOUND_TEAM_RECOMMENDATION,
                similar_team_names[:9] + [HELP_BUTTON, CANCEL_BUTTON])

    # Eliminate duplicates
    highlights = latest_highlight_manager.get_unique_highlights(highlights)

    # Order highlights by date and take the first 10
    highlights = sorted(highlights,
                        key=lambda h: h.get_parsed_time_since_added(),
                        reverse=True)[:highlight_count]

    # Check if user has see result activated
    see_result_setting = user_manager.get_see_result_setting(fb_id)

    return create_generic_attachment(
        highlights_to_json(fb_id, highlights, see_result=see_result_setting))
Example #3
0
    def post(self, request, *args, **kwargs):

        # Converts the text payload into a python dictionary
        incoming_message = json.loads(request.body.decode('utf-8'))

        logger.log("Message received: " + str(incoming_message))

        response_msg = []

        for entry in incoming_message['entry']:

            for message in entry['messaging']:

                sender_id = message['sender'].get('id')
                HighlightsBotView.LATEST_SENDER_ID = sender_id

                user_manager.increment_user_message_count(sender_id)

                logger.log_for_user("Message received: " + str(message), sender_id)

                # Events
                if 'message' in message:

                    text = message['message'].get('text') if message['message'].get('text') else ''
                    message = language.remove_accents(text.lower())

                    # Do not respond in those cases
                    if 'no' == message or 'nothing' == message or 'ok' == message or 'shut up' in message or message == '':
                        continue

                    # Send typing event - so user is aware received message
                    messenger_manager.send_typing(sender_id)

                    # Special replies
                    # TODO: remove at some point
                    if message == 'add competition ' + EMOJI_TROPHY:
                        logger.log("ADD COMPETITION")

                        context_manager.update_context(sender_id, ContextType.ADDING_REGISTRATION)

                        suggestions_override = ['champions league', 'ligue 1', 'premier league', 'europa league',
                                                'fa cup', 'serie a', 'world cup', 'bundesliga', 'friendly match']

                        response_msg.append(
                            messenger_manager.send_add_registration_message(sender_id,
                                                                            suggestions_override=suggestions_override)
                        )

                    # Special replies
                    # TODO: remove at some point
                    elif message == 'no thanks ' + EMOJI_CROSS:
                        response_msg.append(
                            messenger_manager.send_facebook_message(
                                sender_id, messenger_manager.create_message("Another time! " + EMOJI_SMILE))
                        )

                    # Cancel quick reply
                    elif 'cancel' in message:
                        logger.log("CANCEL")

                        context_manager.set_default_context(sender_id)
                        response_msg.append(
                            messenger_manager.send_cancel_message(sender_id)
                        )

                    # Done quick reply
                    elif 'done' in message:
                        logger.log("DONE")

                        context_manager.set_default_context(sender_id)
                        response_msg.append(
                            messenger_manager.send_done_message(sender_id)
                        )

                    # HELP
                    elif 'help' in message:
                        logger.log("HELP")

                        context_manager.set_default_context(sender_id)
                        response_msg.append(
                            messenger_manager.send_help_message(sender_id)
                        )

                    elif accepted_messages(message, ['thank you', 'thanks', 'cheers', 'merci', 'cimer',
                                                     'good job', 'good bot']):
                        logger.log("THANK YOU MESSAGE")

                        context_manager.set_default_context(sender_id)
                        response_msg.append(
                            messenger_manager.send_thank_you_message(sender_id)
                        )

                    # TUTORIAL CONTEXT
                    # FIXME: duplication between tutorial and registration team
                    elif context_manager.is_tutorial_context(sender_id):
                        logger.log("TUTORIAL ADD REGISTRATION")

                        registration_to_add = message

                        # Check if team exists, make a recommendation if no teams
                        if registration_to_add == 'other':

                            response_msg.append(
                                messenger_manager.send_getting_started_message_2(sender_id)
                            )

                        elif football_team_manager.has_football_team(registration_to_add):
                            # Does team exist check

                            registration_team_manager.add_team(sender_id, registration_to_add)

                            response_msg.append(
                                messenger_manager.send_tutorial_message(sender_id, text)
                            )

                            response_msg.append(
                                messenger_manager.send_tutorial_highlight(sender_id, registration_to_add)
                            )

                            response_msg.append(
                                view_message_helper.send_subscriptions_settings(sender_id)
                            )

                        elif football_team_manager.similar_football_team_names(registration_to_add):
                            # Team recommendation

                            # Register wrong search
                            new_football_registration_manager.add_football_registration(registration_to_add, 'user')

                            # Format recommendation names
                            recommendations = football_team_manager.similar_football_team_names(registration_to_add)
                            recommendations = [recommendation.title() for recommendation in recommendations]

                            response_msg.append(
                                messenger_manager.send_recommended_team_tutorial_message(sender_id, recommendations)
                            )

                        else:
                            # No team or recommendation found

                            # Register wrong search
                            new_football_registration_manager.add_football_registration(registration_to_add, 'user')

                            response_msg.append(
                                messenger_manager.send_team_not_found_tutorial_message(sender_id)
                            )

                    # SEE RESULT CHANGE SETTING
                    elif context_manager.is_see_result_setting_context(sender_id):
                        logger.log("SEE RESULT CHANGE SETTING")

                        if text in [SHOW_BUTTON, HIDE_BUTTON]:
                            user_manager.set_see_result_setting(sender_id, text == SHOW_BUTTON)

                            response_msg.append(
                                messenger_manager.send_setting_changed(sender_id)
                            )

                            context_manager.set_default_context(sender_id)

                        else:
                            response_msg.append(
                                messenger_manager.send_setting_invalid(sender_id)
                            )

                            response_msg.append(
                                messenger_manager.send_see_result_setting(sender_id)
                            )

                    # SUBSCRIPTION SETTING
                    elif accepted_messages(message, ['subscription', 'teams', 'subscribe', 'notification']):
                        logger.log("SUBSCRIPTION SETTING")

                        response_msg.append(
                            view_message_helper.send_subscriptions_settings(sender_id)
                        )

                    # ADD REGISTRATION SETTING
                    elif accepted_messages(message, ['add']) and context_manager.is_notifications_setting_context(sender_id):
                        logger.log("ADD REGISTRATION SETTING")

                        context_manager.update_context(sender_id, ContextType.ADDING_REGISTRATION)

                        response_msg.append(
                            messenger_manager.send_add_registration_message(sender_id)
                        )

                    # REMOVE REGISTRATION SETTING
                    elif accepted_messages(message, ['remove']) and context_manager.is_notifications_setting_context(sender_id):
                        logger.log("REMOVE REGISTRATION SETTING")

                        context_manager.update_context(sender_id, ContextType.REMOVE_REGISTRATION)

                        registrations = view_message_helper.get_registrations_formatted(sender_id)

                        response_msg.append(
                            messenger_manager.send_delete_registration_message(sender_id, registrations)
                        )

                    # ADDING REGISTRATION
                    # FIXME: duplication between tutorial and registration
                    elif context_manager.is_adding_registration_context(sender_id) \
                            or context_manager.is_notifications_setting_context(sender_id):
                        logger.log("ADDING REGISTRATION")

                        registration_to_add = message

                        # Check if registration exists, make a recommendation if no registration
                        if registration_to_add == 'other' or registration_to_add == 'try again':
                            context_manager.update_context(sender_id, ContextType.ADDING_REGISTRATION)

                            response_msg.append(
                                messenger_manager.send_add_registration_message(sender_id)
                            )

                        elif football_team_manager.has_football_team(registration_to_add):
                            # Does team exist check
                            registration_team_manager.add_team(sender_id, registration_to_add)

                            response_msg.append(
                                messenger_manager.send_registration_added_message(sender_id, text)
                            )

                            response_msg.append(
                                view_message_helper.send_subscriptions_settings(sender_id)
                            )

                        elif football_competition_manager.has_football_competition(registration_to_add):
                            # Does competition exist check
                            registration_competition_manager.add_competition(sender_id, registration_to_add)

                            response_msg.append(
                                messenger_manager.send_registration_added_message(sender_id, text)
                            )

                            response_msg.append(
                                view_message_helper.send_subscriptions_settings(sender_id)
                            )

                        elif football_team_manager.similar_football_team_names(registration_to_add) or \
                                football_competition_manager.similar_football_competition_names(registration_to_add):
                            # Registration recommendation
                            context_manager.update_context(sender_id, ContextType.ADDING_REGISTRATION)

                            # Register wrong search
                            new_football_registration_manager.add_football_registration(registration_to_add, 'user')

                            recommendations = football_team_manager.similar_football_team_names(registration_to_add)\
                                              + football_competition_manager.similar_football_competition_names(registration_to_add)

                            # Format recommendation names
                            recommendations = [recommendation.title() for recommendation in recommendations]

                            response_msg.append(
                                messenger_manager.send_recommended_registration_message(sender_id, recommendations)
                            )

                        else:
                            # No registration recommendation found
                            context_manager.update_context(sender_id, ContextType.ADDING_REGISTRATION)

                            # Register wrong search
                            new_football_registration_manager.add_football_registration(registration_to_add, 'user')

                            response_msg.append(
                                messenger_manager.send_registration_not_found_message(sender_id)
                            )

                    # REMOVING REGISTRATION
                    elif context_manager.is_deleting_team_context(sender_id):
                        logger.log("REMOVING REGISTRATION")
                        registration_to_delete = message.lower()

                        if football_team_manager.has_football_team(registration_to_delete):
                            # Delete team
                            registration_team_manager.delete_team(sender_id, registration_to_delete)

                            response_msg.append(
                                messenger_manager.send_registration_deleted_message(sender_id, message)
                            )

                            response_msg.append(
                                view_message_helper.send_subscriptions_settings(sender_id)
                            )

                        elif football_competition_manager.has_football_competition(registration_to_delete):
                            # Delete competition
                            registration_competition_manager.delete_competition(sender_id, registration_to_delete)

                            response_msg.append(
                                messenger_manager.send_registration_deleted_message(sender_id, message)
                            )

                            response_msg.append(
                                view_message_helper.send_subscriptions_settings(sender_id)
                            )

                        else:
                            # Registration to delete not found
                            context_manager.update_context(sender_id, ContextType.REMOVE_REGISTRATION)

                            registrations = view_message_helper.get_registrations_formatted(sender_id)

                            response_msg.append(
                                messenger_manager.send_registration_to_delete_not_found_message(sender_id, registrations)
                            )

                    # SEE RESULT SETTING
                    elif accepted_messages(message, ['see result setting', 'spoiler', 'show result', 'hide result',
                                                     'show score', 'hide score']):
                        logger.log("SEE RESULT SETTING")

                        response_msg.append(
                            view_message_helper.send_send_see_result_settings(sender_id)
                        )

                    # SHARE
                    elif accepted_messages(message, ['share', 'send to a friend']):
                        logger.log("SHARE")

                        response_msg.append(
                            messenger_manager.send_share_introduction_message(sender_id)
                        )
                        response_msg.append(
                            messenger_manager.send_share_message(sender_id)
                        )

                    # SEARCH HIGHLIGHT OPTION
                    elif accepted_messages(message, ['search', 'search again']):
                        logger.log("SEARCH HIGHLIGHTS")

                        response_msg.append(
                            view_message_helper.search_highlights(sender_id)
                        )

                    # SEARCHING HIGHLIGHTS
                    elif context_manager.is_searching_highlights_context(sender_id):
                        logger.log("SEARCHING HIGHLIGHTS")

                        response_msg.append(
                            messenger_manager.send_highlight_message_for_team(sender_id, message)
                        )

                if 'postback' in message:
                    postback = message['postback']['payload']

                    if postback == 'get_started':
                        logger.log("GET STARTED POSTBACK")

                        user = user_manager.get_user(sender_id)

                        response_msg.append(
                            messenger_manager.send_getting_started_message(sender_id, user.first_name)
                        )
                        response_msg.append(
                            messenger_manager.send_getting_started_message_2(sender_id)
                        )

                        # Set the user in tutorial context
                        context_manager.update_context(sender_id, ContextType.TUTORIAL_ADD_TEAM)

                    # SEARCH HIGHLIGHT SETTING POSTBACK
                    elif postback == 'search_highlights':
                        logger.log("SEARCH HIGHLIGHTS POSTBACK")

                        response_msg.append(
                            view_message_helper.search_highlights(sender_id)
                        )

                    # SUBSCRIPTION SETTING POSTBACK
                    elif postback == 'my_subscriptions':
                        logger.log("SUBSCRIPTION SETTING POSTBACK")

                        response_msg.append(
                            view_message_helper.send_subscriptions_settings(sender_id)
                        )

                    # SHARE POSTBACK
                    elif postback == 'share':
                        logger.log("SHARE POSTBACK")

                        response_msg.append(
                            messenger_manager.send_share_introduction_message(sender_id)
                        )
                        response_msg.append(
                            messenger_manager.send_share_message(sender_id)
                        )

                    # SEE RESULT SETTING POSTBACK
                    elif postback == 'see_result_setting':
                        logger.log("SEE RESULT SETTING POSTBACK")

                        response_msg.append(
                            view_message_helper.send_send_see_result_settings(sender_id)
                        )

                logger.log_for_user("Message sent: " + str(response_msg), sender_id)
                HighlightsBotView.LATEST_SENDER_ID = 0

        if not settings.DEBUG:
            return HttpResponse()

        else:
            # DEBUG MODE ONLY
            formatted_response = "[" + ", ".join(response_msg) + "]"
            return JsonResponse(formatted_response, safe=False)
Example #4
0
    def post(self, request, *args, **kwargs):

        # Converts the text payload into a python dictionary
        incoming_message = json.loads(request.body.decode('utf-8'))

        logger.info("Message received", extra={
            'incoming_message': incoming_message
        })

        response_msg = []

        for entry in incoming_message['entry']:

            for message in entry['messaging']:

                sender_id = message['sender'].get('id')
                HighlightsBotView.LATEST_SENDER_ID = sender_id

                user = user_manager.get_user(sender_id)
                user_manager.increment_user_message_count(sender_id)

                logger.log_for_user("Message received", sender_id, extra={
                    'full_msg': message
                })

                # Events
                if 'message' in message:

                    text = message['message'].get('text') if message['message'].get('text') else ''
                    message = language.remove_accents(text.lower())

                    logger.log_for_user("Text message received", sender_id, extra={
                        'full_msg': message,
                        'msg_type': 'message'
                    })

                    # Do not respond in those cases
                    if 'no' == message or 'nothing' == message or 'ok' == message or 'shut up' in message or message == '':
                        logger.log_for_user('No response', sender_id)
                        continue

                    # Send typing event - so user is aware received message
                    sender.send_typing(sender_id)

                    # Special replies
                    # TODO: remove at some point
                    if message == EMOJI_TROPHY + ' add nations league':
                        logger.log_for_user("ADD NATIONS LEAGUE", sender_id)

                        context_manager.update_context(sender_id, ContextType.SUBSCRIPTIONS_SETTING)

                        registration_competition_manager.add_competition(sender_id, 'nations league')

                        response_msg.append(
                            manager_response.send_registration_added_message(sender_id, 'Nations League')
                        )

                        response_msg.append(
                            view_message_helper.send_subscriptions_settings(sender_id)
                        )

                    # Special replies
                    # TODO: remove at some point
                    elif message == EMOJI_CROSS + ' no thanks':
                        logger.log_for_user("NO THANKS NATIONS LEAGUE", sender_id)

                        response_msg.append(
                            manager_response.send_facebook_message(
                                sender_id, manager_response.create_message("Another time! " + EMOJI_SMILE))
                        )

                    # Cancel quick reply
                    elif 'cancel' in message:
                        logger.log_for_user("CANCEL", sender_id)

                        context_manager.set_default_context(sender_id)
                        response_msg.append(
                            manager_response.send_cancel_message(sender_id)
                        )

                    # Done quick reply
                    elif 'done' in message:
                        logger.log_for_user("DONE", sender_id)

                        context_manager.set_default_context(sender_id)
                        response_msg.append(
                            manager_response.send_done_message(sender_id)
                        )

                    # HELP
                    elif 'help' in message:
                        logger.log_for_user("HELP", sender_id)

                        context_manager.set_default_context(sender_id)
                        response_msg.append(
                            manager_response.send_help_message(sender_id)
                        )

                    elif accepted_messages(message, ['thank you', 'thanks', 'cheers', 'merci', 'cimer',
                                                     'good job', 'good bot']):
                        logger.log_for_user("THANK YOU MESSAGE", sender_id)

                        context_manager.set_default_context(sender_id)
                        response_msg.append(
                            manager_response.send_thank_you_message(sender_id)
                        )

                    # TUTORIAL CONTEXT
                    elif context_manager.is_tutorial_context(sender_id):
                        logger.log_for_user("TUTORIAL ADD REGISTRATION", sender_id, extra={
                            'action': 'tutorial'
                        })

                        message = message

                        # Check if team exists, make a recommendation if no teams
                        if football_team_manager.has_football_team(message):
                            # Does team exist check

                            registration_team_manager.add_team(sender_id, message)

                            response_msg.append(
                                manager_highlights.send_tutorial_message(sender_id, text)
                            )

                            response_msg.append(
                                manager_highlights.send_highlights_for_team_or_competition(sender_id,
                                                                                           message,
                                                                                           highlight_count=1,
                                                                                           default_teams=['psg', 'barcelona', 'real madrid', 'spain', 'france'])
                            )

                            response_msg.append(
                                view_message_helper.send_subscriptions_settings(sender_id)
                            )

                        elif football_competition_manager.has_football_competition(message):
                            # Does competition exist check

                            registration_competition_manager.add_competition(sender_id, message)

                            response_msg.append(
                                manager_highlights.send_tutorial_message(sender_id, text)
                            )

                            response_msg.append(
                                manager_highlights.send_highlights_for_team_or_competition(sender_id,
                                                                                           message,
                                                                                           highlight_count=1,
                                                                                           default_teams=['psg', 'barcelona', 'real madrid', 'spain', 'france'])
                            )

                            response_msg.append(
                                view_message_helper.send_subscriptions_settings(sender_id)
                            )

                        elif football_team_manager.similar_football_team_names(message) \
                            + football_competition_manager.similar_football_competition_names(message):
                            # Registration recommendation

                            # Register wrong search
                            new_football_registration_manager.add_football_registration(message, 'user', user)

                            recommendations = football_team_manager.similar_football_team_names(message) \
                                              + football_competition_manager.similar_football_competition_names(message)

                            # Format recommendation names
                            recommendations = [recommendation.title() for recommendation in recommendations]

                            response_msg.append(
                                manager_highlights.send_recommended_team_or_competition_tutorial_message(sender_id, recommendations)
                            )

                        else:
                            # No team or recommendation found

                            # Register wrong search
                            new_football_registration_manager.add_football_registration(message, 'user', user)

                            response_msg.append(
                                manager_highlights.send_team_not_found_tutorial_message(sender_id)
                            )

                    # SEE RESULT CHANGE SETTING
                    elif context_manager.is_see_result_setting_context(sender_id):
                        logger.log_for_user("SEE RESULT CHANGE SETTING", sender_id)

                        if text in [SHOW_BUTTON, HIDE_BUTTON]:
                            user_manager.set_see_result_setting(sender_id, text == SHOW_BUTTON)

                            response_msg.append(
                                manager_response.send_setting_changed(sender_id)
                            )

                            context_manager.set_default_context(sender_id)

                        else:
                            response_msg.append(
                                manager_response.send_setting_invalid(sender_id)
                            )

                            response_msg.append(
                                manager_response.send_see_result_setting(sender_id)
                            )

                    # ADD REGISTRATION SETTING
                    elif accepted_messages(message, ['add']) and context_manager.is_notifications_setting_context(sender_id):
                        logger.log_for_user("ADD REGISTRATION SETTING", sender_id)

                        context_manager.update_context(sender_id, ContextType.ADDING_REGISTRATION)

                        response_msg.append(
                            manager_response.send_add_registration_message(sender_id)
                        )

                    # REMOVE REGISTRATION SETTING
                    elif accepted_messages(message, ['remove']) and context_manager.is_notifications_setting_context(sender_id):
                        logger.log_for_user("REMOVE REGISTRATION SETTING", sender_id)

                        context_manager.update_context(sender_id, ContextType.REMOVE_REGISTRATION)

                        registrations = view_message_helper.get_registrations_formatted(sender_id)

                        response_msg.append(
                            manager_response.send_delete_registration_message(sender_id, registrations)
                        )

                    # ADDING REGISTRATION
                    elif context_manager.is_adding_registration_context(sender_id) \
                            or context_manager.is_notifications_setting_context(sender_id):
                        logger.log_for_user("ADDING REGISTRATION", sender_id)

                        message = message

                        # Check if registration exists, make a recommendation if no registration
                        if message == OTHER_BUTTON.lower() or message == TRY_AGAIN_BUTTON.lower():
                            context_manager.update_context(sender_id, ContextType.ADDING_REGISTRATION)

                            response_msg.append(
                                manager_response.send_add_registration_message(sender_id)
                            )

                        elif accepted_messages(message, [I_M_GOOD_BUTTON.lower(), 'stop', 'done', 'good']):
                            response_msg.append(
                                view_message_helper.send_subscriptions_settings(sender_id)
                            )

                        elif football_team_manager.has_football_team(message):
                            # Does team exist check
                            registration_team_manager.add_team(sender_id, message)

                            response_msg.append(
                                manager_response.send_registration_added_message(sender_id, text)
                            )

                            response_msg.append(
                                manager_response.send_add_registration_message(sender_id)
                            )

                        elif football_competition_manager.has_football_competition(message):
                            # Does competition exist check
                            registration_competition_manager.add_competition(sender_id, message)

                            response_msg.append(
                                manager_response.send_registration_added_message(sender_id, text)
                            )

                            response_msg.append(
                                manager_response.send_add_registration_message(sender_id)
                            )

                        elif football_team_manager.similar_football_team_names(message) or \
                                football_competition_manager.similar_football_competition_names(message):
                            # Registration recommendation
                            context_manager.update_context(sender_id, ContextType.ADDING_REGISTRATION)

                            # Register wrong search
                            new_football_registration_manager.add_football_registration(message, 'user', user)

                            recommendations = football_team_manager.similar_football_team_names(message)\
                                              + football_competition_manager.similar_football_competition_names(message)

                            # Format recommendation names
                            recommendations = [recommendation.title() for recommendation in recommendations]

                            response_msg.append(
                                manager_response.send_recommended_registration_message(sender_id, recommendations)
                            )

                        else:
                            # No registration recommendation found
                            context_manager.update_context(sender_id, ContextType.ADDING_REGISTRATION)

                            # Register wrong search
                            new_football_registration_manager.add_football_registration(message, 'user', user)

                            response_msg.append(
                                manager_response.send_registration_not_found_message(sender_id)
                            )

                    # REMOVING REGISTRATION
                    elif context_manager.is_deleting_team_context(sender_id):
                        logger.log_for_user("REMOVING REGISTRATION", sender_id)
                        registration_to_delete = message.lower()

                        if football_team_manager.has_football_team(registration_to_delete):
                            # Delete team
                            registration_team_manager.delete_team(sender_id, registration_to_delete)

                            response_msg.append(
                                manager_response.send_registration_deleted_message(sender_id, registration_to_delete.title())
                            )

                            response_msg.append(
                                view_message_helper.send_subscriptions_settings(sender_id)
                            )

                        elif football_competition_manager.has_football_competition(registration_to_delete):
                            # Delete competition
                            registration_competition_manager.delete_competition(sender_id, registration_to_delete)

                            response_msg.append(
                                manager_response.send_registration_deleted_message(sender_id, registration_to_delete.title())
                            )

                            response_msg.append(
                                view_message_helper.send_subscriptions_settings(sender_id)
                            )

                        else:
                            # Registration to delete not found
                            context_manager.update_context(sender_id, ContextType.REMOVE_REGISTRATION)

                            registrations = view_message_helper.get_registrations_formatted(sender_id)

                            response_msg.append(
                                manager_response.send_registration_to_delete_not_found_message(sender_id, registrations)
                            )

                    # SUBSCRIPTION SETTING
                    elif accepted_messages(message, ['subscription', 'teams', 'subscribe', 'notification', 'add', 'remove']):
                        logger.log_for_user("SUBSCRIPTION SETTING", sender_id, extra={
                            'action': 'subscriptions'
                        })

                        response_msg.append(
                            view_message_helper.send_subscriptions_settings(sender_id)
                        )

                    # SEE RESULT SETTING
                    elif accepted_messages(message, ['settings', 'see result setting', 'spoiler', 'show result', 'hide result',
                                                     'show score', 'hide score', 'no score']):
                        logger.log_for_user("SEE RESULT SETTING", sender_id, extra={
                            'action': 'settings'
                        })

                        response_msg.append(
                            view_message_helper.send_send_see_result_settings(sender_id)
                        )

                    # SHARE
                    elif accepted_messages(message, ['share', 'send to a friend']):
                        logger.log_for_user("SHARE", sender_id, extra={
                            'action': 'share'
                        })

                        response_msg.append(
                            manager_share.send_share_introduction_message(sender_id)
                        )
                        response_msg.append(
                            manager_share.send_share_message(sender_id)
                        )

                    # SEARCH HIGHLIGHT OPTION
                    elif accepted_messages(message, ['search', 'search again']):
                        logger.log_for_user("SEARCH HIGHLIGHTS", sender_id, extra={
                            'action': 'search'
                        })

                        response_msg.append(
                            view_message_helper.search_highlights(sender_id)
                        )

                    # SEARCHING HIGHLIGHTS
                    elif context_manager.is_searching_highlights_context(sender_id):
                        logger.log_for_user("SEARCHING HIGHLIGHTS", sender_id, extra={
                            'action': 'searching'
                        })

                        team_or_competition = message

                        if football_team_manager.has_football_team(team_or_competition) \
                                or football_competition_manager.has_football_competition(team_or_competition):
                            # Team or competition found

                            response_msg.append(
                                manager_highlights.send_highlights_for_team_or_competition(sender_id, team_or_competition)
                            )

                        elif football_team_manager.similar_football_team_names(team_or_competition) \
                                + football_competition_manager.similar_football_competition_names(team_or_competition):
                            # Recommendation found

                            # Register wrong search
                            new_football_registration_manager.add_football_registration(team_or_competition, 'user', user)

                            recommendations = football_team_manager.similar_football_team_names(team_or_competition) \
                                              + football_competition_manager.similar_football_competition_names(team_or_competition)

                            if len(recommendations) == 1:
                                response_msg.append(
                                    manager_highlights.send_highlights_for_team_or_competition(sender_id, recommendations[0])
                                )

                            else:
                                # Format recommendation names
                                recommendations = [recommendation.title() for recommendation in recommendations]

                                response_msg.append(
                                    manager_highlights.send_recommended_team_or_competition_message(sender_id, recommendations)
                                )

                        else:
                            # No team or recommendation found

                            # Register wrong search
                            new_football_registration_manager.add_football_registration(team_or_competition, 'user', user)

                            response_msg.append(
                                manager_highlights.send_team_not_found_tutorial_message(sender_id)
                            )

                if 'postback' in message:
                    postback = message['postback']['payload']

                    logger.log_for_user("Postback message received", sender_id, extra={
                        'full_msg': message,
                        'msg_type': 'postback'
                    })

                    if postback == 'get_started':
                        logger.log_for_user("GET STARTED POSTBACK", sender_id, extra={
                            'action': 'start'
                        })

                        response_msg.append(
                            manager_response.send_getting_started_message(sender_id, user.first_name)
                        )
                        response_msg.append(
                            manager_response.send_getting_started_message_2(sender_id)
                        )

                        # Set the user in tutorial context
                        context_manager.update_context(sender_id, ContextType.TUTORIAL_ADD_TEAM)

                    # SEARCH HIGHLIGHT SETTING POSTBACK
                    elif postback == 'search_highlights':
                        logger.log_for_user("SEARCH HIGHLIGHTS POSTBACK", sender_id, extra={
                            'action': 'search'
                        })

                        response_msg.append(
                            view_message_helper.search_highlights(sender_id)
                        )

                    # SUBSCRIPTION SETTING POSTBACK
                    elif postback == 'my_subscriptions':
                        logger.log_for_user("SUBSCRIPTION SETTING POSTBACK", sender_id, extra={
                            'action': 'subscriptions'
                        })

                        response_msg.append(
                            view_message_helper.send_subscriptions_settings(sender_id)
                        )

                    # SHARE POSTBACK
                    elif postback == 'share':
                        logger.log_for_user("SHARE POSTBACK", sender_id, extra={
                            'action': 'share'
                        })

                        response_msg.append(
                            manager_share.send_share_introduction_message(sender_id)
                        )
                        response_msg.append(
                            manager_share.send_share_message(sender_id)
                        )

                    # SEE RESULT SETTING POSTBACK
                    elif postback == 'see_result_setting':
                        logger.log_for_user("SEE RESULT SETTING POSTBACK", sender_id, extra={
                            'action': 'settings'
                        })

                        response_msg.append(
                            view_message_helper.send_send_see_result_settings(sender_id)
                        )

                # Log the responses
                for msg in response_msg:
                    logger.log_for_user("Message sent", sender_id, extra={
                        'full_msg': msg,
                        'msg_type': 'response'
                    })
                HighlightsBotView.LATEST_SENDER_ID = 0

        if not settings.DEBUG:
            return HttpResponse()

        else:
            # DEBUG MODE ONLY
            formatted_response = "[" + ", ".join(response_msg) + "]"
            return JsonResponse(formatted_response, safe=False)
Example #5
0
def add_new_competition_to_db(highlight):
    new_football_registration_manager.add_football_registration(highlight.category, highlight.source)