Skip to content

Commit

Permalink
Updated generators to use CloudAdapter and latest version (#2094)
Browse files Browse the repository at this point in the history
* Updated generators to use CloudAdapter and latest version

* Updated version and added missing SingleTenant config items

---------

Co-authored-by: Tracy Boehrer <trboehre@microsoft.com>
  • Loading branch information
tracyboehrer and Tracy Boehrer authored Apr 17, 2024
1 parent 48ba78e commit 2407c51
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
from datetime import datetime

from botbuilder.core import (
BotFrameworkAdapter,
BotFrameworkAdapterSettings,
ConversationState,
TurnContext,
)
from botbuilder.integration.aiohttp import CloudAdapter, ConfigurationBotFrameworkAuthentication
from botbuilder.schema import ActivityTypes, Activity


class AdapterWithErrorHandler(BotFrameworkAdapter):
class AdapterWithErrorHandler(CloudAdapter):
def __init__(
self,
settings: BotFrameworkAdapterSettings,
settings: ConfigurationBotFrameworkAuthentication,
conversation_state: ConversationState,
):
super().__init__(settings)
Expand Down
14 changes: 7 additions & 7 deletions generators/app/templates/core/{{cookiecutter.bot_name}}/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
- Handle user interruptions for such things as `Help` or `Cancel`.
- Prompt for and validate requests for information from the user.
"""

from http import HTTPStatus
from aiohttp import web
from aiohttp.web import Request, Response, json_response
from botbuilder.core import (
BotFrameworkAdapterSettings,
ConversationState,
MemoryStorage,
UserState,
)
from botbuilder.core.integration import aiohttp_error_middleware
from botbuilder.integration.aiohttp import ConfigurationBotFrameworkAuthentication
from botbuilder.schema import Activity

from config import DefaultConfig
Expand All @@ -31,7 +31,7 @@

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

# Create MemoryStorage, UserState and ConversationState
MEMORY = MemoryStorage()
Expand All @@ -49,21 +49,21 @@
BOT = DialogAndWelcomeBot(CONVERSATION_STATE, USER_STATE, DIALOG)


# Listen for incoming requests on /api/messages.
# 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)
return Response(status=HTTPStatus.UNSUPPORTED_MEDIA_TYPE)

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)
response = await ADAPTER.process_activity(auth_header, activity, BOT.on_turn)
if response:
return json_response(data=response.body, status=response.status)
return Response(status=201)
return Response(status=HTTPStatus.OK)


APP = web.Application(middlewares=[aiohttp_error_middleware])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class DefaultConfig:
PORT = 3978
APP_ID = os.environ.get("MicrosoftAppId", "")
APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "")
APP_TYPE = os.environ.get("MicrosoftAppType", "MultiTenant")
APP_TENANTID = os.environ.get("MicrosoftAppTenantId", "")
LUIS_APP_ID = os.environ.get("LuisAppId", "")
LUIS_API_KEY = os.environ.get("LuisAPIKey", "")
# LUIS endpoint host name, ie "westus.api.cognitive.microsoft.com"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
botbuilder-integration-aiohttp>=4.15.0
botbuilder-integration-aiohttp>=4.14.8
botbuilder-dialogs>=4.15.0
botbuilder-ai>=4.15.0
botbuilder-ai>=4.14.8
datatypes-date-time>=1.0.0.a2
18 changes: 7 additions & 11 deletions generators/app/templates/echo/{{cookiecutter.bot_name}}/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
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 import TurnContext
from botbuilder.core.integration import aiohttp_error_middleware
from botbuilder.integration.aiohttp import CloudAdapter, ConfigurationBotFrameworkAuthentication
from botbuilder.schema import Activity, ActivityTypes

from bot import MyBot
Expand All @@ -22,9 +20,7 @@

# 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)

ADAPTER = CloudAdapter(ConfigurationBotFrameworkAuthentication(CONFIG))

# Catch-all for errors.
async def on_error(context: TurnContext, error: Exception):
Expand Down Expand Up @@ -66,15 +62,15 @@ async def messages(req: Request) -> Response:
if "application/json" in req.headers["Content-Type"]:
body = await req.json()
else:
return Response(status=415)
return Response(status=HTTPStatus.UNSUPPORTED_MEDIA_TYPE)

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)
response = await ADAPTER.process_activity(auth_header, activity, BOT.on_turn)
if response:
return json_response(data=response.body, status=response.status)
return Response(status=201)
return Response(status=HTTPStatus.OK)


APP = web.Application(middlewares=[aiohttp_error_middleware])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ class DefaultConfig:
PORT = 3978
APP_ID = os.environ.get("MicrosoftAppId", "")
APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "")
APP_TYPE = os.environ.get("MicrosoftAppType", "MultiTenant")
APP_TENANTID = os.environ.get("MicrosoftAppTenantId", "")
18 changes: 7 additions & 11 deletions generators/app/templates/empty/{{cookiecutter.bot_name}}/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
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 import TurnContext
from botbuilder.core.integration import aiohttp_error_middleware
from botbuilder.integration.aiohttp import CloudAdapter, ConfigurationBotFrameworkAuthentication
from botbuilder.schema import Activity, ActivityTypes

from bot import MyBot
Expand All @@ -22,9 +20,7 @@

# 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)

ADAPTER = CloudAdapter(ConfigurationBotFrameworkAuthentication(CONFIG))

# Catch-all for errors.
async def on_error(context: TurnContext, error: Exception):
Expand Down Expand Up @@ -66,15 +62,15 @@ async def messages(req: Request) -> Response:
if "application/json" in req.headers["Content-Type"]:
body = await req.json()
else:
return Response(status=415)
return Response(status=HTTPStatus.UNSUPPORTED_MEDIA_TYPE)

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)
response = await ADAPTER.process_activity(auth_header, activity, BOT.on_turn)
if response:
return json_response(data=response.body, status=response.status)
return Response(status=201)
return Response(status=HTTPStatus.OK)


APP = web.Application(middlewares=[aiohttp_error_middleware])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ class DefaultConfig:
PORT = 3978
APP_ID = os.environ.get("MicrosoftAppId", "")
APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "")
APP_TYPE = os.environ.get("MicrosoftAppType", "MultiTenant")
APP_TENANTID = os.environ.get("MicrosoftAppTenantId", "")

0 comments on commit 2407c51

Please sign in to comment.