Description of the problem
Wrote ChatBot in Python and after deploying it to App Service, it doesn't start. Looking at the logs via advanced tools (Kudu site: https://<your site name>. /newui) the log shows: Failed to find attribute 'app' in 'app'.
2024-10-25T02:43:29.242073529Z _____
2024-10-25T02:43:29.242126029Z / _ \ __________ _________ ____
2024-10-25T02:43:29.242132529Z / /_\ \\___ / | \_ __ \_/ __ \
2024-10-25T02:43:29.242136329Z / | \/ /| | /| | \/\ ___/
2024-10-25T02:43:29.242139929Z \____|__ /_____ \____/ |__| \___ >
2024-10-25T02:43:29.242144329Z \/ \/ \/
2024-10-25T02:43:29.242147829Z A P P S E R V I C E O N L I N U X
2024-10-25T02:43:29.242151329Z
2024-10-25T02:43:29.242154629Z Documentation: /webapp-linux
2024-10-25T02:43:29.242157929Z Python 3.9.19
2024-10-25T02:43:29.242161329Z Note: Any data outside '/home' is not persisted
2024-10-25T02:43:30.929950845Z Starting OpenBSD Secure Shell server: sshd.
2024-10-25T02:43:30.957953290Z WEBSITES_INCLUDE_CLOUD_CERTS is not set to true.
2024-10-25T02:43:31.046614933Z Updating certificates in /etc/ssl/certs...
2024-10-25T02:43:47.356555353Z 1 added, 0 removed; done.
2024-10-25T02:43:47.363583943Z Running hooks in /etc/ca-certificates/...
2024-10-25T02:43:47.381370217Z done.
2024-10-25T02:43:47.458519508Z CA certificates copied and updated successfully.
2024-10-25T02:43:47.764113974Z App Command Line not configured, will attempt auto-detect
2024-10-25T02:43:47.766294671Z Launching oryx with: create-script -appPath /home/site/wwwroot -output /opt/startup/ -virtualEnvName antenv -defaultApp /opt/defaultsite
2024-10-25T02:43:48.070131739Z Found build manifest file at '/home/site/wwwroot/'. Deserializing it...
2024-10-25T02:43:48.117325272Z Build Operation ID: 1a78e454a2c951e6
2024-10-25T02:43:48.219390927Z Output is compressed. Extracting it...
2024-10-25T02:43:48.219435627Z Extracting '/home/site/wwwroot/' to directory '/tmp/8dcf49cf7434c99'...
2024-10-25T02:43:48.221347324Z Oryx Version: 0.2.20240619.2, Commit: cf006407a02b225f59dccd677986973c7889aa50, ReleaseTagName: 20240619.2
2024-10-25T02:43:58.209300259Z App path is set to '/tmp/8dcf49cf7434c99'
2024-10-25T02:44:00.187313274Z Detected an app based on Flask
2024-10-25T02:44:00.187408174Z Generating `gunicorn` command for 'app:app'
2024-10-25T02:44:00.433932149Z Writing output script to '/opt/startup/'
2024-10-25T02:44:00.699343416Z Using packages from virtual environment antenv located at /tmp/8dcf49cf7434c99/antenv.
2024-10-25T02:44:00.706651312Z Updated PYTHONPATH to '/opt/startup/app_logs:/tmp/8dcf49cf7434c99/antenv/lib/python3.9/site-packages'
2024-10-25T02:44:03.601118861Z [2024-10-25 02:44:03 +0000] [1064] [INFO] Starting gunicorn 22.0.0
2024-10-25T02:44:03.744989087Z [2024-10-25 02:44:03 +0000] [1064] [INFO] Listening at: http://0.0.0.0:8000 (1064)
2024-10-25T02:44:03.746948887Z [2024-10-25 02:44:03 +0000] [1064] [INFO] Using worker: sync
2024-10-25T02:44:03.849806606Z [2024-10-25 02:44:03 +0000] [1067] [INFO] Booting worker with pid: 1067
2024-10-25T02:44:10.688674133Z Failed to find attribute 'app' in 'app'.
2024-10-25T02:44:10.696985330Z [2024-10-25 02:44:10 +0000] [1067] [INFO] Worker exiting (pid: 1067)
2024-10-25T02:44:11.222634199Z [2024-10-25 02:44:11 +0000] [1064] [ERROR] Worker (pid:1067) exited with code 4
2024-10-25T02:44:11.222689699Z [2024-10-25 02:44:11 +0000] [1064] [ERROR] Shutting down: Master
2024-10-25T02:44:11.222696699Z [2024-10-25 02:44:11 +0000] [1064] [ERROR] Reason: App failed to load.
Problem solving
Follow the steps below to modify the code and set the App Service startup command.
Step 1 : Add the init_func function to the
Python ChatBot example code download address (create bot Python version:/zh-cn/bot-service/bot-service-quickstart-create-bot?view=azure-bot-service-4.0&tabs=python%2Cvs#create-a-bot)
# Create the Bot BOT = EchoBot() # Listen for incoming requests on /api/messages async def messages(req: Request) -> Response: return await (req, BOT) ## Modify from here to add init_func startup function def init_func(argv): APP = (middlewares=[aiohttp_error_middleware]) .add_post("/api/messages", messages) return APP # APP = (middlewares=[aiohttp_error_middleware]) # .add_post("/api/messages", messages) if __name__ == "__main__": APP = init_func(None) try: web.run_app(APP, host="0.0.0.0", port=) except Exception as error: raise error
Step 2: Add Configuration Items and Deploy to App Service
The added configuration item is for authentication with Azure Bot Service in China, the description needs to refer to the following two parts.
1: Robot identification information:/zh-cn/bot-service/v4sdk/bot-builder-authentication?view=azure-bot-service-4.0&tabs=singletenant%2Caadv2%2Cpython#bot-identity-information
2: Robot authentication settings in China./zh-cn/bot-service/how-to-deploy-china-cloud?view=azure-bot-service-4.0&tabs=javascript#configure-userassignedmsisingletenant-bot
#!/usr/bin/env python3 # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. import os """ Bot Configuration """ class DefaultConfig: """ Bot Configuration """ PORT = 3978 APP_ID = ("MicrosoftAppId", "app id") APP_PASSWORD = ("MicrosoftAppPassword", "secret") APP_TYPE = ("MicrosoftAppType", "SingleTenant") APP_TENANTID = ("MicrosoftAppTenantId", "tenant id") OAUTH_URL = ("OAuthUrl", "/") TO_BOT_FROM_CHANNEL_TOKEN_ISSUER = ("ToBotFromChannelTokenIssuer", "") TO_BOT_FROM_CHANNEL_OPENID_METADATA_URL = ("ToBotFromChannelOpenIdMetadataUrl", "/v1/.well-known/openidconfiguration") TO_BOT_FROM_EMULATOR_OPENID_METADATA_URL = ("ToBotFromEmulatorOpenIdMetadataUrl", "/a55a4d5b-9241-49b1-b4ff-befa8db00269/v2.0/.well-known/openid-configuration") VALIDATE_AUTHORITY = ("ValidateAuthority", "true") TO_CHANNEL_FROM_BOT_LOGIN_URL = ("ToChannelFromBotLoginUrl","/<tenant id>") TO_CHANNEL_FROM_BOT_OAUTH_SCOPE = ("ToChannelFromBotOAuthScope", "")
:
botbuilder-integration-aiohttp>=4.15.0 aiohttp botbuilder-core botbuilder-schema
Once the modifications are complete, deploy the Python application to the App Service.
Step 3: Add startup commands for App Service
Go to the App Service configuration page and set the startup command.
python3 -m -H 0.0.0.0 -P 8000 app:init_func
After the modification, check the logs, the application started successfully!
bibliography
Azure ChatBot Running With Python on an Azure WebApp Not Working : /questions/77781014/azure-chatbot-running-with-python-on-an-azure-webapp-not-working