-
Notifications
You must be signed in to change notification settings - Fork 149
/
server.py
85 lines (70 loc) · 2.05 KB
/
server.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
server.py
~~~~~~~~~
web server,定义前端调用接口
:date: 2020-02-14 14:36:00
:author: by jiangdg
"""
from flask import Flask, jsonify
from flask import request
import requests
import json
import logging
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def test():
return"测试"
@app.route('/ai', methods=['GET', 'POST'])
def webToBot():
"""
前端调用接口
路径:/ai
请求方式:GET、POST
请求参数:content
:return: response rasa响应数据
"""
content = request.values.get('content')
if content is None:
return 'empty input'
response = requestRasabotServer('jiangdg', content)
return response.text.encode('utf-8').decode("unicode-escape")
def requestRasabotServer(userid, content):
"""
访问rasa服务
:param userid: 用户id
:param content: 自然语言文本
:return: json格式响应数据
"""
params = {'sender': userid, 'message': content}
botIp = '127.0.0.1'
botPort = '5005'
# rasa使用rest channel
# https://rasa.com/docs/rasa/user-guide/connectors/your-own-website/#rest-channels
# POST /webhooks/rest/webhook
rasaUrl = "http://{0}:{1}/webhooks/rest/webhook".format(botIp, botPort)
reponse = requests.post(
rasaUrl,
data=json.dumps(params),
headers={'Content-Type': 'application/json'}
)
return reponse
if __name__ == '__main__':
webIp = '127.0.0.1'
webPort = '8088'
print("##### webIp={}, webPort={}".format(webIp, webPort))
# 初始化日志引擎
fh = logging.FileHandler(encoding='utf-8', mode='a', filename='chitchat.log')
logging.basicConfig(
handlers=[fh],
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
)
# 启动服务,开启多线程、debug模式
# 浏览器访问http://127.0.0.1:8088/ai?content="你好"
app.run(
host=webIp,
port=int(webPort),
threaded=True,
debug=True
)