<commit_before># -*- encoding:utf8 -*-

import os
import urllib
import re

from flask import Blueprint, request, Response, abort
from flask_negotiate import consumes
from flask.ext.cors import cross_origin

import settings
from model.utils import Utils
from model.loader import PluginLoader

slack = Blueprint('slack', __name__, url_prefix='/api/slack')


@slack.route("/webhook", methods=['POST'])
@cross_origin()
@consumes('application/x-www-form-urlencoded')
def webhook():
    # For debug
    form = request.form
    print form

    # Check slack webhook token in request body
    request_token = Utils().parse_dic(form, 'token', 400)
    token = os.environ.get('SLACK_WEBHOOK_TOKEN')
    if not token or token != request_token:
        abort(401)

    # Parse request body
    username = Utils().parse_dic(form, 'user_name')
    trigger_word_uni = Utils().parse_dic(form, 'trigger_word')
    text_uni = Utils().parse_dic(form, 'text')

    # Check trigger user is not bot
    if not username or 'bot' in username:
        dic = {}
        return Response(Utils().dump_json(dic), mimetype='application/json')

    botname = settings.BOTNAME
    if not botname:
        abort(500)

    re_flags = settings.RE_FLAGS
    plugins = PluginLoader().get_plugins()
    content = None
    kwargs = {
        'text': text_uni,
        'trigger_word': trigger_word_uni,
        'botname': botname
    }
    for plugin in plugins:
        regex_uni = Utils().convert_unicode(plugin().hear_regex(**kwargs))
        print("Using plugin : %r" % plugin)
        print("Using regex : %r" % regex_uni)
        print("Target text : %r" % urllib.unquote_plus(text_uni))
        if re.compile(regex_uni, re_flags).match(urllib.unquote_plus(text_uni)):
            print("Regex found :)")
            content = plugin().response(**kwargs)
            break

    if not content:
        dic = {}
    else:
        dic = {"text": content}
    return Response(Utils().dump_json(dic), mimetype='application/json')<commit_msg>Fix slack response bugs which returns nesting content.<commit_after># -*- encoding:utf8 -*-

import os
import urllib
import re

from flask import Blueprint, request, Response, abort
from flask_negotiate import consumes
from flask.ext.cors import cross_origin

import settings
from model.utils import Utils
from model.loader import PluginLoader

slack = Blueprint('slack', __name__, url_prefix='/api/slack')


@slack.route("/webhook", methods=['POST'])
@cross_origin()
@consumes('application/x-www-form-urlencoded')
def webhook():
    # For debug
    form = request.form
    print form

    # Check slack webhook token in request body
    request_token = Utils().parse_dic(form, 'token', 400)
    token = os.environ.get('SLACK_WEBHOOK_TOKEN')
    if not token or token != request_token:
        abort(401)

    # Parse request body
    username = Utils().parse_dic(form, 'user_name')
    trigger_word_uni = Utils().parse_dic(form, 'trigger_word')
    text_uni = Utils().parse_dic(form, 'text')

    # Check trigger user is not bot
    if not username or 'bot' in username:
        dic = {}
        return Response(Utils().dump_json(dic), mimetype='application/json')

    botname = settings.BOTNAME
    if not botname:
        abort(500)

    re_flags = settings.RE_FLAGS
    plugins = PluginLoader().get_plugins()
    content = None
    kwargs = {
        'text': text_uni,
        'trigger_word': trigger_word_uni,
        'botname': botname
    }
    for plugin in plugins:
        regex_uni = Utils().convert_unicode(plugin().hear_regex(**kwargs))
        print("Using plugin : %r" % plugin)
        print("Using regex : %r" % regex_uni)
        print("Target text : %r" % urllib.unquote_plus(text_uni))
        if re.compile(regex_uni, re_flags).match(urllib.unquote_plus(text_uni)):
            print("Regex found :)")
            content = plugin().response(**kwargs)
            break

    if not content:
        dic = {}
    else:
        if isinstance(content, dict) and (content.get('text') or content.get('attachments')):
            dic = content
        else:
            dic = {"text": content}
    return Response(Utils().dump_json(dic), mimetype='application/json')
{'fraction_non_alphanumeric': 0.12162485065710872, 'fraction_numerical': 0.0047789725209080045, 'mean_word_length': 3.12007874015748, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5341669', 'n_tokens_mistral': 1323, 'n_tokens_neox': 1283, 'n_words': 324}