init: Initial commit
This commit is contained in:
47
handler.py
Normal file
47
handler.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from botbuilder.core import TurnContext
|
||||
from botbuilder.integration.aiohttp import CloudAdapter
|
||||
|
||||
from aiohttp.web import Request, Response, json_response
|
||||
|
||||
from bot import WebhookBot
|
||||
from config import DefaultConfig
|
||||
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
class BotHandler:
|
||||
def __init__(self, adapter: CloudAdapter, bot: WebhookBot, config: DefaultConfig) -> None:
|
||||
self.adapter = adapter
|
||||
self.bot = bot
|
||||
|
||||
async def messages(self, req: Request) -> Response:
|
||||
try:
|
||||
# Log incoming request
|
||||
print(f"[Handler] Received request at /api/messages")
|
||||
print(f"[Handler] Request method: {req.method}")
|
||||
print(f"[Handler] Content-Type: {req.headers.get('Content-Type', 'None')}")
|
||||
print(f"[Handler] Content-Length: {req.headers.get('Content-Length', 'None')}")
|
||||
|
||||
# Process the request - adapter will read the body
|
||||
print(f"[Handler] Processing request with adapter...")
|
||||
response = await self.adapter.process(req, self.bot)
|
||||
if response is not None:
|
||||
print(f"[Handler] Adapter returned response: {response.status}")
|
||||
return response
|
||||
print(f"[Handler] Adapter returned None, sending 204")
|
||||
return json_response(status=204)
|
||||
except Exception as e:
|
||||
print(f"[Handler] Error processing request: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return json_response(status=500)
|
||||
|
||||
async def on_error(self, context: TurnContext, error: Exception) -> None:
|
||||
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."
|
||||
)
|
||||
return
|
||||
Reference in New Issue
Block a user