Custom python script to execute slack integration

Hi,

I would like to do the slack integration by executing a custom python script instead of running “rasa run” command. Could anyone please confirm the best way for doing this?

I was using the following code to execute the rasa_core 0.14 version. But now I can’t use it for new rasa version. It would be greatly appreciable, if someone could help me to change the below code to execute the new rasa version.

from rasa_core.channels.slack import SlackInput
from rasa_core.agent import Agent
from rasa_core.interpreter import RasaNLUInterpreter
from rasa_core.utils import EndpointConfig

nlu_interpreter = RasaNLUInterpreter(NLU_MODEL_PATH)
action_endpoint = EndpointConfig(url=ACTION_ENDPOINT_URL)

agent = Agent.load(
    CORE_MODEL_PATH,
    interpreter = nlu_interpreter,
    action_endpoint = action_endpoint,
)

input_channel = SlackInput(SLACK_ACCESS_TOKEN)
agent.handle_channels([input_channel], RASA_CORE_PORT, serve_forever=True)

In the new Rasa version we combined the Rasa Core and Rasa NLU model. When you train a Rasa model using, for example, using rasa train, you will get a tar.gz file containing both models, Core and NLU.

In order to load an agent, the model path should point to an unzipped version of the combined Rasa model file. To unzip a tar.gz model file, you can use the method rasa.model.get_model.

I modified your code. Can you try it? Please, let me know if it works.

Note: It is still possible to train the models individually. In that case the code will look a little bit different.

from rasa.core.channels.slack import SlackInput
from rasa.core.agent import Agent
from rasa.core.interpreter import RasaNLUInterpreter
from rasa.model import get_model, get_model_subdirectories
from rasa.utils.endpoints import EndpointConfig

MODEL_PATH = "<path-rasa-model>"
ACTION_ENDPOINT_URL = "<endpoint-url>"
SLACK_ACCESS_TOKEN = "<access-token>"
RASA_CORE_PORT = "<port>"


model_path = get_model(MODEL_PATH)

if not model_path:
    print("No model found.")
    exit(1)

_, nlu_model = get_model_subdirectories(model_path)
nlu_interpreter = RasaNLUInterpreter(nlu_model)

action_endpoint = EndpointConfig(url=ACTION_ENDPOINT_URL)

agent = Agent.load(
    model_path,
    interpreter = nlu_interpreter,
    action_endpoint = action_endpoint,
)

input_channel = SlackInput(SLACK_ACCESS_TOKEN)
agent.handle_channels([input_channel], RASA_CORE_PORT, serve_forever=True)
1 Like

Thank you so much for your support @Tanja

That worked perfectly fine. I did one more additional change in the last line to remove " serve_forever=True" parameter, as it was throwing an error (unexpected keyword argument ‘serve_forever’). Your code was working without any issues after removing that parameter.

Once again, thanks a lot for your time to modify my code to make it workable. Great support !!!

1 Like