예제 #1
0
def get_the_joke(match):
    language  = get_group_from_match(match, 'language', 'english')
    category  = get_group_from_match(match, 'category', 'neutral')
    try:
        message = get_joke(language=lang_codes[language], category=category)
    except:
        message = 'No {} {} jokes found.'.format(language, category)

    return message
예제 #2
0
def on_vote_poll(e):
    poll_code  = get_group_from_match(e.match, 'poll_code', '')
    vote_value = get_group_from_match(e.match, 'vote_value', '+1')
    im         = e.implant

    try:
        im.vote_rounds.insert_vote(poll_code, e.user.id, vote_value)
    except Exception as exc:
        #im.add_memo(e.user.name, reply_text=str(exc))
        log.exception('Error `on_vote_poll`.')
        return str(exc)
    else:
        return 'Added vote by {}: `{}` for poll {}.'.format(e.user.name, vote_value, poll_code)
 def save_poll_timestamp(self, poll_match, timestamp):
     poll_code = get_group_from_match(poll_match, 'poll_code', '')
     try:
         self.vote_rounds.set_round_timestamp(poll_code, timestamp)
     except Exception as exc:
         #im.add_memo(e.user.name, reply_text=str(exc))
         #log.exception('Error `on_vote_poll`.')
         return str(exc)
     else:
         log.debug('Added {} to poll {}.'.format(timestamp, poll_code))
         return True
예제 #4
0
def on_request_results(e):
    im = e.implant
    poll_code = get_group_from_match(e.match, 'poll_code', '')
    if not poll_code:
        return 'No voting round code given.'

    try:
        poll    = im.vote_rounds.find_vote_round (poll_code)
        counter = im.vote_rounds.get_round_result(poll_code)
    except Exception as exc:
        return str(exc)
    else:
        reply_text = []
        reply_text.append('Results for poll on `{}` with code `{}` '.format(poll['topic'], poll['code']))
        reply_text.append(str(counter))
        return '\n'.join(reply_text)
def on_request_results(e):
    im = e.implant
    poll_code = get_group_from_match(e.match, 'poll_code', '')
    if not poll_code:
        return 'No voting round code given.'

    try:
        poll    = im.vote_rounds.find_vote_round (poll_code)
        counter = im.vote_rounds.get_round_result(poll_code)
    except Exception as exc:
        return str(exc)
    else:
        reply_text = []
        reply_text.append('Results for poll `{}`:'.format(poll['code']))
        reply_text.append('*{}*'.format(poll['topic']))
        reply_text.append('*Positives:* {}'.format(counter[1]))
        reply_text.append('*Absent:* {}'.format(counter[0]))
        reply_text.append('*Negatives:* {}'.format(counter[-1]))
        #reply_text.append(str(counter))
        return '\n'.join(reply_text)
예제 #6
0
    def match_event(self, state, msg):
        event = None
        match = None

        if not state.isstate('idle') and msg == 'cancel':
            return 'cancel', None

        if state.isstate('idle'):
            tran_rgx = re.compile(EP_Regex.get_pattern(self._used_regexes[0]))
            vote_rgx = re.compile(EP_Regex.get_pattern(self._used_regexes[1]))

            # '(?P<action>\bcreate \b|\bclose \b|\bshow (?:\bresults of \b)?\b)' \
            match = tran_rgx.search(msg)
            if match:
                action    = get_group_from_match(match, 'action',    '').strip()
                object    = get_group_from_match(match, 'object',    '').strip()
                poll_code = get_group_from_match(match, 'poll_code', '').strip()

                if action.startswith('show') and (action.endswith('result') or action.endswith('results')):
                    if 'poll' in object or 'voting' in object:
                        return 'request_results', match

                elif action.startswith('show'):
                    if 'poll' in object or 'voting' in object:
                        if 'open' in object:
                            return 'request_open_polls', match
                        else:
                            return 'request_all_polls', match

                elif action.startswith('create'):
                    if 'poll' in object or 'voting' in object:
                        return 'open_poll', match

                elif action.startswith('close'):
                    if 'poll' in object or 'voting' in object:
                        if poll_code:
                            return 'close_poll', match

            # match a vote value
            match = vote_rgx.match(msg)
            if match:
                action = get_group_from_match(match, 'action', '')
                if action.startswith('vote'):
                    return 'vote_poll', match

        elif state.isstate('ask_topic'):
            if msg:
                return 'receive_topic', None
            else:
                return 'topic_error', None

        elif state.isstate('ask_code'):
            code_rgx = re.compile(EP_Regex.get_pattern(self._used_regexes[2]))
            match = code_rgx.match(msg)
            if match:
                return 'receive_code', match
            else:
                return 'code_error', None

        elif state.isstate('ask_hours'):
            match = re.compile(r"(?P<hours>[0-9]{1,3})").match(msg)
            if match:
                return 'receive_hours', match
            else:
                return 'hours_error', None

        elif state.isstate('ask_open_confirm'):
            if msg == 'yes' or msg == 'no':
                return 'confirm_open', None
            else:
                return 'confirm_open_error', None

        elif state.isstate('ask_close_confirm'):
            if msg == 'yes' or msg == 'no':
                return 'confirm_close', None
            else:
                return 'confirm_close_error', None

        else:
            event = None
            match = None

        return event, match
예제 #7
0
def on_receive_hours(e):
    hours = get_group_from_match(e.match, 'hours', '')
    e.implant.add_memo(e.user.name, hours=hours)
    hours = e.implant.user_memo_item(e.user.name, 'hours')
    return 'The poll will be open for voting for {} hours_'.format(hours)
예제 #8
0
def on_receive_code(e):
    poll_code = get_group_from_match(e.match, 'poll_code', '')
    e.implant.add_memo(e.user.name, round_code=poll_code)
    poll_code = e.implant.user_memo_item(e.user.name, 'round_code')
    return '_Set the poll code to "{}"_'.format(poll_code)
def on_vote_poll(e):
    poll_code  = get_group_from_match(e.match, 'poll_code', '')
    vote_value = get_group_from_match(e.match, 'vote_value', '+1')
    return e.implant.add_vote(poll_code, e.user.id, vote_value)