microsoft / botbuilder-python

The Microsoft Bot Framework provides what you need to build and connect intelligent bots that interact naturally wherever your users are talking, from text/sms to Skype, Slack, Office 365 mail and other popular services.
http://botframework.com
MIT License
710 stars 281 forks source link

UnsupportedMediaType - Azure Linux WebApp #312

Closed masyanru closed 5 years ago

masyanru commented 5 years ago

Version

4.5.0b4

Describe the bug

I've built docker container for this example https://github.com/microsoft/botbuilder-python/tree/master/samples/06.using-cards Localy with Bot Framework Emulator everything works fine, but when I am trying to run this container inside Linux Web App in Azure I get "There was an error sending this message to your bot: HTTP status code UnsupportedMediaType" in every channel (WebChat, Telegram, Teams etc.)

btw, if I connect from Bot Framework Emulator to container in Linux Web App - it works.

Dockerfile

FROM python:3
COPY ./app /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 3978
CMD [ "python", "-u", "main.py" ]`

[bug]

masyanru commented 5 years ago

I thought the problem with docker container, but today I created the Linux Web App, push python code from another example (https://github.com/microsoft/botbuilder-python/tree/master/samples/45.state-management) to the WebApp, created start.txt with gunicorn --bind 0.0.0.0 --timeout 600 app:app and you know what? In Bot Emulator working perfectly and I can connect remotely, but in "Test in Web Chat" in Azure Portal I've got the same error "HTTP status code UnsupportedMediaType". So weird.

Screenshot 2019-09-12 at 19 49 45

Screenshot 2019-09-12 at 19 50 48

Screenshot 2019-09-12 at 19 51 00

mdrichardson commented 5 years ago

@masyanru What steps are you taking to deploy the Linux Web App?

masyanru commented 5 years ago

@mdrichardson

az group create --name myResourceGroup --location "westeurope"

az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku B1 --is-linux

az webapp create --resource-group myResourceGroup --plan myAppServicePlan --name <APP_NAME> --runtime "PYTHON|3.7" --deployment-local-git

az bot create --resource-group myResourceGroup --name myPyhtonBot --kind registration --endpoint https://<APP_NAME>.azurewebsites.net/api/messages --appid <APP_ID> --password <PASSWROD>

then created startup.txt gunicorn --bind 0.0.0.0 --timeout 600 app:app

after push the code to the web app Screenshot 2019-09-12 at 20 56 59

Screenshot 2019-09-12 at 20 58 15

Screenshot 2019-09-12 at 20 58 29

and that's all

mdrichardson commented 5 years ago

Thanks. I'll start debugging and see if I can help.

This might take awhile as this is pretty rare because we almost never see support issues for:

tsuwandy commented 5 years ago

@mdrichardson, could you sync with @daveta as he's also looking into this. Thanks!

mdrichardson commented 5 years ago

@masyanru Figured it out and honestly, took me wayyy longer than I care to admit. You can see that both of the samples that you tried had this issue.

In app.py, implement this diff:

@app.route("/api/messages", methods=["POST"])
def messages():
    """Main bot message handler."""
-   if request.headers["Content-Type"] == "application/json":
+   if "application/json" in request.headers["Content-Type"]:
        body = request.json
    else:
        return Response(status=415)
mdrichardson commented 5 years ago

@axelsrz @daveta

Does my comment above only apply to deployed bots? Just Linux bots?

Basically, what I noticed was that request.headers["Content-Type"] came in as

application/json; charset=utf-8

...which caused the 415: UnsupportedMediaType to throw.

As this issue was my only experience with the Python SDK, I'm not comfortable submitting a PR (unless you just want my above changes in each sample), but let me know if you need additional context.

Un-assigning myself from this one.

carlosscastro commented 5 years ago

@axelsrz @daveta Any updates here?

axelsrz commented 5 years ago

We should take that change in the samples, @mdrichardson if you want to submit the PRs go ahead, if not I can do it.

mdrichardson commented 5 years ago

@axelsrz Would the PR only need that line changed in each sample? I'm comfortable doing that...just haven't work in Python enough to do much else. But if that's all that's needed, let me know and I'll work up a PR.

mdrichardson commented 5 years ago

I'll work on the PR this week