Azure Bot throws service unavailable error when testing in web chat

264 views Asked by At

When I test my bot locally via the Bot Emulator it runs fine with no issues, after deploying into Azure I receive this error when trying to Test in Web Chat: There was an error sending this message to your bot: HTTP status code ServiceUnavailable

This is what I've already done:

  • I have ensured my Messaging API is correctly configured.
  • I have ensured my AppID and Password match and are properly configured.
  • I have redeployed (multiple times) and used multiple browsers to test in web chat.

When I message in web-chat I get no response or welcome message: enter image description here

Edit: Now the bot no longer works locally either.. enter image description here

1

There are 1 answers

3
Pravallika KV On BEST ANSWER

To connect and test Azure Bot, few modifications should be done.

  • Use the below code in app.py:

Here, I have added init_func().

import sys
import traceback
from datetime import datetime

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

from bot import MyBot
from config import DefaultConfig

CONFIG = DefaultConfig()

SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD)
ADAPTER = BotFrameworkAdapter(SETTINGS)


async def on_error(context: TurnContext, error: Exception):

    print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr)
    traceback.print_exc()

    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."
    )

    if context.activity.channel_id == "emulator":

        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",
        )
        
        await context.send_activity(trace_activity)

ADAPTER.on_turn_error = on_error

BOT = MyBot()

async def messages(req: Request) -> Response:

    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)

# Modifying the below code

def init_func(argv):
    APP = web.Application(middlewares=[aiohttp_error_middleware])
    APP.router.add_post("/api/messages", messages)
    return APP
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

  • Create Azure Bot and Azure App Service.

  • Copy Microsoft App ID value.

  • Click on Manage Password in the configuration, create new secret and copy its value to use it as App Password in the code.

  • Open Config.py in your local Echo Bot project and paste the App_ID and App_Password which were copied as mentioned above.

import os
class DefaultConfig:
    """ Bot Configuration """

    PORT = 3978
    APP_ID = os.environ.get("MicrosoftAppId", "<Your_App_ID>")
    APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "<Your_App-Password>")
  • Deploy the application to Azure App Service.
  • After successful deployment, navigate to your Web App=>Configuration=>General Settings=>Startup Command, update the command python3 -m aiohttp.web -H 0.0.0.0 -P 8000 app:init_func.

enter image description here

  • Go to Azure Bot=>Test in Web chat to test the connection:

https://i.imgur.com/2Yymb0y.png