<commit_before>from flask import Flask, request, abort
from flask_pymongo import PyMongo
from wit import Wit
import grequests
import requests
import json

from utils import generate_sketch_dicts
import operator


CONFIGURATION_FILENAME = "configuration.json"

GOOGLE_MAPS_BASE_URL = 'https://maps.googleapis.com/maps/api/directions/json'
GOOGLE_MAPS_MODES = ["walking", "bicycling", "transit"]
GOOGLE_MAPS_API_KEY = ''

UBER_BASE_URL = 'https://api.uber.com/v1/'
UBER_API_KEY = ''

BUS_PRICE = 1.50


def getConfigurationVariables():
    with open (CONFIGURATION_FILENAME) as json_data:
        dictionary = json.load(json_data)
        global GOOGLE_MAPS_API_KEY
        global UBER_API_KEY
        global FB_VALIDATION_TOKEN
        global FB_TOKEN
        global WIT_TOKEN
        GOOGLE_MAPS_API_KEY = dictionary['GOOGLE_MAPS_API_KEY']
        UBER_API_KEY = dictionary['UBER_API_KEY']
        FB_VALIDATION_TOKEN = dictionary['FB_VALIDATION_TOKEN']
        FB_TOKEN = dictionary['FB_TOKEN']
        WIT_TOKEN = dictionary['WIT_TOKEN']


app = Flask(__name__)
getConfigurationVariables()
mongo = PyMongo(app)

client = Wit(access_token=WIT_TOKEN)

@app.route('/bot', methods=['POST'])
def tim_the_bot():
    if request.method == 'POST':
        payload = json.loads(request.get_data())
        print(payload)
        for event in payload['entry']:
            messaging = event['messaging']
            for x in messaging:
                if x.get('message') and x['message'].get('text'):
                    message = x['message']['text']
                    recipient_id = x['sender']['id']
                    print "Incoming from %s: %s" % (recipient_id, message)
                    fb_send_reply(recipient_id, wit_process_message(message))
    return '', 200


def fb_send_reply(recipient_id, message):
    params = {"access_token": FB_TOKEN}
    headers = {"Content-Type": "application/json"}
    data = json.dumps({"recipient": { "id": recipient_id},"message": {"text": message}})

    r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)

def wit_process_message(message):
    resp = client.message(message)
    print(resp)
    intent = None
    intent_array = resp['entities'].get('intent', False)
    if intent_array:
        intent = intent_array[0]['value']
<commit_msg>name and more<commit_after>from flask import Flask, request, abort
from flask_pymongo import PyMongo
from wit import Wit
import grequests
import requests
import json

from utils import generate_sketch_dicts
import operator


CONFIGURATION_FILENAME = "configuration.json"

GOOGLE_MAPS_BASE_URL = 'https://maps.googleapis.com/maps/api/directions/json'
GOOGLE_MAPS_MODES = ["walking", "bicycling", "transit"]
GOOGLE_MAPS_API_KEY = ''

UBER_BASE_URL = 'https://api.uber.com/v1/'
UBER_API_KEY = ''

BUS_PRICE = 1.50


def getConfigurationVariables():
    with open (CONFIGURATION_FILENAME) as json_data:
        dictionary = json.load(json_data)
        global GOOGLE_MAPS_API_KEY
        global UBER_API_KEY
        global FB_VALIDATION_TOKEN
        global FB_TOKEN
        global WIT_TOKEN
        GOOGLE_MAPS_API_KEY = dictionary['GOOGLE_MAPS_API_KEY']
        UBER_API_KEY = dictionary['UBER_API_KEY']
        FB_VALIDATION_TOKEN = dictionary['FB_VALIDATION_TOKEN']
        FB_TOKEN = dictionary['FB_TOKEN']
        WIT_TOKEN = dictionary['WIT_TOKEN']


app = Flask(__name__)
getConfigurationVariables()
mongo = PyMongo(app)

client = Wit(access_token=WIT_TOKEN)

@app.route('/bot', methods=['POST'])
def tim_the_bot():
    if request.method == 'POST':
        payload = json.loads(request.get_data())
        print(payload)
        for event in payload['entry']:
            messaging = event['messaging']
            for x in messaging:
                if x.get('message') and x['message'].get('text'):
                    message = x['message']['text']
                    recipient_id = x['sender']['id']
                    print "Incoming from %s: %s" % (recipient_id, message)
                    wit_process_message(recipient_id, message)
    return '', 200


def fb_send_reply(recipient_id, message):
    params = {"access_token": FB_TOKEN}
    headers = {"Content-Type": "application/json"}
    data = json.dumps({"recipient": { "id": recipient_id},"message": {"text": message}})

    r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)

def fb_get_user(user_id):
    url = "https://graph.facebook.com/v2.6/%s?fields=first_name&access_token=%s" % (user_id, FB_TOKEN)
    r = requests.post(url)
    return r.json()

def wit_process_message(recipient_id, message):
    resp = client.message(message)
    print(resp)
    intent = None
    intent_array = resp['entities'].get('intent', False)
    if intent_array:
        intent = intent_array[0]['value']
        print(fb_get_user(recipient_id))
        fb_send_reply(recipient_id, "Let me check that for you, give me on sec.")

{'fraction_non_alphanumeric': 0.12141280353200883, 'fraction_numerical': 0.004415011037527594, 'mean_word_length': 3.4579606440071555, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, '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': '1096540', 'n_tokens_mistral': 1656, 'n_tokens_neox': 1594, 'n_words': 323}