使用 Bot 框架部署 Azure Web 应用程序:解决“无法找到属性‘app’”错误

问题描述 投票:0回答:1

我已在 Azure Web App 上使用 Bot Framework 部署了 Python 代码库,但遇到错误:“无法在‘app’中找到属性‘app’。”启动应用程序的代码遵循标准的 Microsoft 代码示例。

这是代码片段:

import sys
import traceback
from datetime import datetime
from http import HTTPStatus

from aiohttp import web
from aiohttp.web import Request, Response, json_response
from botbuilder.core import (
    BotFrameworkAdapterSettings,
    TurnContext,
    BotFrameworkAdapter,
)
from botbuilder.core.integration import aiohttp_error_middleware
from botbuilder.schema import Activity, ActivityTypes

from bot import MyBot
from config import DefaultConfig
from openaibot.openaibot import OpenAIBot

CONFIG = DefaultConfig()

# Create adapter.
# See https://aka.ms/about-bot-adapter to learn more about how bots work.
SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD)
ADAPTER = BotFrameworkAdapter(SETTINGS)


# Catch-all for errors.
async def on_error(context: TurnContext, error: Exception):
    # This check writes out errors to console log .vs. app insights.
    # NOTE: In production environment, you should consider logging this to Azure
    #       application insights.
    print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr)
    traceback.print_exc()

    # Send a message to the user
    await context.send_activity("The bot encountered an error or bug.")
    await context.send_activity(
        "To continue to run this bot, please fix the bot source code."
    )
    # Send a trace activity if we're talking to the Bot Framework Emulator
    if context.activity.channel_id == "emulator":
        # Create a trace activity that contains the error object
        trace_activity = Activity(
            label="TurnError",
            name="on_turn_error Trace",
            timestamp=datetime.utcnow(),
            type=ActivityTypes.trace,
            value=f"{error}",
            value_type="https://www.botframework.com/schemas/error",
        )
        # Send a trace activity, which will be displayed in Bot Framework Emulator
        await context.send_activity(trace_activity)


ADAPTER.on_turn_error = on_error

# Create the Bot
#BOT = MyBot()
OAIBOT = OpenAIBot(CONFIG)
BOT = MyBot(OAIBOT)



# Listen for incoming requests on /api/messages
async def messages(req: Request) -> Response:
    # Main bot message handler.
    if "application/json" in req.headers["Content-Type"]:
        body = await req.json()
    else:
        return  Response(status=415) 

    activity = Activity().deserialize(body)
    auth_header = req.headers["Authorization"] if "Authorization" in req.headers else ""

    response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn)
    if response:
        return json_response(data=response.body, status=response.status)
    return Response(status=201)


#####
APP = web.Application(middlewares=[aiohttp_error_middleware])
APP.router.add_post("/api/messages", messages)

if __name__ == "__main__":
    try:
        web.run_app(APP, host="localhost", port=CONFIG.PORT)
    except Exception as error:
        raise error

该错误似乎与

web.run_app
部分有关。我不确定为什么找不到“app”属性。

对于可能导致此问题的原因有什么见解或建议吗?部署到 Azure Web App 时,是否需要进行不同的特定配置?我非常感谢任何有关解决此问题的帮助或指导。

python azure bots
1个回答
1
投票

发布我的评论作为答案对社区来说是一件好事。

成功部署到 Azure Web 应用程序后,您应该在启动命令 > 配置 > 常规设置 > 启动命令 > python3 -m aiohttp.web -H 0.0.0.0 -P 8000 app 中添加以下内容: Azure Web 应用程序中的 init_func 如下所示。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.