Last active
October 31, 2018 18:34
-
-
Save mmxmb/23d469b5aff75677c6039a7940772dd5 to your computer and use it in GitHub Desktop.
Get websocket_server.py from https://github.com/Pithikos/python-websocket-server . Can test the chatbot using client.html from the same repo. Open client.html in multiple windows to simulate several clients talking to a single bot running on a single thread. To create custom client_id, edit id assignments on line 136 of websocket_server.py.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rasa_core.agent import Agent | |
import os | |
import argparse | |
from websocket_server import WebsocketServer # taken from https://github.com/Pithikos/python-websocket-server | |
# CONSTANTS | |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Ignore TF CPU instructions warning; see https://stackoverflow.com/questions/47068709/your-cpu-supports-instructions-that-this-tensorflow-binary-was-not-compiled-to-u | |
HOST = '0.0.0.0' # accept any host | |
PORT = 9001 | |
def run_ws(ws_server): | |
""" | |
Add request processing functions to a websocket server and make it handle requests unitl an explicit terminate() request (or SIGINT signal). | |
This process runs forever! | |
Parameters | |
---------- | |
ws_server : websocket_server.websocket_server.WebsocketServer | |
""" | |
ws_server.set_fn_new_client(new_client) # Listen for client entering chat | |
ws_server.set_fn_client_left(client_left) # Listen for client leaving chat | |
ws_server.set_fn_message_received(message_received) # Listen for client input (and respond) | |
ws_server.run_forever() # Run forever (until client leaves chat) | |
##------------------------------------------------------------------------## | |
#------------------- Websocket Request Handlers ---------------------------# | |
##------------------------------------------------------------------------## | |
def new_client(client, ws_server): | |
""" | |
Called for every client connecting (after handshake). | |
Parameters | |
---------- | |
client : dict | |
Some info about client. Ex: {'id': 1, 'handler': <websocket_server.websocket_server.WebSocketHandler object at 0x000001C44ECF0208>, 'address': ('127.0.0.1', 19273)} | |
ws_server : websocket_server.websocket_server.WebsocketServer | |
Websockets Server object | |
""" | |
ws_server.send_message_to_all("Client with id = {} connected".format(client['id'])) | |
if debug: | |
print("New client connected and was given id {}".format(client['id'])) | |
def client_left(client, ws_server): | |
""" | |
Called for every client disconnecting. | |
Parameters | |
---------- | |
client : dict | |
Some info about client. Ex: {'id': 1, 'handler': <websocket_server.websocket_server.WebSocketHandler object at 0x000001C44ECF0208>, 'address': ('127.0.0.1', 19273)} | |
ws_server : websocket_server.websocket_server.WebsocketServer | |
Websockets Server object | |
""" | |
ws_server.send_message_to_all("Client with id = {} disconnected".format(client['id'])) | |
if debug: | |
print("Client({}) disconnected".format(client['id'])) | |
def message_received(client, ws_server, message): | |
""" | |
Called when a client sends a message. | |
Parameters | |
---------- | |
client : dict | |
Some info about client. Ex: {'id': 1, 'handler': <websocket_server.websocket_server.WebSocketHandler object at 0x000001C44ECF0208>, 'address': ('127.0.0.1', 19273)} | |
ws_server : websocket_server.websocket_server.WebsocketServer | |
Websockets Server object | |
message : str | |
Text input from a client | |
""" | |
response = helpbot.handle_message(text_message=message, sender_id=client['id']) | |
ws_server.send_message(client, response[0]) | |
if debug: | |
print("Client({}) said: {}".format(client['id'], message)) | |
print("Bot responded: {}".format(response)) | |
##------------------------------------------------------------------------## | |
#------------------------------- Misc Helpers -----------------------------# | |
##------------------------------------------------------------------------## | |
def parse_args(): | |
parser = argparse.ArgumentParser(description='Run chatbot on port 9001') | |
parser.add_argument('-d', '--debug', action="store_true", dest="debug", | |
help='send debug messages to stdout (default: no output by default)') | |
args = parser.parse_args() | |
return args.debug | |
if __name__=="__main__": | |
dialogue_path = os.path.abspath(os.path.join(os.path.dirname( __file__ ), 'models/dialogue')) | |
debug = parse_args() | |
helpbot = Agent.load(path=dialogue_path) | |
run_ws(WebsocketServer(port=PORT, host=HOST)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment