예제 #1
0
def AskReviewResponse(bot, message, User):
    """Stage 2 Response. Asks the user for their review/asks if they will reconsider and give a review.
    @param {Bot} bot
    @param {String} message
    @param {Person} User"""
    next_stage = "AskReview"
    next_message = "Sorry I'm not sure I understand what you said."
    if check_for_expected_input(message, positives):
        next_message = "Ok, what is your review?"
        next_stage = "GiveReview"
    elif check_for_expected_input(message, negatives):
        next_message = "Are you sure? By reviewing films I can understand what you like more and suggest more accurate films!"
        next_stage = "ConfirmNoReview"
    
    bot.send_message(User.id, next_message)
    setUserContextAndStage(User.id, contexts['FilmReview'], stages['filmReview'][next_stage])
예제 #2
0
def confirm_genre_response(bot, message, User):
    """Stage 4 Response. Confirming the extracted genre information was correct.
  @param {Bot} bot
  @param {String} message
  @param {Person} User"""
    db_query = db.getQueryInfo(User.id, 2)
    genre = ''
    if db_query:
        genre = [query['Information'] for query in db_query]
    next_question_message = "Sorry I'm not sure I understand."
    if genre:
        next_question_message = next_question_message + " Did you want the film to be a {}".format(
            format_query_info(genre))
    next_stage = 'ConfirmGenre'
    skipFlag = False
    if check_for_expected_input(message, skip):
        skipFlag = True
    if skipFlag or check_for_expected_input(message, positives):
        crew_query_info = db.getQueryInfo(User.id, 3)
        if crew_query_info:
            crewIDs = [crew['Information'] for crew in crew_query_info]
            crew_names = []
            for crewID in crewIDs:
                crew_names.append(db.getCrewByID(crewID)['Name'])
            crew = format_query_info(crew_names)
            if skipFlag:
                next_question_message = "Ok, let's skip choosing a genre. From what you said earlier, you wanted {} to have been involved with the film?".format(
                    crew)
            else:
                next_question_message = "Right, I'll note that down. From what you said earlier, you wanted {} to have been involved with the film?".format(
                    crew)
            next_stage = 'ConfirmCrew'
        else:
            if skipFlag:
                next_question_message = "Ok let's skip choosing a genre. Are there any people you want to have been involved with the film?"
            else:
                next_question_message = "Right, I'll note that down. Are there any people you want to have been involved with the film?"
            next_stage = "AskCrew"
    elif check_for_expected_input(message, negatives):
        next_stage = 'AskGenre'
        next_question_message = "Ok, so are there any genres that you want the film to be of?"
        db.removeQueryInfo(User.id, 2)

    bot.send_message(User.id, next_question_message)
    setUserContextAndStage(User.id, contexts['FilmSuggestion'],
                           stages['filmSuggestion'][next_stage])
예제 #3
0
def registrationHandler(bot, update, User):
    """Handler to figure out if the user's message is acceptable, and if so, diverts the user to the correct
    logic depending on their current stage.
    @param {Bot} bot
    @param {update} update
    @param {Person} User"""
    message = update.message.text
    messageLower = message.lower()
    if check_for_expected_input(message, botAssets.skip):
        skipResponse(bot, update)
        setUserContextAndStage(User.id, contexts['ChitChat'], stages['ChitChat'])
    else:
        genresInfo = getAllGenres()
        genreNames = []
        genreIDs = []
        for individualGenreInfo in genresInfo:
            genreNames.append(individualGenreInfo['Name'])
            genreIDs.append(individualGenreInfo['GenreID'])
        
        if messageLower in genreNames:
            genreID = genreIDs[genreNames.index(messageLower)]
            #TODO Check if the user choices are unique
            #TODO Allow users to change their previous choices in this section
            if User.stage == stages['registrationStages']['FirstGenre']:
                insertFavouriteGenres(User.id, genreID, 0, 0)
                setUserContextAndStage(User.id, User.context, stages['registrationStages']['SecondGenre'])
                askSecondFavouriteGenre(bot, update)
            elif User.stage == stages['registrationStages']['SecondGenre']:
                insertFavouriteGenres(User.id, 0, genreID, 0)
                setUserContextAndStage(User.id, User.context, stages['registrationStages']['ThirdGenre'])
                askThirdFavouriteGenre(bot, update)
            elif User.stage == stages['registrationStages']['ThirdGenre']:
                insertFavouriteGenres(User.id, 0, 0, genreID)
                setUserContextAndStage(User.id, User.context, stages['registrationStages']['Age'])
                askAge(bot, update, User)
        elif User.stage == stages['registrationStages']['Age']:
            if message.isdigit() and int(message) in range(4,100):
                updateUserAge(User.id, int(message))
                setUserContextAndStage(User.id, contexts['ChitChat'], stages['ChitChat'])
                registrationComplete(bot, update)
            else:
                askAgeAgain(bot, update)
        else:
            bot.send_message(chat_id=update.message.chat_id, message="Sorry, I'm not sure I understand.")
예제 #4
0
def ConfirmNoReviewResponse(bot, message, User):
    """Stage 4 Response. Ends the conversation and sets the stage/context back to previous values or,
    if the user changes their mind, takles asks them for their review.
    @param {Bot} bot
    @param {String} message
    @param {Person} User"""
    next_stage = stages['filmReview']["ConfirmNoReview"]
    next_message = "Sorry, I don't understand. Do you want to reconsider and give a review?"
    next_context = contexts['FilmReview']
    if check_for_expected_input(message, positives):
        db.removeSuggestedFilm(User.id)
        next_stage = User.previous_stage
        next_context = User.previous_context
        next_message = "Ok, no problem!"
    elif check_for_expected_input(message, negatives):
        next_message = "Ok, what is your review?"
        next_stage = "GiveReview"
    bot.send_message(User.id, next_message)
    setUserContextAndStage(User.id, next_context, next_stage)
예제 #5
0
def ask_film_response(bot, message, User):
    """Stage 3 Response. Asking if the user wants the suggested film to be similar to
  another film.
  @param {Bot} bot
  @param {String} message
  @param {Person} User"""
    skipFlag = False
    next_stage = 'AskFilm'
    next_question_message = "Sorry I don't understand. Do you want the film to be similar to another film you've seen?"
    if check_for_expected_input(message, skip):
        skipFlag = True
    if skipFlag or check_for_expected_input(message, negatives):
        genres_query_info = db.getQueryInfo(User.id, 2)
        if genres_query_info:
            genres = format_query_info(
                [genre['Information'] for genre in genres_query_info])
            next_question_message = "Ok. So you want a film that is a {}?".format(
                genres)
            next_stage = 'ConfirmGenre'
        else:
            next_question_message = "Ok, Did you want the film to have any specific genres?"
            next_stage = 'AskGenre'
    elif check_for_expected_input(message, positives):
        next_question_message = "Ok, what film do you want it to be similar to?"
    else:
        films = extract_film(User.id, message)
        #In case the user just sends the film title and nothing else
        preprocessed_message = preprocess_text(message)
        db_query = search_for_film_in_db(preprocessed_message)
        if db_query:
            film = (db_query['FilmID'], db_query['Title'])
            if film not in films:
                films.append(film)
                db.insertQueryInformation(User.id, film[0], 1)
        if films:
            filmTitles = format_query_info([film[1] for film in films])
            next_question_message = "So you want a film similar to {}?".format(
                filmTitles)
            next_stage = 'ConfirmFilm'

    bot.send_message(User.id, next_question_message)
    setUserContextAndStage(User.id, contexts['FilmSuggestion'],
                           stages['filmSuggestion'][next_stage])
예제 #6
0
def AskIfWatchedResponse(bot, message, User):
    """Stage 1 Response. Asks the user if they will review the film.
    @param {Bot} bot
    @param {String} message
    @param {Person} User"""
    next_stage = 'AskIfWatched'
    next_message = ""
    next_context = "FilmReview"
    if check_for_expected_input(message, positives):
        next_message = "Excellent, would you like to review it for me?"
        next_stage = "AskReview"
    elif check_for_expected_input(message, negatives):
        next_message = "Ok, I hope you will watch it soon!"
        next_context = User.previous_context
        next_stage = User.previous_stage
    else:
        filmTitle = db.getFilmByID(User.suggested_film)['Title']
        next_message = "Sorry I didn't understand, have you seen {} yet?".format(filmTitle)
    bot.send_message(User.id, next_message)
    setUserContextAndStage(User.id, contexts[next_context], stages[next_context[0].lower() + next_context[1:]][next_stage])
예제 #7
0
def ask_genre_response(bot, message, User):
    """Stage 5 Response. Asking the user if they want the suggested film to be of a certain genre.
  @param {Bot} bot
  @param {String} message
  @param {Person} User"""
    skipFlag = False
    next_stage = 'AskGenre'
    next_question_message = "Sorry I'm not sure what you meant, were there any genres you had in mind for the film I will suggest?"
    if check_for_expected_input(message, skip):
        skipFlag = True
    if skipFlag or check_for_expected_input(message, negatives):
        crew_query_info = db.getQueryInfo(User.id, 3)
        if crew_query_info:
            crewIDs = [crew['Information'] for crew in crew_query_info]
            crew_names = []
            for crewID in crewIDs:
                crew_names.append(db.getCrewByID(crewID)['Name'])
            crew = format_query_info(crew_names)
            next_question_message = "ok so you want a film where {} was involved?".format(
                crew)
            next_stage = 'ConfirmCrew'
        else:
            next_question_message = "Ok so do you want a film with specific actors, directors or writers?"
            next_stage = 'AskCrew'
    elif check_for_expected_input(message, positives):
        next_question_message = "Ok, what genres did you have in mind?"
    else:
        genres = extract_genres(User.id, message)
        if genres:
            genres = format_query_info(genres)
            next_question_message = "So you want a film which is a {}?".format(
                genres)
            next_stage = 'ConfirmGenre'

    bot.send_message(User.id, next_question_message)
    setUserContextAndStage(User.id, contexts['FilmSuggestion'],
                           stages['filmSuggestion'][next_stage])
예제 #8
0
def ScoreFilmResponse(bot, message, User):
    """Stage 5 Response. Checks whether the user entered a score between 1-5, and if so,
    stores it in the database.
    @param {Bot} bot
    @param {String} message
    @param {Person} User"""
    next_stage = stages['filmReview']['ScoreFilm']
    next_message = "Sorry I'm not sure I understand. Could you enter a score for the film out of 5? 1 being poor and 5 being excellent."
    next_context = contexts['FilmReview']
    try:
        score = round(float(message))
        if score < 1 or score > 5:
            raise Exception("Score not between 1 and 5")
        liked = db.getReview(User.id, User.suggested_film)['Pos']
        db.insertUserRating(User.id, User.suggested_film, liked, score)
        db.removeSuggestedFilm(User.id)
        next_stage = User.previous_stage
        next_context = User.previous_context
        next_message = "Excellent! Thank you for rating the film."
        bot.send_message(User.id, next_message, reply_markup=telegram.ReplyKeyboardRemove())
    except Exception as e:
        print("Error rounding user rating", str(e))
        bot.send_message(User.id, next_message)
    setUserContextAndStage(User.id, next_context, next_stage)
예제 #9
0
def ask_crew_response(bot, message, User):
    """Stage 7 Response. Asking the user if they want the suggested film to include certain
  actors, actresses, directors or writers.
  @param {Bot} bot
  @param {String} message
  @param {Person} User"""
    generatedFilm = False
    skipFlag = False
    next_stage = 'AskCrew'
    next_question_message = "Sorry I'm not sure I understood correctly. Did you want any specific people to have been involved with the film?"
    if check_for_expected_input(message, skip):
        skipFlag = True
    if skipFlag or check_for_expected_input(message, negatives):
        generate_film(bot, User, 0)
        db.updateSuggestedFilmIndex(User.id, 0)
        generatedFilm = True
    elif check_for_expected_input(message, positives):
        next_question_message = "Ok, what are the names of these crew members?"
    else:
        crew = extract_crew(User.id, message)
        preprocessed_message = preprocess_text(message)
        db_query = db.getCrewByProcessedName(preprocessed_message)
        if db_query:
            crewInfo = (db_query['CrewID'], db_query['Name'])
            if crewInfo[1] not in crew:
                crew.append(crewInfo[1])
                db.insertQueryInformation(User.id, crew[0], 1)
        if crew:
            crew_names = format_query_info(crew)
            next_question_message = "So you want {} to have worked on the film?".format(
                crew_names)
            next_stage = 'ConfirmCrew'
    if not generatedFilm:
        bot.send_message(User.id, next_question_message)
        setUserContextAndStage(User.id, contexts['FilmSuggestion'],
                               stages['filmSuggestion'][next_stage])