45 lines
1.7 KiB
Python
45 lines
1.7 KiB
Python
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:
|
|
# Process the request - adapter will read the body
|
|
response = await self.adapter.process(req, self.bot)
|
|
if response is not None:
|
|
return response
|
|
return json_response(status=204)
|
|
except Exception as e:
|
|
import traceback
|
|
traceback.print_exc()
|
|
return json_response(status=500)
|
|
|
|
async def api_test(self, req: Request) -> Response:
|
|
try:
|
|
body = await req.json()
|
|
# Send a message with the bot to the channel
|
|
self.bot.send_message(body.get("channel_id", "00000000-0000-0000-0000-000000000000"), body.get("message", "Hello, world!"))
|
|
return json_response(status=200, data={"message": "Message sent successfully"})
|
|
except Exception as e:
|
|
return json_response(status=500, data={"message": "Error sending message"})
|
|
|
|
async def on_error(self, context: TurnContext, error: Exception) -> None:
|
|
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 |