Beispiel #1
0
def is_notifications_setting_context(fb_id):
    return get_user(fb_id).context == ContextType.SUBSCRIPTIONS_SETTING.value
Beispiel #2
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)
Beispiel #3
0
    def test_search_see_result_hidden(self):
        # Given
        user = user_manager.get_user(TEST_USER_ID)
        user.see_result = False
        user.save()

        # When
        json_response = self.send_message(TEST_USER_ID, 'chelsea')

        # Then
        self.assertEqual(json_response, [{
            'recipient': {
                'id': str(TEST_USER_ID)
            },
            "messaging_type": "RESPONSE",
            'message': {
                'attachment': {
                    'payload': {
                        'template_type':
                        'generic',
                        'elements': [{
                            'title':
                            'Chelsea - Barcelona',
                            'subtitle':
                            'Champions League',
                            'image_url':
                            DEFAULT_TEST_IMAGE,
                            'default_action': {
                                'url':
                                settings.BASE_URL + '/highlight/1?user_id=' +
                                str(TEST_USER_ID),
                                'webview_height_ratio':
                                'full',
                                'type':
                                'web_url',
                                'messenger_extensions':
                                'false'
                            },
                            "buttons": [{
                                "type":
                                "web_url",
                                'url':
                                settings.BASE_URL + '/highlight/1?user_id=' +
                                str(TEST_USER_ID),
                                "title":
                                "Short highlights",
                            }, {
                                "type":
                                "web_url",
                                'url':
                                settings.BASE_URL +
                                '/highlight/1/extended?user_id=' +
                                str(TEST_USER_ID),
                                "title":
                                "Extended highlights",
                            }]
                        }]
                    },
                    'type': 'template'
                }
            }
        }])

        # Set back old properties
        user.see_result = True
        user.save()
Beispiel #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)
Beispiel #5
0
    def post(self, request, *args, **kwargs):
        logger.log("Message received: " + str(request.body))

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

        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

                # Send typing event - so user is aware received message
                messenger_manager.send_typing(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())

                    # Cancel quick reply
                    if 'cancel' in message:
                        print("CANCEL")
                        context_manager.update_context(sender_id,
                                                       ContextType.NONE)

                        response_msg.append(
                            messenger_manager.send_cancel_message(sender_id))

                        # Answer with new message what want to do
                        response_msg.append(
                            messenger_manager.
                            send_anything_else_i_can_do_message(sender_id))

                    # Done quick reply
                    elif 'done' in message:
                        print("DONE")
                        context_manager.update_context(sender_id,
                                                       ContextType.NONE)

                        response_msg.append(
                            messenger_manager.send_done_message(sender_id))

                        # Answer with new message what want to do
                        response_msg.append(
                            messenger_manager.
                            send_anything_else_i_can_do_message(sender_id))

                    # HELP
                    elif 'help' in message:
                        print("HELP")
                        context_manager.update_context(sender_id,
                                                       ContextType.NONE)

                        response_msg.append(
                            messenger_manager.send_help_message(sender_id))

                    elif 'thank you' in message or 'thanks' in message or 'cheers' in message or 'merci' in message:
                        print("THANK YOU MESSAGE")
                        context_manager.update_context(sender_id,
                                                       ContextType.NONE)

                        response_msg.append(
                            messenger_manager.send_than_you_message(sender_id))

                    # TUTORIAL CONTEXT
                    # FIXME: duplication between tutorial and adding team
                    elif context_manager.is_tutorial_context(sender_id):
                        print("TUTORIAL ADD TEAM")

                        team_to_add = message

                        # Check if team exists, make a recommendation if no teams
                        if team_to_add == 'other':
                            response_msg.append(
                                messenger_manager.send_add_team_message(
                                    sender_id))

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

                            team_manager.add_team(sender_id, team_to_add)

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

                            context_manager.update_context(
                                sender_id, ContextType.NOTIFICATIONS_SETTING)

                            # Send notification mode FIXME: remove duplication from notification

                            teams = team_manager.get_teams_for_user(sender_id)
                            # Format team names
                            teams = [team.title() for team in teams]

                            response_msg.append(
                                messenger_manager.send_notification_message(
                                    sender_id, teams))

                        elif football_team_manager.similar_football_team_names(
                                team_to_add):
                            # Team recommendation

                            recommendations = football_team_manager.similar_football_team_names(
                                team_to_add)[:messenger_manager.
                                             MAX_QUICK_REPLIES]
                            # Format recommendation names
                            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

                            response_msg.append(
                                messenger_manager.
                                send_team_not_found_tutorial_message(
                                    sender_id))

                    # SEARCH HIGHLIGHT OPTION
                    elif 'search highlights' in message or 'search again' in message:
                        print("SEARCH HIGHLIGHTS")
                        context_manager.update_context(
                            sender_id, ContextType.SEARCH_HIGHLIGHTS)

                        response_msg.append(
                            messenger_manager.send_search_highlights_message(
                                sender_id))

                    # SEARCHING HIGHLIGHTS
                    elif context_manager.is_searching_highlights_context(
                            sender_id):
                        print("SEARCHING HIGHLIGHTS")
                        team_found = messenger_manager.has_highlight_for_team(
                            message)

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

                        if team_found:
                            context_manager.update_context(
                                sender_id, ContextType.NONE)

                            # Answer with new message what want to do
                            response_msg.append(
                                messenger_manager.
                                send_anything_else_i_can_do_message(sender_id))

                        else:
                            context_manager.update_context(
                                sender_id, ContextType.SEARCH_HIGHLIGHTS)

                    # NOTIFICATION SETTING
                    elif 'my teams' in message:
                        print("NOTIFICATION SETTING")
                        context_manager.update_context(
                            sender_id, ContextType.NOTIFICATIONS_SETTING)

                        teams = team_manager.get_teams_for_user(sender_id)
                        # Format team names
                        teams = [team.title() for team in teams]

                        response_msg.append(
                            messenger_manager.send_notification_message(
                                sender_id, teams))

                    # ADD TEAM SETTING
                    elif 'add' in message and context_manager.is_notifications_setting_context(
                            sender_id):
                        print("ADD TEAM SETTING")
                        context_manager.update_context(sender_id,
                                                       ContextType.ADDING_TEAM)

                        response_msg.append(
                            messenger_manager.send_add_team_message(sender_id))

                    # REMOVE TEAM SETTING
                    elif 'remove' in message and context_manager.is_notifications_setting_context(
                            sender_id):
                        print("REMOVE TEAM SETTING")
                        context_manager.update_context(
                            sender_id, ContextType.DELETING_TEAM)

                        teams = team_manager.get_teams_for_user(sender_id)
                        # Format team names
                        teams = [team.title() for team in teams]

                        response_msg.append(
                            messenger_manager.send_delete_team_message(
                                sender_id, teams))

                    # ADDING TEAM
                    # FIXME: duplication between tutorial and adding team
                    elif context_manager.is_adding_team_context(sender_id):
                        print("ADDING TEAM")

                        team_to_add = message

                        # Check if team exists, make a recommendation if no teams
                        if team_to_add == 'other' or team_to_add == 'try again':
                            context_manager.update_context(
                                sender_id, ContextType.ADDING_TEAM)

                            response_msg.append(
                                messenger_manager.send_add_team_message(
                                    sender_id))

                        elif football_team_manager.has_football_team(
                                team_to_add):
                            # Does team exist check
                            context_manager.update_context(
                                sender_id, ContextType.NOTIFICATIONS_SETTING)

                            team_manager.add_team(sender_id, team_to_add)
                            response_msg.append(
                                messenger_manager.send_team_added_message(
                                    sender_id, True, text))

                            teams = team_manager.get_teams_for_user(sender_id)
                            # Format team names
                            teams = [team.title() for team in teams]

                            response_msg.append(
                                messenger_manager.send_notification_message(
                                    sender_id, teams))

                        elif football_team_manager.similar_football_team_names(
                                team_to_add):
                            # Team recommendation
                            context_manager.update_context(
                                sender_id, ContextType.ADDING_TEAM)

                            recommendations = football_team_manager.similar_football_team_names(
                                team_to_add)[:messenger_manager.
                                             MAX_QUICK_REPLIES]
                            # Format recommendation names
                            recommendations = [
                                recommendation.title()
                                for recommendation in recommendations
                            ]

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

                        else:
                            # No team or recommendation found
                            context_manager.update_context(
                                sender_id, ContextType.ADDING_TEAM)

                            response_msg.append(
                                messenger_manager.send_team_not_found_message(
                                    sender_id))

                    # DELETING TEAM
                    elif context_manager.is_deleting_team_context(sender_id):
                        print("DELETING TEAM")
                        team_to_delete = message.lower()

                        if football_team_manager.has_football_team(
                                team_to_delete):
                            # Delete team
                            team_manager.delete_team(sender_id, team_to_delete)
                            response_msg.append(
                                messenger_manager.send_team_deleted_message(
                                    sender_id, message))

                            teams = team_manager.get_teams_for_user(sender_id)
                            # Format team names
                            teams = [team.title() for team in teams]

                            context_manager.update_context(
                                sender_id, ContextType.NOTIFICATIONS_SETTING)
                            response_msg.append(
                                messenger_manager.send_notification_message(
                                    sender_id, teams))

                        else:
                            # Team to delete not found
                            context_manager.update_context(
                                sender_id, ContextType.DELETING_TEAM)

                            teams = team_manager.get_teams_for_user(sender_id)
                            # Format team names
                            teams = [team.title() for team in teams]

                            response_msg.append(
                                messenger_manager.
                                send_team_to_delete_not_found_message(
                                    sender_id, teams))

                    # IF NO MATCH, UNLESS NAME OF A TEAM IS TYPED, ASK WHAT WANT TO DO
                    else:
                        context_manager.update_context(sender_id,
                                                       ContextType.NONE)

                        if football_team_manager.has_football_team(message):
                            # FIXME: duplication with searching highlights
                            print("NO MATCH - SEARCHING HIGHLIGHTS")
                            response_msg.append(
                                messenger_manager.
                                send_highlight_message_for_team(
                                    sender_id, message))

                            # Answer with new message what want to do
                            response_msg.append(
                                messenger_manager.
                                send_anything_else_i_can_do_message(sender_id))

                        else:
                            print("WHAT WANT TO DO")
                            response_msg.append(
                                messenger_manager.
                                send_what_do_you_want_to_do_message(sender_id))

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

                    if postback == 'get_started':
                        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)

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

        if not settings.DEBUG:
            return HttpResponse()

        # For DEBUG MODE only
        formatted_response = "["

        for i in range(len(response_msg)):
            formatted_response += response_msg[i]

            if i != len(response_msg) - 1:
                formatted_response += ", "

        formatted_response += "]"

        return JsonResponse(formatted_response, safe=False)
Beispiel #6
0
def is_tutorial_context(fb_id):
    return get_user(fb_id).context == ContextType.TUTORIAL_ADD_TEAM.value
Beispiel #7
0
def is_searching_highlights_context(fb_id):
    return get_user(fb_id).context == ContextType.SEARCH_HIGHLIGHTS.value
Beispiel #8
0
def is_deleting_team_context(fb_id):
    return get_user(fb_id).context == ContextType.DELETING_TEAM.value
Beispiel #9
0
def is_adding_team_context(fb_id):
    return get_user(fb_id).context == ContextType.ADDING_TEAM.value
Beispiel #10
0
def is_notifications_setting_continue_context(fb_id):
    return get_user(fb_id).context == ContextType.NOTIFICATIONS_SETTING_CONTINUE.value