maharanasarkar / whatsapp-connector-rasa

A custom channel connector to connect WhatsApp API to Rasa
Apache License 2.0
15 stars 7 forks source link

Bug: Rasa WhatsApp Connector Sending Messages #12

Open mumbaigirl opened 8 months ago

mumbaigirl commented 8 months ago

The Rasa WhatsApp connector is currently experiencing a bug where it sends messages automatically without the user initiating a conversation.

Steps to Reproduce:

Keep the Rasa server running. rasa run --m models --enable-api --cors '*' --debug Wait for a period of time without any user interaction. Users receive messages from the Rasa WhatsApp connector without initiating a conversation. Screenshot 2024-03-12 221142 Screenshot 2024-03-12 221410

Below is the NGINX configuration

# Define upstream server
upstream rasa_server {
    server localhost:5005;  # Assuming Rasa server is running on localhost:5005
}

# HTTP server
server {
    listen 80;
    server_name my-domain.in www.my-domain.in;

    # Redirect all HTTP traffic to HTTPS
    return 301 https://$host$request_uri;
}

# HTTPS server
server {
    listen 443 ssl http2;
    server_name my-domain.in www.my-domain.in;

    # SSL certificate configuration
    ssl_certificate /etc/letsencrypt/live/ssl/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/ssl/privkey.pem;

    # SSL configuration
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";

    # Rasa server location block
    location / {
        proxy_pass http://rasa_server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    location /core/ {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $host;
    proxy_pass http://localhost/;
  }

    # Error pages
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}
Matilii commented 8 months ago

@thatcuteseller did you managed to solve this bug? Ive got the same issue.

mumbaigirl commented 8 months ago

@thatcuteseller did you managed to solve this bug? Ive got the same issue.

@Matilii No , I had to stop the server to prevent it from sending WhatsApp messages, I have raised an issue ,the author @maharanasarkar will look into it.

@Matilii By the way , are you able to connect telegram.py with RASA, on a live server not using ngrok?

mumbaigirl commented 8 months ago

@maharanasarkar kuthe aahe tumhi?

Matilii commented 8 months ago

@thatcuteseller

yes i was able to host the Rasa bot and then use the whatsapp connector. Without using ngrok on the hosted server

Huascar321 commented 1 week ago

Hello everyone, a little late but I think I figured out why this behavior is happening.

Following this stackoverflow problem:

https://stackoverflow.com/questions/72894209/whatsapp-cloud-api-sending-old-message-inbound-notification-multiple-time-on-my

The connector is not returning a 200 ok code to WhatsApp but is returning a 204 code

You can see this in this fragment of code in the whatsapp.py:

`@whatsapp_webhook.route("/webhook", methods=["POST"]) async def message(request: Request) -> HTTPResponse: sender = self.client.get_mobile(request.json)

        text = self.get_message(request.json)
        logger.debug(text)

        out_channel = self.get_output_channel()
        if sender is not None and message is not None:
            metadata = self.get_metadata(request)
            try:
                await on_new_message(
                    UserMessage(
                        text,
                        out_channel,
                        sender,
                        input_channel=self.name(),
                        metadata=metadata,
                    )
                )
            except Exception as e:
                logger.error(f"Exception when trying to handle message.{e}")
                logger.debug(e, exc_info=True)
                if self.debug_mode:
                    raise
        else:
            logger.debug("Invalid message")

        return response.text("", status=204) # You should change this to 200`

I hope this could help someone in the future