-
Notifications
You must be signed in to change notification settings - Fork 46
/
serve.py
53 lines (44 loc) · 1.84 KB
/
serve.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import logging
from rasa_core.channels import HttpInputChannel
from rasa_core import utils
from rasa_core.agent import Agent
from rasa_core.interpreter import RasaNLUInterpreter
from rasa_core.channels.channel import UserMessage
from rasa_core.channels.direct import CollectingOutputChannel
from rasa_core.channels.rest import HttpInputComponent
from flask import Blueprint, request, jsonify
logger = logging.getLogger(__name__)
class SimpleWebBot(HttpInputComponent):
"""A simple web bot that listens on a url and responds."""
def blueprint(self, on_new_message):
custom_webhook = Blueprint('custom_webhook', __name__)
@custom_webhook.route("/status", methods=['GET'])
def health():
return jsonify({"status": "ok"})
@custom_webhook.route("/", methods=['POST'])
def receive():
payload = request.json
sender_id = payload.get("sender", None)
text = payload.get("message", None)
out = CollectingOutputChannel()
on_new_message(UserMessage(text, out, sender_id))
responses = [m.get("text") for _, m in out.messages]
return jsonify(responses)
return custom_webhook
def run(serve_forever=True):
#path to your NLU model
interpreter = RasaNLUInterpreter("./models/nlu/default/customernlu")
# path to your dialogues models
agent = Agent.load("./models/dialogue", interpreter=interpreter)
#http api endpoint for responses
input_channel = SimpleWebBot()
if serve_forever:
agent.handle_channel(HttpInputChannel(5004, "/chat", input_channel))
return agent
if __name__ == '__main__':
utils.configure_colored_logging(loglevel="INFO")
run()