Rasa integration with flask and chat widget not working


I am struggling to get my Rasa chatbot to interact with my Flask application. I am using the Rasa chat widget, which displays fine on my Flask website. However, there seems to be some missing link between my website and my rasa server.


from flask import Flask, render_template, request, jsonify
import requests

app = Flask(__name__)

# Replace this with your Rasa server URL
RASA_SERVER_URL = "http://localhost:5005/webhooks/rest/webhook"

def index():
    return render_template("index.html")

@app.route("/ask", methods=["POST"])
def ask():
    user_message = request.form["user_message"]

    # Send the user message to the Rasa server
    rasa_response = requests.post(RASA_SERVER_URL, json={"sender": "user", "message": user_message}).json()

    # Extract the bot response from the Rasa server response
    bot_response = rasa_response[0]["text"]

    return jsonify({"bot_response": bot_response})

if __name__ == "__main__":
    app.run(port="8050", debug=True)


# This file contains the credentials for the voice & chat platforms
# which your bot is using.
# https://rasa.com/docs/rasa/messaging-and-voice-channels

#  # you don't need to provide anything here - this channel doesn't
#  # require any credentials

#  verify: "<verify>"
#  secret: "<your secret>"
#  page-access-token: "<your page access token>"

#  slack_token: "<your slack token>"
#  slack_channel: "<the slack channel>"
#  slack_signing_secret: "<your slack signing secret>"

  user_message_evt: user_uttered
  bot_message_evt: bot_uttered
  session_persistence: true

#  url: "https://<mattermost instance>/api/v4"
#  token: "<bot token>"
#  webhook_url: "<callback URL>"

# This entry is needed if you are using Rasa X. The entry represents credentials
# for the Rasa X "channel", i.e. Talk to your bot and Share with guest testers.
  url: "http://localhost:5002/api"


# The config recipe.
# https://rasa.com/docs/rasa/model-configuration/
recipe: default.v1

# Configuration for Rasa NLU.
# https://rasa.com/docs/rasa/nlu/components/
language: en

# # No configuration for the NLU pipeline was provided. The following default pipeline was used to train your model.
# # If you'd like to customize it, uncomment and adjust the pipeline.
# # See https://rasa.com/docs/rasa/tuning-your-model for more information.
- name: WhitespaceTokenizer
- name: RegexFeaturizer
- name: LexicalSyntacticFeaturizer
- name: CountVectorsFeaturizer
  analyzer: char_wb
  min_ngram: 1
  max_ngram: 4
- name: DIETClassifier
  epochs: 100
  constrain_similarities: true
  # - name: EntitySynonymMapper
  # - name: ResponseSelector
  #   epochs: 100
  #   constrain_similarities: true
# - name: FallbackClassifier
#   threshold: 0.7
#   ambiguity_threshold: 0.1
- name: "DucklingEntityExtractor"
    # url of the running duckling server
  url: "http://localhost:8000"
    # dimensions to extract
  dimensions: ["time", "number", "amount-of-money", "distance", "phone-number"]
    # if not set the default timezone of Duckling is going to be used
    # needed to calculate dates from relative expressions like "tomorrow"
  #timezone: "Europe/Berlin"
  locale: 'en_GB'

# Configuration for Rasa Core.
# https://rasa.com/docs/rasa/core/policies/
# # No configuration for policies was provided. The following default policies were used to train your model.
# # If you'd like to customize them, uncomment and adjust the policies.
# # See https://rasa.com/docs/rasa/policies for more information.
# - name: MemoizationPolicy
#   priority: 3
#   max_history: 3n
- name: "RulePolicy"
  priority: 1
  core_fallback_threshold: 0.5
  core_fallback_action_name: action_default_fallback
  enable_fallback_prediction: true
  restrict_rules: true
  check_for_contradictions: true
  # - name: UnexpecTEDIntentPolicy
  #   max_history: 5
  #   epochs: 100
- name: TEDPolicy
  priority: 2
  max_history: 5
  epochs: 100
  constrain_similarities: true
assistant_id: 20230703-191416-taxonomic-station

#action_default_fallback: action_custom_fallback

From my rasa server and action servers I get the following feedback:

Rasa server on
Action endpoint is up and running on

My flask app reports:

Running on

I had to change the original 8000 port because this port is already blocked by Duckling which is started with

docker run -p 8000:8000 rasa/duckling

I think that this is probably just a port issue but I currently have no idea how to resolve this.

Thanks for your support.

What is the command with which you have run your rasa server? Did you add --enable-api in the command?

I used the following code to start the rasa server

rasa run -m models --enable-api --cors --debug

Hi @newinmunich, try this:

rasa run -m models --enable-api --cors "*" --debug -p 5005

Hi @rasa_learner, unfortunately this did not solve the issue.