-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.py
41 lines (29 loc) · 917 Bytes
/
chatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
import re
import logging
from logging import FileHandler, Formatter
from flask import Flask, render_template, jsonify, request
from rivescript import RiveScript
app = Flask(__name__)
# initialize RiveScript stuff
bot = RiveScript()
bot.load_directory(os.path.join(os.getcwd(), 'brain'))
bot.sort_replies()
# setup log file
file_handler = FileHandler('error_log.log')
file_handler.setLevel(logging.ERROR)
file_handler.setFormatter(
Formatter('%(asctime)s,%(msecs)d %(levelname)-5s [%(filename)s:%(lineno)d] %(message)s',
datefmt='%d-%m-%Y:%H:%M:%S'
)
)
app.logger.addHandler(file_handler)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/reply', methods=['POST'])
def reply():
# capture what the user said
user_msg = request.json['userMsg']
botreply = bot.reply('localuser', user_msg)
jsonify({"reply": botreply})