def start_server(input_channels, cors, auth_token, port, initial_agent, enable_api=True, jwt_secret=None, jwt_method=None): """Run the agent.""" if enable_api: app = server.create_app(initial_agent, cors_origins=cors, auth_token=auth_token, jwt_secret=jwt_secret, jwt_method=jwt_method) else: app = Flask(__name__) CORS(app, resources={r"/*": {"origins": cors or ""}}) if input_channels: rasa_core.channels.channel.register(input_channels, app, initial_agent.handle_message, route="/webhooks/") if logger.isEnabledFor(logging.DEBUG): utils.list_routes(app) http_server = WSGIServer(('0.0.0.0', port), app) logger.info("Rasa Core server is up and running on " "{}".format(constants.DEFAULT_SERVER_FORMAT.format(port))) http_server.start() return http_server
def start_server(input_channels, cors, auth_token, port, initial_agent, enable_api=True): """Run the agent.""" if enable_api: app = server.create_app(initial_agent, cors_origins=cors, auth_token=auth_token) else: app = Flask(__name__) CORS(app, resources={r"/*": {"origins": cors or ""}}) if input_channels: rasa_core.channels.channel.register(input_channels, app, initial_agent.handle_message, route="/webhooks/") if logger.isEnabledFor(logging.DEBUG): utils.list_routes(app) http_server = WSGIServer(('0.0.0.0', port), app) logger.info("Rasa Core server is up and running on " "{}".format(constants.DEFAULT_SERVER_URL)) http_server.start() return http_server
def test_facebook_channel(): from rasa_core.channels.facebook import FacebookInput from rasa_core.agent import Agent from rasa_core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = FacebookInput( fb_verify="YOUR_FB_VERIFY", # you need tell facebook this token, to confirm your URL fb_secret="YOUR_FB_SECRET", # your app secret fb_access_token="YOUR_FB_PAGE_ACCESS_TOKEN" # token for the page you subscribed to ) # set serve_forever=True if you want to keep the server running s = agent.handle_channels([input_channel], 5004, serve_forever=False) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs try: assert s.started routes_list = utils.list_routes(s.application) assert routes_list.get("/webhooks/facebook/").startswith( 'fb_webhook.health') assert routes_list.get("/webhooks/facebook/webhook").startswith( 'fb_webhook.webhook') finally: s.stop()
def test_callback_channel(): from rasa_core.channels.callback import CallbackInput from rasa_core.agent import Agent from rasa_core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = CallbackInput( # URL Core will call to send the bot responses endpoint=EndpointConfig("http://localhost:5004")) # set serve_forever=True if you want to keep the server running s = agent.handle_channels([input_channel], 5004, serve_forever=False) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs try: assert s.started routes_list = utils.list_routes(s.application) assert routes_list.get("/webhooks/callback/").startswith( 'callback_webhook.health') assert routes_list.get("/webhooks/callback/webhook").startswith( 'callback_webhook.webhook') finally: s.stop()
def test_twilio_channel(): from rasa_core.channels.twilio import TwilioInput from rasa_core.agent import Agent from rasa_core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = TwilioInput( # you get this from your twilio account account_sid="YOUR_ACCOUNT_SID", # also from your twilio account auth_token="YOUR_AUTH_TOKEN", # a number associated with your twilio account twilio_number="YOUR_TWILIO_NUMBER") # set serve_forever=True if you want to keep the server running s = agent.handle_channels([input_channel], 5004, serve_forever=False) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs try: assert s.started routes_list = utils.list_routes(s.application) assert routes_list.get("/webhooks/twilio/").startswith( 'twilio_webhook.health') assert routes_list.get("/webhooks/twilio/webhook").startswith( 'twilio_webhook.message') finally: s.stop()
def test_slack_channel(): from rasa_core.channels.slack import SlackInput from rasa_core.agent import Agent from rasa_core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = SlackInput( slack_token="YOUR_SLACK_TOKEN", # this is the `bot_user_o_auth_access_token` slack_channel="YOUR_SLACK_CHANNEL" # the name of your channel to which the bot posts (optional) ) # set serve_forever=False if you want to keep the server running s = agent.handle_channels([input_channel], 5004, serve_forever=False) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs try: assert s.started routes_list = utils.list_routes(s.application) assert routes_list.get("/webhooks/slack/").startswith( 'slack_webhook.health') assert routes_list.get("/webhooks/slack/webhook").startswith( 'slack_webhook.webhook') finally: s.stop()
def test_botframework_channel(): from rasa_core.channels.botframework import BotFrameworkInput from rasa_core.agent import Agent from rasa_core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = BotFrameworkInput( # you get this from your Bot Framework account app_id="MICROSOFT_APP_ID", # also from your Bot Framework account app_password="******") # set serve_forever=True if you want to keep the server running s = agent.handle_channels([input_channel], 5004, serve_forever=False) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs try: assert s.started routes_list = utils.list_routes(s.application) assert routes_list.get("/webhooks/botframework/").startswith( 'botframework_webhook.health') assert routes_list.get("/webhooks/botframework/webhook").startswith( 'botframework_webhook.webhook') finally: s.stop()
def test_rocketchat_channel(): from rasa_core.channels.rocketchat import RocketChatInput from rasa_core.agent import Agent from rasa_core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = RocketChatInput( # your bots rocket chat user name user="******", # the password for your rocket chat bots account password="******", # url where your rocket chat instance is running server_url="https://demo.rocket.chat") # set serve_forever=True if you want to keep the server running s = agent.handle_channels([input_channel], 5004, serve_forever=False) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs try: assert s.started routes_list = utils.list_routes(s.application) assert routes_list.get("/webhooks/rocketchat/").startswith( 'rocketchat_webhook.health') assert routes_list.get("/webhooks/rocketchat/webhook").startswith( 'rocketchat_webhook.webhook') finally: s.stop()
def test_slack_channel(): from rasa_core.channels.slack import SlackInput from rasa_core.agent import Agent from rasa_core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = SlackInput( slack_token="YOUR_SLACK_TOKEN", # this is the `bot_user_o_auth_access_token` slack_channel="YOUR_SLACK_CHANNEL" # the name of your channel to which the bot posts (optional) ) # set serve_forever=True if you want to keep the server running s = agent.handle_channels([input_channel], 5004, serve_forever=False) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs try: assert s.started routes_list = utils.list_routes(s.application) assert routes_list.get("/webhooks/slack/").startswith( 'slack_webhook.health') assert routes_list.get("/webhooks/slack/webhook").startswith( 'slack_webhook.webhook') finally: s.stop()
def test_mattermost_channel(): from rasa_core.channels.mattermost import MattermostInput from rasa_core.agent import Agent from rasa_core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = MattermostInput( # this is the url of the api for your mattermost instance url="http://chat.example.com/api/v4", # the name of your team for mattermost team="community", # the username of your bot user that will post user="******", # messages pw="password" # the password of your bot user that will post messages ) # set serve_forever=True if you want to keep the server running s = agent.handle_channels([input_channel], 5004, serve_forever=False) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs try: assert s.started routes_list = utils.list_routes(s.application) assert routes_list.get("/webhooks/mattermost/").startswith( 'mattermost_webhook.health') assert routes_list.get("/webhooks/mattermost/webhook").startswith( 'mattermost_webhook.webhook') finally: s.stop()
def test_facebook_channel(): from rasa_core.channels.facebook import FacebookInput from rasa_core.agent import Agent from rasa_core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = FacebookInput( fb_verify="YOUR_FB_VERIFY", # you need tell facebook this token, to confirm your URL fb_secret="YOUR_FB_SECRET", # your app secret fb_access_token="YOUR_FB_PAGE_ACCESS_TOKEN" # token for the page you subscribed to ) # set serve_forever=False if you want to keep the server running s = agent.handle_channels([input_channel], 5004, serve_forever=False) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs try: assert s.started routes_list = utils.list_routes(s.application) assert routes_list.get("/webhooks/facebook/").startswith( 'fb_webhook.health') assert routes_list.get("/webhooks/facebook/webhook").startswith( 'fb_webhook.webhook') finally: s.stop()
def test_socketio_channel(): from rasa_core.channels.socketio import SocketIOInput from rasa_core.agent import Agent from rasa_core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = SocketIOInput( # event name for messages sent from the user user_message_evt="user_uttered", # event name for messages sent from the bot bot_message_evt="bot_uttered", # socket.io namespace to use for the messages namespace=None) # set serve_forever=True if you want to keep the server running s = agent.handle_channels([input_channel], 5004, serve_forever=False) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs try: assert s.started routes_list = utils.list_routes(s.application) assert routes_list.get("/webhooks/socketio/").startswith( 'socketio_webhook.health') finally: s.stop()
def test_twilio_channel(): from rasa_core.channels.twilio import TwilioInput from rasa_core.agent import Agent from rasa_core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = TwilioInput( # you get this from your twilio account account_sid="YOUR_ACCOUNT_SID", # also from your twilio account auth_token="YOUR_AUTH_TOKEN", # a number associated with your twilio account twilio_number="YOUR_TWILIO_NUMBER" ) # set serve_forever=False if you want to keep the server running s = agent.handle_channels([input_channel], 5004, serve_forever=False) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs try: assert s.started routes_list = utils.list_routes(s.application) assert routes_list.get("/webhooks/twilio/").startswith( 'twilio_webhook.health') assert routes_list.get("/webhooks/twilio/webhook").startswith( 'twilio_webhook.message') finally: s.stop()
def test_rocketchat_channel(): from rasa_core.channels.rocketchat import RocketChatInput from rasa_core.agent import Agent from rasa_core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = RocketChatInput( # your bots rocket chat user name user="******", # the password for your rocket chat bots account password="******", # url where your rocket chat instance is running server_url="https://demo.rocket.chat" ) # set serve_forever=False if you want to keep the server running s = agent.handle_channels([input_channel], 5004, serve_forever=False) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs try: assert s.started routes_list = utils.list_routes(s.application) assert routes_list.get("/webhooks/rocketchat/").startswith( 'rocketchat_webhook.health') assert routes_list.get("/webhooks/rocketchat/webhook").startswith( 'rocketchat_webhook.webhook') finally: s.stop()
def test_botframework_channel(): from rasa_core.channels.botframework import BotFrameworkInput from rasa_core.agent import Agent from rasa_core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = BotFrameworkInput( # you get this from your Bot Framework account app_id="MICROSOFT_APP_ID", # also from your Bot Framework account app_password="******" ) # set serve_forever=False if you want to keep the server running s = agent.handle_channels([input_channel], 5004, serve_forever=False) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs try: assert s.started routes_list = utils.list_routes(s.application) assert routes_list.get("/webhooks/botframework/").startswith( 'botframework_webhook.health') assert routes_list.get("/webhooks/botframework/webhook").startswith( 'botframework_webhook.webhook') finally: s.stop()
def test_mattermost_channel(): from rasa_core.channels.mattermost import MattermostInput from rasa_core.agent import Agent from rasa_core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = MattermostInput( # this is the url of the api for your mattermost instance url="http://chat.example.com/api/v4", # the name of your team for mattermost team="community", # the username of your bot user that will post user="******", # messages pw="password" # the password of your bot user that will post messages ) # set serve_forever=False if you want to keep the server running s = agent.handle_channels([input_channel], 5004, serve_forever=False) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs try: assert s.started routes_list = utils.list_routes(s.application) assert routes_list.get("/webhooks/mattermost/").startswith( 'mattermost_webhook.health') assert routes_list.get("/webhooks/mattermost/webhook").startswith( 'mattermost_webhook.webhook') finally: s.stop()
def test_webexteams_channel(): from rasa_core.channels.webexteams import WebexTeamsInput from rasa_core.agent import Agent from rasa_core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = WebexTeamsInput( access_token="YOUR_ACCESS_TOKEN", # this is the `bot access token` room="YOUR_WEBEX_ROOM" # the name of your channel to which the bot posts (optional) ) # set serve_forever=True if you want to keep the server running s = agent.handle_channels([input_channel], 5004, serve_forever=False) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs try: assert s.started routes_list = utils.list_routes(s.application) assert routes_list.get("/webhooks/webexteams/").startswith( 'webexteams_webhook.health') assert routes_list.get("/webhooks/webexteams/webhook").startswith( 'webexteams_webhook.webhook') finally: s.stop()
def start_server(input_channels, cors, auth_token, port, initial_agent, enable_api=True, jwt_secret=None, jwt_method=None): """Run the agent.""" from rasa_core import server from flask import Flask from flask_cors import CORS, cross_origin if enable_api: app = server.create_app(initial_agent, cors_origins=cors, auth_token=auth_token, jwt_secret=jwt_secret, jwt_method=jwt_method) else: from rasa_core.version import __version__ app = Flask(__name__) CORS(app, resources={r"/*": {"origins": cors or ""}}) cors_origins = cors or [] @app.route("/", methods=['GET', 'OPTIONS']) @cross_origin(origins=cors_origins) def hello(): """Check if the server is running and responds with the version.""" return "hello from Rasa Core: " + __version__ if input_channels: rasa_core.channels.channel.register(input_channels, app, initial_agent.handle_message, route="/webhooks/") if logger.isEnabledFor(logging.DEBUG): utils.list_routes(app) http_server = WSGIServer(('0.0.0.0', port), app) logger.info("Rasa Core server is up and running on " "{}".format(constants.DEFAULT_SERVER_FORMAT.format(port))) http_server.start() return http_server
def start_server(input_channels, cors, auth_token, port, initial_agent, enable_api=True, jwt_secret=None, jwt_method=None): """Run the agent.""" if enable_api: app = server.create_app(initial_agent, cors_origins=cors, auth_token=auth_token, jwt_secret=jwt_secret, jwt_method=jwt_method) # print('I am a Not a flask app'); else: app = Flask(__name__) app.logger.setLevel(logging.ERROR) # print('I am a flask app'); app.debug = False CORS(app, resources={r"/*": {"origins": cors or ""}}) if input_channels: rasa_core.channels.channel.register(input_channels, app, initial_agent.handle_message, route="/webhooks/") if logger.isEnabledFor(logging.DEBUG): utils.list_routes(app) http_server = WSGIServer(('0.0.0.0', port), application=app, log=None, backlog=None, error_log=None) # http://www.gevent.org/api/gevent.pywsgi.html; Log level details; logger.info("Rasa Core server is up and running on " "{}".format(constants.DEFAULT_SERVER_FORMAT.format(port))) http_server.start() return http_server
def test_list_routes(default_agent): from rasa_core import server app = server.create_app(default_agent, auth_token=None) routes = utils.list_routes(app) assert set(routes.keys()) == { 'hello', 'version', 'execute_action', 'append_event', 'replace_events', 'list_trackers', 'retrieve_tracker', 'retrieve_story', 'respond', 'predict', 'parse', 'train_stack', 'log_message', 'load_model', 'evaluate_stories', 'get_domain', 'continue_training', 'status', 'tracker_predict' }
def test_register_channel_without_route(): """Check we properly connect the input channel blueprint if route is None""" from rasa_core.channels import RestInput from flask import Flask import rasa_core # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = RestInput() app = Flask(__name__) rasa_core.channels.channel.register([input_channel], app, agent.handle_message, route=None) routes_list = utils.list_routes(app) assert routes_list.get("/webhook").startswith( "custom_webhook_RestInput.receive")
def test_telegram_channel(): # telegram channel will try to set a webhook, so we need to mock the api httpretty.register_uri( httpretty.POST, 'https://api.telegram.org/bot123:YOUR_ACCESS_TOKEN/setWebhook', body='{"ok": true, "result": {}}') httpretty.enable() from rasa_core.channels.telegram import TelegramInput from rasa_core.agent import Agent from rasa_core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = TelegramInput( # you get this when setting up a bot access_token="123:YOUR_ACCESS_TOKEN", # this is your bots username verify="YOUR_TELEGRAM_BOT", # the url your bot should listen for messages webhook_url="YOUR_WEBHOOK_URL" ) # set serve_forever=False if you want to keep the server running s = agent.handle_channels([input_channel], 5004, serve_forever=False) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs try: assert s.started routes_list = utils.list_routes(s.application) assert routes_list.get("/webhooks/telegram/").startswith( 'telegram_webhook.health') assert routes_list.get("/webhooks/telegram/webhook").startswith( 'telegram_webhook.message') finally: s.stop() httpretty.disable()
def test_list_routes(default_agent): from rasa_core import server app = server.create_app(default_agent, auth_token=None) routes = utils.list_routes(app) assert len(routes) > 0