【Azure Bot Service】部署Python ChatBot代码到App Service中
问题描述
使用Python编写了ChatBot,在部署到App Service,却无法启动。 通过高级工具(Kudu站点:https://<your site name>.scm.chinacloudsites.cn/newui)查看日志显示: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: http://aka.ms/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/update.d...
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/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite
2024-10-25T02:43:48.070131739Z Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. 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/output.tar.gz' 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/startup.sh'
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.
问题解答
根据下面的步骤修改app.py代码并设置App Service的启动命令。
第一步 : 在 app.py 中添加 init_func 函数
Python ChatBot的实例代码下载地址(创建机器人Python版:https://docs.azure.cn/zh-cn/bot-service/bot-service-quickstart-create-bot?view=azure-bot-service-4.0&tabs=python%2Cvs#create-a-bot)
app.py
# Create the Bot
BOT = EchoBot() # Listen for incoming requests on /api/messages
async def messages(req: Request) -> Response:
return await ADAPTER.process(req, BOT) ## 从这里开始修改,添加 init_func 启动函数 def init_func(argv):
APP = web.Application(middlewares=[aiohttp_error_middleware])
APP.router.add_post("/api/messages", messages)
return APP # APP = web.Application(middlewares=[aiohttp_error_middleware])
# APP.router.add_post("/api/messages", messages) if __name__ == "__main__":
APP = init_func(None)
try:
web.run_app(APP, host="0.0.0.0", port=CONFIG.PORT)
except Exception as error:
raise error
第二步:在Config.py中添加配置项和部署到App Service中
添加的配置项是与中国区Azure Bot Service进行认证的配置项,说明需要参考如下两部分内容:
2: 机器人在中国区的身份验证设置 : https://docs.azure.cn/zh-cn/bot-service/how-to-deploy-china-cloud?view=azure-bot-service-4.0&tabs=javascript#configure-userassignedmsisingletenant-bot
config.py
#!/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 = os.environ.get("MicrosoftAppId", "app id")
APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "secret")
APP_TYPE = os.environ.get("MicrosoftAppType", "SingleTenant")
APP_TENANTID = os.environ.get("MicrosoftAppTenantId", "tenant id") OAUTH_URL = os.environ.get("OAuthUrl", "https://token.botframework.azure.cn/")
TO_BOT_FROM_CHANNEL_TOKEN_ISSUER = os.environ.get("ToBotFromChannelTokenIssuer", "https://api.botframework.azure.cn") TO_BOT_FROM_CHANNEL_OPENID_METADATA_URL = os.environ.get("ToBotFromChannelOpenIdMetadataUrl", "https://login.botframework.azure.cn/v1/.well-known/openidconfiguration")
TO_BOT_FROM_EMULATOR_OPENID_METADATA_URL = os.environ.get("ToBotFromEmulatorOpenIdMetadataUrl", "https://login.partner.microsoftonline.cn/a55a4d5b-9241-49b1-b4ff-befa8db00269/v2.0/.well-known/openid-configuration")
VALIDATE_AUTHORITY = os.environ.get("ValidateAuthority", "true")
TO_CHANNEL_FROM_BOT_LOGIN_URL = os.environ.get("ToChannelFromBotLoginUrl","https://login.partner.microsoftonline.cn/<tenant id>")
TO_CHANNEL_FROM_BOT_OAUTH_SCOPE = os.environ.get("ToChannelFromBotOAuthScope", "https://api.botframework.azure.cn")
requirements.txt:
botbuilder-integration-aiohttp>=4.15.0
aiohttp
botbuilder-core
botbuilder-schema
修改完成后,部署Python应用到App Service。

第三步: 为App Service添加启动命令
进入App Service配置页面,设置启动命令。
python3 -m aiohttp.web -H 0.0.0.0 -P 8000 app:init_func

修改后,查看日志,应用启动成功!

参考资料
Azure ChatBot Running With Python on an Azure WebApp Not Working : https://stackoverflow.com/questions/77781014/azure-chatbot-running-with-python-on-an-azure-webapp-not-working
【Azure Bot Service】部署Python ChatBot代码到App Service中的更多相关文章
- 【Azure 应用程序见解】 Application Insights 对App Service的支持问题
问题描述 Web App 发布后, Application Insights 收集不到数据了 问题分析 在应用服务(App Service)中收集应用的监控数据(如Request,Exception, ...
- 【应用服务 App Service】快速获取DUMP文件(App Service for Windows(.NET/.NET Core))
问题情形 当应用在Azure 应用服务App Service中运行时,有时候出现CPU,Memory很高,但是没有明显的5XX错误和异常日志,有时就是有异常但是也不能明确的指出具体的代码错误.当面临这 ...
- 【应用服务 App Service】NodeJS +Egg 发布到App Service时遇见 [ERR_SYSTEM_ERROR]: A system error occurred:uv_os_get_passwd returned ENOENT(no such file or directory)
问题情形 本地NodeJS应用使用Egg脚手架构建,本地运行测试完全没有问题,发布后App Service后不能运行.通过登录到kudu后(https://<your web site>. ...
- 【从0開始Tornado建站】0.9版本号python站点代码开源--持续更新中
从5月份開始[从0開始Tornado建站]这个专栏,開始一点一点把这个分类兴趣站点弄起来,从无到有的过程也是令人兴奋的:-) 国庆的时候等待备案然后上线,如今站点域名为ustchack ...
- 【Azure 应用服务】Python flask 应用部署在Aure App Service中作为一个子项目时,解决遇见的404 Not Found问题
问题描述 在成功的部署Python flask应用到App Service (Windows)后,如果需要把当前项目(如:hiflask)作为一个子项目(子站点),把web.config文件从wwwr ...
- 【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
问题描述 在上篇博文"[Azure 应用服务]App Service for Linux 中实现 WebSocket 功能 (Python SocketIO)"中,实现了通过 HT ...
- 【Azure 应用服务】一个 App Service 同时部署运行两个及多个 Java 应用程序(Jar包)
问题描述 如何在一个AppService下同时部署运行多个Java 应用程序呢? 问题解答 因为App Service的默认根目录为 wwwroot.如果需要运行多个Java 应用程序,需要在 www ...
- 【Azure 应用服务】NodeJS Express + MSAL 应用实现AAD集成登录并部署在App Service Linux环境中的实现步骤
问题描述 实现部署NodeJS Express应用在App Service Linux环境中,并且使用Microsoft Authentication Library(MSAL)来实现登录Azure ...
- 【Azure 应用服务】PHP应用部署在App Service for Linux环境中,上传文件大于1MB时,遇见了413 Request Entity Too Large 错误的解决方法
问题描述 在PHP项目部署在App Service后,上传文件如果大于1MB就会遇见 413 Request Entity Too Large 的问题. 问题解决 目前这个问题,首先需要分析应用所在的 ...
- 【Azure 应用服务】App Service For Linux 部署PHP Laravel 项目,如何修改首页路径为 wwwroot\public\index.php
问题描述 参考官方文档部署 PHP Laravel 项目到App Service for Linux环境中,但是访问应用时候遇见了500 Server Error 错误. 从部署的日志中,可以明确看出 ...
随机推荐
- 9组-Alpha冲刺-6/4
一.基本情况 队名:不行就摆了吧 组长博客:https://www.cnblogs.com/Microsoft-hc/p/15546712.html 小组人数: 8 二.冲刺概况汇报 卢浩玮 过去两天 ...
- Aspire8一文通
Aspire8一文通 0.简介 微软Aspire是微软今年推出的一个全新的平台无关.语言无关的新框架,它的设计目的是简化云原生应用的开发.部署和管理过程.Aspire的读音是[əˈspaɪər],它的 ...
- 微服务架构springcloud
码云地址:https://gitee.com/lpxs/lp-springcloud.git 有问题可以多沟通:136358344@qq.com. 微服务架构 一.服务化简介 服务化的核心就是将传统的 ...
- Atcoder ABC364 D-F
Atcoder ABC364 D-F D - K-th Nearest 链接: D - K-th Nearest (atcoder.jp) 简要题意: 问题陈述 在一条数线上有 \(N+Q\) 个点 ...
- 2024-08-17:用go语言,给定一个从0开始的整数数组nums和一个整数k, 每次操作可以删除数组中的最小元素。 你的目标是通过这些操作,使得数组中的所有元素都大于或等于k。 请计算出实现这个目
2024-08-17:用go语言,给定一个从0开始的整数数组nums和一个整数k, 每次操作可以删除数组中的最小元素. 你的目标是通过这些操作,使得数组中的所有元素都大于或等于k. 请计算出实现这个目 ...
- RISC-V全志D1多媒体套件文章汇总
提示 此开发板的任何问题都可以在我们的论坛交流讨论 https://forums.100ask.net/c/aw/d1/57 文章目录汇总 教程共计14章,下面是章节汇总: 第0章_RISC-V全志D ...
- 去除WinForm程序中的Devexpress弹窗
去除WinForm程序中的Devexpress弹窗 /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static ...
- 【YashanDB知识库】update/delete未选中行时,v$transaction视图没有事务,alter超时问题
问题现象 1.alter table修改表字段名,卡住,超时. 2.查看v$transaction事务视图,没有看到事务记录. 3.问题单:调整表结构时超时 问题风险及影响 无风险 问题影响版本 客户 ...
- LaTex “too many unprocessed floats”
latex编辑时出现LaTex "too many unprocessed floats" 如何解决? 有人说是用/usepackage[section] {placeins} 我 ...
- ASP.NET Core C# 反射 & 表达式树 (第一篇)
前言 以前就写过几篇关于反射和表达式树的学习笔记, 但是写的很乱. 最近常用到反射和表达式树, 所以特别写一篇做一个整理吧. 泛型和反射 表达式树 学习笔记 c# 常用反射和表达式树整理 反射在项目中 ...