Esempio n. 1
0
def process_raw_message( contributor, payload ):
    """In the event the user sends us a message, 
    handle it here.
    We first need to parse it and normalize it
    The order of the if elif else matters here as higher
    cases will be given higher precident
    """
    processed_payload = payload.lower()
    
    if KEYWORD_HELP in processed_payload:
        BOT_HANDLER_MAPPING[KEYWORD_HELP]( contributor )
    elif KEYWORD_JOIN in processed_payload:
        BOT_HANDLER_MAPPING[KEYWORD_JOIN]( contributor )
    elif KEYWORD_DONE in processed_payload:
        BOT_HANDLER_MAPPING[ KEYWORD_DONE ]( contributor ) 
    elif KEYWORD_LEAVE in processed_payload:
        BOT_HANDLER_MAPPING[KEYWORD_LEAVE]( contributor )
    elif KEYWORD_SKIP in processed_payload:
        BOT_HANDLER_MAPPING[KEYWORD_SKIP]( contributor )
    elif KEYWORD_HISTORY in processed_payload:
        BOT_HANDLER_MAPPING[KEYWORD_HISTORY]( contributor )
    elif KEYWORD_BROWSE in processed_payload:
        BOT_HANDLER_MAPPING[KEYWORD_BROWSE]( contributor )    
    else:
        if contributor.state == WRITING:
            fragment = story_utilities.updateStory( contributor, payload )
            if fragment:
                dispatchers.ctaConfirmEdit( contributor )
            else:
                dispatchers.sendBotMessage(contributor.social_identifier, ":|] Sorry, I didn't get that, it may be too long try again!")
        else:
            # we didn't understand the input so show user all
            # availible options
            BOT_HANDLER_MAPPING[KEYWORD_HELP]( contributor )
Esempio n. 2
0
def handle_leave( contributor ):
    """Handle the case that the user is attempting to leave the story
    """
    # print "handle_leave()"
    story = story_utilities.leaveStory( contributor )
    if story:
        # succesfully left story
        dispatchers.ctaLeftStory( contributor )
    else:
        # there was an issue leaving the story
        dispatchers.sendBotMessage( contributor.social_identifier,  ":|] It looks like you are not working on a story at the moment." )
        dispatchers.ctaOptionsMenu( contributor )
Esempio n. 3
0
def handle_skip( contributor ):
    """Handles the case of a user skipping a pormpt    
    A skip, is a participant leaving a story
    """
    # print "handle_skip()"
    story_id = story_utilities.leaveStory( contributor )
 
    if story_id:
        # succesfully left story
        dispatchers.sendBotMessage(contributor.social_identifier, ":|] Ok, let's try again")
        handle_join(contributor, ignore_story_ids=[story_id])
    else:
        # there was an issue leaving the story
        dispatchers.sendBotMessage( contributor.social_identifier,  ":|] It looks like you are not working on a story at the moment." )
        dispatchers.ctaOptionsMenu( contributor )
Esempio n. 4
0
def handle_browse( contributor ):
    """Handle the case that the user is attempting to read a random story 
    """
    # print "handle_browse()"
    # get a random story
    story = Story.objects.filter(complete=True).order_by('?').first()
    if story:
        dispatchers.sendBotMessage(contributor.social_identifier,  ":|] Here is a random story")
        dispatchers.readBackStory( contributor, story )
        dispatchers.sendBotStructuredButtonMessage(contributor.social_identifier,
                                                            ":|] What would you like to do now?",
                                                            [BUTTON_JOIN, BUTTON_BROWSE, BUTTON_OPTIONS])
    else:
        dispatchers.sendBotStructuredButtonMessage(contributor.social_identifier,
                                                            ":|] Well this is embarassing, I can't find any stories",
                                                            [BUTTON_JOIN, BUTTON_OPTIONS])
Esempio n. 5
0
def handle_done( contributor ):
    """handles the case when a user says they are done with their fragment
    """
    # print "handle_done()"
    # get the last fragment the user was working on
    fragment = contributor.get_last_fragment()
    if fragment and story_utilities.markFragmentAsDone( fragment ):
        # fragment is done
        story = fragment.story
        # print story
        if story.is_story_done():
            # print "is done"
            # the story is done, mark it complete
            story.mark_complete()
            # update everyone that story is done
            dispatchers.notifyOnStoryCompletion(story)
        else:
            # print "is not done" 
            # the story is not done, so update everyone about the latest update
            dispatchers.notifyOnStoryUpdate(story)
            # now figure out who the next person is
            next_contributor = story.get_next_contributor()

            if next_contributor and next_contributor.id != contributor.id:
                next_fragment = story.associate_fragment_with_contributor(next_contributor)
                
                if next_fragment:
                    # notify the next contributor it's their turn
                    dispatchers.notifyNextContributor( next_contributor, story )
                else:
                    # the next fragment does not exist
                    print "no more fragments left"
                
            else:
                # the next contributor is the current contributor
                print "cant find a next contributor whose id is not the current contributor"
    
    else:
        # fragment did not exist or was not written
        dispatchers.sendBotMessage( contributor.social_identifier, ":|] Looks like you havn't written anything!" )
        dispatchers.ctaOptionsMenu( contributor )
Esempio n. 6
0
def handle_join( contributor, ignore_story_ids=None ):
    """Join a story
    """
    # send some flare
    dispatchers.flareOnSearch( contributor )
    
    if contributor.is_busy():
        """ the contributor is currently busy so they need to
        finish or leave their current story before starting a new one
        """
        try:
            active_story = Story.objects.get( id=contributor.active_story )
        except Story.DoesNotExist:
            active_story = None
        
        if active_story:
            dispatchers.ctaNewStoryOnBusy( contributor, active_story )
        else:
            dispatchers.sendBotMessage( contributor.social_identifier, ":|] You broke me, sorry!" )
            dispatchers.ctaOptionsMenu( contributor )
        
    else:
        """ the contributor is availible to start/join a new story
        """
        available_story = Story.objects.filter(complete=False) \
                                       .filter(full=False) \
                                       .exclude(contributors__in=[contributor])
        if ignore_story_ids:
            available_story = available_story.exclude(id__in=ignore_story_ids)

        # pick a random story from the set of stories
        available_story = available_story.order_by('?').first()
        
        if available_story:
            # join the story
            story_utilities.joinStory(contributor, available_story)            
        else:
            # create a new one
            story_utilities.createStory(contributor)
Esempio n. 7
0
def handle_undo( contributor ):
    if contributor.state == WRITING:
        fragment = contributor.get_last_fragment()
        
        if fragment and fragment.last_edit:
            f = story_utilities.undoLastEdit( contributor )
            dispatchers.sendBotMessage( contributor.social_identifier, ":|] Undo done, here is what you have so far" )
            dispatchers.readBackFragment( contributor, f )
        else:
            dispatchers.sendBotMessage( contributor.social_identifier, ":|] Sorry, I can't undo this.")
        
        dispatchers.sendBotStructuredButtonMessage(contributor.social_identifier,
                                                   ":|] What would you like to do now?",
                                                   [BUTTON_DONE, {
                                                        "type": "web_url",
                                                        "title": "Read the story",
                                                        "url": settings.BASE_URL + "/stories/" + str(f.story.id)
                                                    }, BUTTON_OPTIONS])
                                                   
    else:
            dispatchers.sendBotStructuredButtonMessage(contributor.social_identifier,
                                                   ":|] It doesn't look like you are writing anything at the moment",
                                                   [BUTTON_JOIN, BUTTON_BROWSE, BUTTON_OPTIONS])