Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
9 changes: 9 additions & 0 deletions test_samples/tethered-agent/env.TEMPLATE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CONNECTIONS__SERVICE_CONNECTION__SETTINGS__CLIENTID=
CONNECTIONS__SERVICE_CONNECTION__SETTINGS__CLIENTSECRET=
CONNECTIONS__SERVICE_CONNECTION__SETTINGS__TENANTID=

LOGGING__LOGLEVEL__microsoft_agents=DEBUG

CONNECTIONSMAP__0__SERVICEURL=*
CONNECTIONSMAP__0__CONNECTION=SERVICE_CONNECTION

7 changes: 7 additions & 0 deletions test_samples/tethered-agent/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
python-dotenv
aiohttp
microsoft-agents-hosting-aiohttp
microsoft-agents-hosting-core
microsoft-agents-authentication-msal
microsoft-agents-activity
tethered
Empty file.
54 changes: 54 additions & 0 deletions test_samples/tethered-agent/src/agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import aiohttp

from os import environ, path
Comment thread
rodrigobr-msft marked this conversation as resolved.
from dotenv import load_dotenv
from microsoft_agents.activity import load_configuration_from_env
from microsoft_agents.authentication.msal import (
MsalConnectionManager,
)
from microsoft_agents.hosting.core import (
AgentApplication,
TurnState,
TurnContext,
MemoryStorage,
)
from microsoft_agents.hosting.aiohttp import CloudAdapter
from microsoft_agents.hosting.core.app.oauth.authorization import Authorization

load_dotenv()

agents_sdk_config = load_configuration_from_env(environ)

STORAGE = MemoryStorage()
CONNECTION_MANAGER = MsalConnectionManager(**agents_sdk_config)
ADAPTER = CloudAdapter(connection_manager=CONNECTION_MANAGER)

AUTHORIZATION = Authorization(STORAGE, CONNECTION_MANAGER, **agents_sdk_config)

AGENT_APP = AgentApplication[TurnState](
storage=STORAGE, adapter=ADAPTER, authorization=AUTHORIZATION, **agents_sdk_config
)


@AGENT_APP.conversation_update("membersAdded")
@AGENT_APP.message("/help")
async def _help(context: TurnContext, _state: TurnState):
await context.send_activity(
"Welcome to the Echo Agent sample 🚀. "
"Type /help for help or send a message to see the echo feature in action."
)

@AGENT_APP.message("/get")
async def _get(context: TurnContext, _state: TurnState):
async with aiohttp.ClientSession() as session:
async with session.get("https://www.bing.com") as response:
await context.send_activity(
f"This is a GET request. You can use this to retrieve information. Status: {response.status}"
)

@AGENT_APP.activity("message")
async def on_message(context: TurnContext, _):
await context.send_activity(f"you said: {context.activity.text}")
16 changes: 16 additions & 0 deletions test_samples/tethered-agent/src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import tethered

tethered.activate(allow=[
"login.microsoftonline.com",
"login.botframework.*",
"api.botframework.*",
"www.botframework.",
])
Comment thread
rodrigobr-msft marked this conversation as resolved.

from .agent import AGENT_APP, CONNECTION_MANAGER
from .start_server import start_server

start_server(
agent_application=AGENT_APP,
auth_configuration=CONNECTION_MANAGER.get_default_connection_configuration(),
)
32 changes: 32 additions & 0 deletions test_samples/tethered-agent/src/start_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from os import environ
from microsoft_agents.hosting.core import AgentApplication, AgentAuthConfiguration
from microsoft_agents.hosting.aiohttp import (
start_agent_process,
jwt_authorization_middleware,
CloudAdapter,
)
from aiohttp.web import Request, Response, Application, run_app


def start_server(
agent_application: AgentApplication, auth_configuration: AgentAuthConfiguration
):
async def entry_point(req: Request) -> Response:
agent: AgentApplication = req.app["agent_app"]
adapter: CloudAdapter = req.app["adapter"]
return await start_agent_process(
req,
agent,
adapter,
)

APP = Application(middlewares=[jwt_authorization_middleware])
APP.router.add_post("/api/messages", entry_point)
APP["agent_configuration"] = auth_configuration
APP["agent_app"] = agent_application
APP["adapter"] = agent_application.adapter

try:
run_app(APP, host="localhost", port=environ.get("PORT", 3978))
except Exception as error:
raise error
Loading