After deploying my Python Bot in azure its not working
I am trying to create an Azure bot using the Python Azure sdk framework and deploy it on azure app service azure bot. The bot works well on my local with emulator (windows laptop). But once I deploy it does not work in the test web chat option in Azure.
My app.py file code –
import sqlite3
from pathlib import Path
import sys
import traceback
from datetime import datetime
from googleai import ai
from aiohttp import web
from aiohttp.web import Request, Response, json_response
from botbuilder.core import (
BotFrameworkAdapterSettings,
TurnContext,
BotFrameworkAdapter,
ConversationState,
UserState,
MemoryStorage
)
from botbuilder.core.integration import aiohttp_error_middleware
from botbuilder.schema import Activity, ActivityTypes
from bot import MyBot
from config import DefaultConfig
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()
# if(context.activity.text==””)
await context.send_activity(
“i didnt understand what you mean ,pls rewrite your questin with more info what you looking for”
)
# 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
def create_database():
conn = sqlite3.connect(‘Chinook.db’)
speech_file_path = Path(__file__).parent / “Chinook_Sqlite.sql”
with open(speech_file_path, ‘r’,encoding=’cp1252′, errors=’replace’) as f:
sql_script = f.read()
conn.executescript(sql_script)
conn.close()
memstore = MemoryStorage()
constate = ConversationState(memstore)
userstate = UserState(memstore)
# Create the Bot
BOT = MyBot(constate,userstate,CONFIG.EXPIRE_AFTER_SECONDS)
# 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)
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
Startup command is – python3 -m aiohttp.web -H 0.0.0.0 -P 8000 app:init_func
Error –
IN App Service -> Log stream –
Container bot-webapp01_0_aa92351a for site bot-webapp01 has exited, failing site start
Container bot-webapp01_0_aa92351a didn’t respond to HTTP pings on port: 8000, failing site start. See container logs for debugging.
I am trying to create an Azure bot using the Python Azure sdk framework and deploy it on azure app service azure bot. The bot works well on my local with emulator (windows laptop). But once I deploy it does not work in the test web chat option in Azure.My app.py file code -import sqlite3
from pathlib import Path
import sys
import traceback
from datetime import datetime
from googleai import ai
from aiohttp import web
from aiohttp.web import Request, Response, json_response
from botbuilder.core import (
BotFrameworkAdapterSettings,
TurnContext,
BotFrameworkAdapter,
ConversationState,
UserState,
MemoryStorage
)
from botbuilder.core.integration import aiohttp_error_middleware
from botbuilder.schema import Activity, ActivityTypes
from bot import MyBot
from config import DefaultConfig
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()
# if(context.activity.text==””)
await context.send_activity(
“i didnt understand what you mean ,pls rewrite your questin with more info what you looking for”
)
# 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
def create_database():
conn = sqlite3.connect(‘Chinook.db’)
speech_file_path = Path(__file__).parent / “Chinook_Sqlite.sql”
with open(speech_file_path, ‘r’,encoding=’cp1252′, errors=’replace’) as f:
sql_script = f.read()
conn.executescript(sql_script)
conn.close()
memstore = MemoryStorage()
constate = ConversationState(memstore)
userstate = UserState(memstore)
# Create the Bot
BOT = MyBot(constate,userstate,CONFIG.EXPIRE_AFTER_SECONDS)
# 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)
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 errorStartup command is – python3 -m aiohttp.web -H 0.0.0.0 -P 8000 app:init_func Error – IN App Service -> Log stream -Container bot-webapp01_0_aa92351a for site bot-webapp01 has exited, failing site startContainer bot-webapp01_0_aa92351a didn’t respond to HTTP pings on port: 8000, failing site start. See container logs for debugging. Read More