예제 #1
0
def get_slot_value(intent):
    try:
        slots = intent.slots
        slot_sub_reddit = slots["subreddit"]
        print("Getting slot value")
        slot_sub_reddit = slot_sub_reddit.to_dict()
        print("slot_sub_reddit: ", slot_sub_reddit)

        if not slot_sub_reddit["resolutions"]:
            print("NO Resolution")
            return constant.DEFAULT_SUB_REDDIT
        resolution = slot_sub_reddit["resolutions"][
            "resolutions_per_authority"][0]
        print("resolution: ", resolution)

        if resolution["status"]["code"] == "ER_SUCCESS_MATCH":
            print("Slot MATCH")
            return resolution["values"][0]["value"]["name"]
        elif resolution["status"]["code"] == "ER_SUCCESS_NO_MATCH":
            print("NO SLOT MATCH")
            return slot_sub_reddit["value"]
    except Exception as e:
        reddit_api.print_exception_details(e)

    return constant.DEFAULT_SUB_REDDIT
예제 #2
0
def clear_item(session_id):
    if not session_id:
        return
    posts = session_posts.get(session_id, [])
    index = session_index.get(session_id, -1)
    if not posts or index == -1:
        return
    try:
        session_posts.pop(session_id)
        session_index.pop(session_id)
    except Exception as e:
        reddit_api.print_exception_details(e)
예제 #3
0
def get_current_item(session_id):
    if not session_id:
        return data.SORRY_EMPTY_PROMPT
    posts = session_posts.get(session_id, [])
    index = session_index.get(session_id, -1)
    if not posts or index == -1:
        return data.SORRY_EMPTY_PROMPT
    if index < len(posts):
        speech_text = posts[index].title
        session_index[session_id] = index
    else:
        try:
            session_posts.pop(session_id)
            session_index.pop(session_id)
        except Exception as e:
            reddit_api.print_exception_details(e)
        speech_text = data.SORRY_EMPTY_PROMPT
    return speech_text
예제 #4
0
def get_current_post(session_id):
    current_post = Post()
    if not session_id:
        return current_post
    posts = session_posts.get(session_id, [])
    index = session_index.get(session_id, -1)
    if not posts or index == -1:
        return current_post
    if index < len(posts):
        current_post = posts[index]
        session_index[session_id] = index
    else:
        try:
            session_posts.pop(session_id)
            session_index.pop(session_id)
        except Exception as e:
            reddit_api.print_exception_details(e)

    return current_post
예제 #5
0
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response

        intent = handler_input.request_envelope.request.intent
        sub_reddit_name = get_slot_value(intent)
        print("Resolving sub reddit name to ", sub_reddit_name)
        hot_trending_posts_posts = reddit_api.get_subreddit_posts_by_name(
            sub_reddit_name)
        speech_text = data.SORRY_EMPTY_PROMPT
        try:
            session_id = handler_input.request_envelope.session.session_id
            print("session_id here is", session_id)

            for i in range(len(hot_trending_posts_posts)):
                if hot_trending_posts_posts[i].title:
                    speech_text = hot_trending_posts_posts[i].title
                    session_index[session_id] = i
                    session_posts[session_id] = hot_trending_posts_posts
                    break
        except Exception as e:
            reddit_api.print_exception_details(e)

        print("SpeechText ", speech_text)

        # Build GUI
        session_id = handler_input.request_envelope.session.session_id
        current_post = get_current_post(session_id)
        template = get_gui_template(current_post)

        # Build response
        handler_input.response_builder \
            .speak(speech_text) \
            .add_directive(RenderTemplateDirective(template))\
            .set_should_end_session(False)

        return handler_input.response_builder.response