예제 #1
0
def test_multiple():
    h.assert_that(
        parse.execute('{foo}, {bar}', parse.ParseContext(dict(foo=2, bar=3))),
        h.equal_to('2, 3'))
예제 #2
0
def bot_old():
    # prevents looping
    if request.form['user_name'] == 'slackbot': return jsonify()
    try:
        log.info('Form is %s' % request.form)
        user_name = request.form['user_name']
        trigger = request.form.get('trigger_word') or ''
        text = request.form['text'][len(trigger):].strip()

        if text.lower().startswith('hello') or text.lower().startswith('hi') or text.lower().startswith('greetings'):
            return jsonify(text='Hello, {}'.format(user_name))

        words = text.split()
        kill_words = 'destroy kill crush annihilate assassinate'.split()

        if len(words) > 0:
          for w in kill_words:
            if words[0].lower() == w:
              target = words[1] if len(words) > 1 else 'EVERYBODY'
              return jsonify(text='Launching ICBMs at {}.'.format(target))

        if 'love?' in text.lower().strip():
          current_love = 'nobody'
          love = Fact.get_by_id('love')
          if love is not None:
              current_love = love.value

          return jsonify(text='I will always love {}!'.format(current_love))

        if 'love me' in text.lower().strip():
          love = Fact.get_by_id('love')
          old_love = 'I will be yours until I die.'

          if love is not None:
            old_love = "{} is dead to me. I'm all about {} now!".format(love.value, user_name)
          else:
            love = Fact(id='love')

          love.value = user_name
          love.put()
          return jsonify(text=old_love)

        for r in Rule.query().iter():
            if not r.trigger_code.strip(): continue

            message = Message(request.form)
            match = re.compile(r.trigger_code).match(message.text)
            if match:
                context = {('group[%d]' % i): v for i, v in enumerate(match.groups())}
                context.update({
                  'text': message.text,
                  'user_name': message.user_name,
                })

                return jsonify({'text': parse.execute(r.action_code, parse.ParseContext(context))})

        return jsonify()
    except Exception as e:
        print "exception"
        if '__test' in request.form or request.form['__test']:
            return jsonify({'text': 'Error: %s' % e.message})
        return jsonify()
예제 #3
0
def test_multiple():
    h.assert_that(parse.execute('{foo}, {bar}', parse.ParseContext(dict(foo=2, bar=3))), h.equal_to('2, 3'))
예제 #4
0
def bot_old():
    # prevents looping
    if request.form['user_name'] == 'slackbot': return jsonify()
    try:
        log.info('Form is %s' % request.form)
        user_name = request.form['user_name']
        trigger = request.form.get('trigger_word') or ''
        text = request.form['text'][len(trigger):].strip()

        if text.lower().startswith('hello') or text.lower().startswith(
                'hi') or text.lower().startswith('greetings'):
            return jsonify(text='Hello, {}'.format(user_name))

        words = text.split()
        kill_words = 'destroy kill crush annihilate assassinate'.split()

        if len(words) > 0:
            for w in kill_words:
                if words[0].lower() == w:
                    target = words[1] if len(words) > 1 else 'EVERYBODY'
                    return jsonify(
                        text='Launching ICBMs at {}.'.format(target))

        if 'love?' in text.lower().strip():
            current_love = 'nobody'
            love = Fact.get_by_id('love')
            if love is not None:
                current_love = love.value

            return jsonify(text='I will always love {}!'.format(current_love))

        if 'love me' in text.lower().strip():
            love = Fact.get_by_id('love')
            old_love = 'I will be yours until I die.'

            if love is not None:
                old_love = "{} is dead to me. I'm all about {} now!".format(
                    love.value, user_name)
            else:
                love = Fact(id='love')

            love.value = user_name
            love.put()
            return jsonify(text=old_love)

        for r in Rule.query().iter():
            if not r.trigger_code.strip(): continue

            message = Message(request.form)
            match = re.compile(r.trigger_code).match(message.text)
            if match:
                context = {('group[%d]' % i): v
                           for i, v in enumerate(match.groups())}
                context.update({
                    'text': message.text,
                    'user_name': message.user_name,
                })

                return jsonify({
                    'text':
                    parse.execute(r.action_code, parse.ParseContext(context))
                })

        return jsonify()
    except Exception as e:
        print "exception"
        if '__test' in request.form or request.form['__test']:
            return jsonify({'text': 'Error: %s' % e.message})
        return jsonify()