def configure_app( input_channels=None, cors=None, auth_token=None, enable_api=True, jwt_secret=None, jwt_method=None, route="/webhooks/", port=None, ): """Run the agent.""" from rasa.core import server if enable_api: app = server.create_app( cors_origins=cors, auth_token=auth_token, jwt_secret=jwt_secret, jwt_method=jwt_method, ) else: app = Sanic(__name__) CORS(app, resources={r"/*": { "origins": cors or "" }}, automatic_options=True) if input_channels: rasa.core.channels.channel.register(input_channels, app, route=route) else: input_channels = [] if logger.isEnabledFor(logging.DEBUG): utils.list_routes(app) # configure async loop logging async def configure_logging(): if logger.isEnabledFor(logging.DEBUG): rasa.utils.io.enable_async_loop_debugging(asyncio.get_event_loop()) app.add_task(configure_logging) if "cmdline" in {c.name() for c in input_channels}: async def run_cmdline_io(running_app: Sanic): """Small wrapper to shut down the server once cmd io is done.""" await asyncio.sleep(1) # allow server to start await console.record_messages( server_url=constants.DEFAULT_SERVER_FORMAT.format(port)) logger.info("Killing Sanic server now.") running_app.stop() # kill the sanic serverx app.add_task(run_cmdline_io) return app
def test_list_routes(default_agent): from rasa import server app = server.create_app(default_agent, auth_token=None) routes = utils.list_routes(app) assert set(routes.keys()) == { "hello", "version", "status", "retrieve_tracker", "append_events", "replace_events", "retrieve_story", "execute_action", "predict", "add_message", "train", "evaluate_stories", "evaluate_intents", "tracker_predict", "parse", "load_model", "unload_model", "get_domain", }
def test_facebook_channel(): with mock.patch.object(sanic.Sanic, "run", fake_sanic_run): # START DOC INCLUDE 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 ) s = agent.handle_channels([input_channel], 5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list.get("fb_webhook.health").startswith( "/webhooks/facebook") assert routes_list.get("fb_webhook.webhook").startswith( "/webhooks/facebook/webhook")
def test_telegram_channel(): # telegram channel will try to set a webhook, so we need to mock the api with mock.patch.object(sanic.Sanic, 'run', fake_sanic_run): httpretty.register_uri( httpretty.POST, 'https://api.telegram.org/bot123:YOUR_ACCESS_TOKEN/setWebhook', body='{"ok": true, "result": {}}') httpretty.enable() # START DOC INCLUDE 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") s = agent.handle_channels([input_channel], 5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list.get("telegram_webhook.health").startswith( "/webhooks/telegram") assert routes_list.get("telegram_webhook.message").startswith( "/webhooks/telegram/webhook") httpretty.disable()
def test_slack_channel(): with mock.patch.object(sanic.Sanic, "run", fake_sanic_run): # START DOC INCLUDE 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) ) s = agent.handle_channels([input_channel], 5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list.get("slack_webhook.health").startswith( "/webhooks/slack") assert routes_list.get("slack_webhook.webhook").startswith( "/webhooks/slack/webhook")
def test_mattermost_channel(): with mock.patch.object(sanic.Sanic, "run", fake_sanic_run): # START DOC INCLUDE 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 ) s = agent.handle_channels([input_channel], 5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list.get("mattermost_webhook.health").startswith( "/webhooks/mattermost") assert routes_list.get("mattermost_webhook.webhook").startswith( "/webhooks/mattermost/webhook")
def test_rocketchat_channel(): with mock.patch.object(sanic.Sanic, "run", fake_sanic_run): # START DOC INCLUDE 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", ) s = agent.handle_channels([input_channel], 5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list.get("rocketchat_webhook.health").startswith( "/webhooks/rocketchat") assert routes_list.get("rocketchat_webhook.webhook").startswith( "/webhooks/rocketchat/webhook")
def test_botframework_channel(): with mock.patch.object(sanic.Sanic, "run", fake_sanic_run): # START DOC INCLUDE 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="******", ) s = agent.handle_channels([input_channel], 5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list.get("botframework_webhook.health").startswith( "/webhooks/botframework") assert routes_list.get("botframework_webhook.webhook").startswith( "/webhooks/botframework/webhook")
def test_telegram_channel(): # START DOC INCLUDE 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", ) s = agent.handle_channels([input_channel], 5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list.get("telegram_webhook.health").startswith("/webhooks/telegram") assert routes_list.get("telegram_webhook.message").startswith( "/webhooks/telegram/webhook" )
def test_mattermost_channel(): # START DOC INCLUDE from rasa.core.channels.mattermost import MattermostInput input_channel = MattermostInput( # this is the url of the api for your mattermost instance url="http://chat.example.com/api/v4", # the bot token of the bot account that will post messages token="xxxxx", # the password of your bot user that will post messages # the webhook-url your bot should listen for messages webhook_url="YOUR_WEBHOOK_URL", ) s = rasa.core.run.configure_app([input_channel], port=5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list.get("mattermost_webhook.health").startswith( "/webhooks/mattermost" ) assert routes_list.get("mattermost_webhook.webhook").startswith( "/webhooks/mattermost/webhook" )
def test_socketio_channel(): with mock.patch.object(sanic.Sanic, "run", fake_sanic_run): # START DOC INCLUDE 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, ) s = agent.handle_channels([input_channel], 5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list.get("socketio_webhook.health").startswith( "/webhooks/socketio") assert routes_list.get("handle_request").startswith("/socket.io")
def test_twilio_channel(): with mock.patch.object(sanic.Sanic, "run", fake_sanic_run): # START DOC INCLUDE 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", ) s = agent.handle_channels([input_channel], 5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list.get("twilio_webhook.health").startswith( "/webhooks/twilio") assert routes_list.get("twilio_webhook.message").startswith( "/webhooks/twilio/webhook")
def test_mattermost_channel(): # START DOC INCLUDE from rasa.core.channels.mattermost import MattermostInput 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 ) s = rasa.core.run.configure_app([input_channel], port=5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list.get("mattermost_webhook.health").startswith( "/webhooks/mattermost" ) assert routes_list.get("mattermost_webhook.webhook").startswith( "/webhooks/mattermost/webhook" )
def test_webexteams_channel(): with mock.patch.object(sanic.Sanic, "run", fake_sanic_run): # START DOC INCLUDE 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) ) s = agent.handle_channels([input_channel], 5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list.get("webexteams_webhook.health").startswith( "/webhooks/webexteams") assert routes_list.get("webexteams_webhook.webhook").startswith( "/webhooks/webexteams/webhook")
def test_callback_channel(): with mock.patch.object(sanic.Sanic, "run", fake_sanic_run): # START DOC INCLUDE 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") ) s = agent.handle_channels([input_channel], 5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list.get("callback_webhook.health").startswith( "/webhooks/callback" ) assert routes_list.get("callback_webhook.webhook").startswith( "/webhooks/callback/webhook" )
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', 'evaluate_intents', '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.channel import RestInput import rasa.core input_channel = RestInput() app = Sanic(__name__) rasa.core.channels.channel.register([input_channel], app, route=None) routes_list = utils.list_routes(app) assert routes_list.get("custom_webhook_RestInput.receive").startswith("/webhook")
def test_channel_inheritance(): from rasa.core.channels import RestInput from rasa.core.channels.rasa_chat import RasaChatInput rasa_input = RasaChatInput("https://example.com") s = rasa.core.run.configure_app([RestInput(), rasa_input], port=5004) routes_list = utils.list_routes(s) assert routes_list["custom_webhook_RasaChatInput.health"].startswith( "/webhooks/rasa") assert routes_list["custom_webhook_RasaChatInput.receive"].startswith( "/webhooks/rasa/webhook")
def test_register_channel_without_route(): """Check we properly connect the input channel blueprint if route is None""" from rasa.core.channels import RestInput import rasa.core # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) input_channel = RestInput() app = Sanic(__name__) rasa.core.channels.channel.register([input_channel], app, route=None) routes_list = utils.list_routes(app) assert routes_list.get("custom_webhook_RestInput.receive").startswith( "/webhook")
def test_facebook_channel(): from rasa.core.channels.facebook import FacebookInput 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 ) s = run.configure_app([input_channel], port=5004) routes_list = utils.list_routes(s) assert routes_list["fb_webhook.health"].startswith("/webhooks/facebook") assert routes_list["fb_webhook.webhook"].startswith("/webhooks/facebook/webhook")
def test_callback_channel(): # START DOC INCLUDE from rasa.core.channels.callback import CallbackInput input_channel = CallbackInput( # URL Core will call to send the bot responses endpoint=EndpointConfig("http://localhost:5004")) s = rasa.core.run.configure_app([input_channel], port=5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list["callback_webhook.health"].startswith( "/webhooks/callback") assert routes_list["callback_webhook.webhook"].startswith( "/webhooks/callback/webhook")
def test_channel_inheritance(): from rasa.core.channels.channel import RestInput from rasa.core.channels.rasa_chat import RasaChatInput from rasa.core.agent import Agent from rasa.core.interpreter import RegexInterpreter # load your trained agent agent = Agent.load(MODEL_PATH, interpreter=RegexInterpreter()) rasa_input = RasaChatInput("https://example.com") s = agent.handle_channels([RestInput(), rasa_input], 5004) routes_list = utils.list_routes(s) assert routes_list.get("custom_webhook_RasaChatInput.health").startswith( "/webhooks/rasa") assert routes_list.get("custom_webhook_RasaChatInput.receive").startswith( "/webhooks/rasa/webhook")
def test_channel_registration_with_absolute_url_prefix_overwrites_route(): from rasa.core.channels.channel import RestInput import rasa.core input_channel = RestInput() test_route = "/absolute_route" input_channel.url_prefix = lambda: test_route app = Sanic(__name__) ignored_base_route = "/should_be_ignored" rasa.core.channels.channel.register( [input_channel], app, route="/should_be_ignored" ) # Assure that an absolute url returned by `url_prefix` overwrites route parameter # given in `register`. routes_list = utils.list_routes(app) assert routes_list.get("custom_webhook_RestInput.health").startswith(test_route) assert ignored_base_route not in routes_list.get("custom_webhook_RestInput.health")
def test_slack_channel(): # START DOC INCLUDE from rasa.core.channels.slack import SlackInput 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) ) s = rasa.core.run.configure_app([input_channel], port=5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list["slack_webhook.health"].startswith("/webhooks/slack") assert routes_list["slack_webhook.webhook"].startswith( "/webhooks/slack/webhook")
def test_slack_channel(): # START DOC INCLUDE from rasa.core.channels.slack import SlackInput input_channel = SlackInput( # this is the Slack Bot Token slack_token="YOUR_SLACK_TOKEN", # the name of your channel to which the bot posts (optional) slack_channel="YOUR_SLACK_CHANNEL", # signing secret from slack to verify incoming webhook messages slack_signing_secret="YOUR_SIGNING_SECRET", ) s = rasa.core.run.configure_app([input_channel], port=5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list["slack_webhook.health"].startswith("/webhooks/slack") assert routes_list["slack_webhook.webhook"].startswith("/webhooks/slack/webhook")
def test_socketio_channel(): # START DOC INCLUDE from rasa.core.channels.socketio import SocketIOInput 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, ) s = rasa.core.run.configure_app([input_channel], port=5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list.get("socketio_webhook.health").startswith("/webhooks/socketio") assert routes_list.get("handle_request").startswith("/socket.io")
def test_twilio_channel(): # START DOC INCLUDE from rasa.core.channels.twilio import TwilioInput 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", ) s = rasa.core.run.configure_app([input_channel], port=5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list["twilio_webhook.health"].startswith("/webhooks/twilio") assert routes_list["twilio_webhook.message"].startswith("/webhooks/twilio/webhook")
def test_webexteams_channel(): # START DOC INCLUDE from rasa.core.channels.webexteams import WebexTeamsInput 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) ) s = rasa.core.run.configure_app([input_channel], port=5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list["webexteams_webhook.health"].startswith( "/webhooks/webexteams") assert routes_list["webexteams_webhook.webhook"].startswith( "/webhooks/webexteams/webhook")
def test_botframework_channel(): # START DOC INCLUDE from rasa.core.channels.botframework import BotFrameworkInput input_channel = BotFrameworkInput( # you get this from your Bot Framework account app_id="MICROSOFT_APP_ID", # also from your Bot Framework account app_password="******", ) s = rasa.core.run.configure_app([input_channel], port=5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list["botframework_webhook.health"].startswith( "/webhooks/botframework") assert routes_list["botframework_webhook.webhook"].startswith( "/webhooks/botframework/webhook")
def test_facebook_channel(): # START DOC INCLUDE from rasa.core.channels.facebook import FacebookInput 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 ) s = rasa.core.run.configure_app([input_channel], port=5004) # END DOC INCLUDE # the above marker marks the end of the code snipped included # in the docs routes_list = utils.list_routes(s) assert routes_list["fb_webhook.health"].startswith("/webhooks/facebook") assert routes_list["fb_webhook.webhook"].startswith("/webhooks/facebook/webhook")