-
Notifications
You must be signed in to change notification settings - Fork 262
[Docs] Add Self-Manging your server section and updating quickstart (TS/PY) #2726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bf14aea
[Docs] Add "Add to an Existing Project" section to Quickstart
heyitsaamir bb92324
[Docs] Add HTTP Server guide, move hosting-static-pages to server sec…
heyitsaamir 6000847
Update teams.md/src/components/include/in-depth-guides/server/http-se…
heyitsaamir 73e1b52
[Docs] Add clarifying comment that Teams only sends POST to bot endpoint
heyitsaamir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
teams.md/src/components/include/in-depth-guides/server/http-server/csharp.incl.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <!-- adapter-interface --> | ||
|
|
||
| N/A | ||
|
|
||
| <!-- self-managed --> | ||
|
|
||
| N/A | ||
|
|
||
| <!-- custom-adapter --> | ||
|
|
||
| N/A |
96 changes: 96 additions & 0 deletions
96
teams.md/src/components/include/in-depth-guides/server/http-server/python.incl.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| <!-- default-framework --> | ||
|
|
||
| [FastAPI](https://fastapi.tiangolo.com/) | ||
|
|
||
| <!-- adapter-interface --> | ||
|
|
||
| ```python | ||
| class HttpServerAdapter(Protocol): | ||
| def register_route(self, method: HttpMethod, path: str, handler: HttpRouteHandler) -> None: ... | ||
| def serve_static(self, path: str, directory: str) -> None: ... | ||
| async def start(self, port: int) -> None: ... | ||
| async def stop(self) -> None: ... | ||
|
|
||
| class HttpRouteHandler(Protocol): | ||
| async def __call__(self, request: HttpRequest) -> HttpResponse: ... | ||
| ``` | ||
|
|
||
| <!-- self-managed --> | ||
|
|
||
| ```python | ||
| import asyncio | ||
| import uvicorn | ||
| from fastapi import FastAPI | ||
| from microsoft_teams.apps import App, FastAPIAdapter | ||
|
|
||
| # 1. Create your FastAPI app with your own routes | ||
| my_fastapi = FastAPI(title="My App + Teams Bot") | ||
|
|
||
| @my_fastapi.get("/health") | ||
| async def health(): | ||
| return {"status": "healthy"} | ||
|
|
||
| # 2. Wrap it in the FastAPIAdapter | ||
| adapter = FastAPIAdapter(app=my_fastapi) | ||
|
|
||
| # 3. Create the Teams app with the adapter | ||
| app = App(http_server_adapter=adapter) | ||
|
|
||
| @app.on_message | ||
| async def handle_message(ctx): | ||
| await ctx.send(f"Echo: {ctx.activity.text}") | ||
|
|
||
| async def main(): | ||
| # 4. Initialize — registers /api/messages on your FastAPI app (does NOT start a server) | ||
| await app.initialize() | ||
|
|
||
| # 5. Start the server yourself | ||
| config = uvicorn.Config(app=my_fastapi, host="0.0.0.0", port=3978) | ||
| server = uvicorn.Server(config) | ||
| await server.serve() | ||
|
|
||
| asyncio.run(main()) | ||
| ``` | ||
|
|
||
| > See the full example: [FastAPI non-managed example](https://github.com/microsoft/teams.py/tree/main/examples/http-adapters/src/fastapi_non_managed.py) | ||
|
|
||
| <!-- custom-adapter --> | ||
|
|
||
| Here is a Starlette adapter — only `register_route` is needed: | ||
|
|
||
| ```python | ||
| from starlette.applications import Starlette | ||
| from starlette.requests import Request | ||
| from starlette.responses import JSONResponse, Response | ||
| from starlette.routing import Route | ||
| from microsoft_teams.apps.http.adapter import HttpMethod, HttpRequest, HttpResponse, HttpRouteHandler | ||
|
|
||
| class StarletteAdapter: | ||
| def __init__(self, app: Starlette): | ||
| self._app = app | ||
|
|
||
| def register_route(self, method: HttpMethod, path: str, handler: HttpRouteHandler) -> None: | ||
| # Teams only sends POST requests to your bot endpoint | ||
| async def starlette_handler(request: Request) -> Response: | ||
| body = await request.json() | ||
| headers = dict(request.headers) | ||
| result: HttpResponse = await handler(HttpRequest(body=body, headers=headers)) | ||
| if result.get("body") is not None: | ||
| return JSONResponse(content=result["body"], status_code=result["status"]) | ||
| return Response(status_code=result["status"]) | ||
|
|
||
| route = Route(path, starlette_handler, methods=[method]) | ||
| self._app.routes.insert(0, route) | ||
| ``` | ||
|
|
||
| Usage: | ||
|
|
||
| ```python | ||
| starlette_app = Starlette() | ||
| adapter = StarletteAdapter(starlette_app) | ||
| app = App(http_server_adapter=adapter) | ||
| await app.initialize() | ||
| # Start Starlette with uvicorn yourself | ||
| ``` | ||
|
|
||
| > See the full implementation: [Starlette adapter example](https://github.com/microsoft/teams.py/tree/main/examples/http-adapters/src/starlette_adapter.py) |
90 changes: 90 additions & 0 deletions
90
...md/src/components/include/in-depth-guides/server/http-server/typescript.incl.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| <!-- default-framework --> | ||
|
|
||
| [Express](https://expressjs.com/) | ||
|
|
||
| <!-- adapter-interface --> | ||
|
|
||
| ```typescript | ||
| interface IHttpServerAdapter { | ||
| registerRoute(method: HttpMethod, path: string, handler: HttpRouteHandler): void; | ||
| serveStatic?(path: string, directory: string): void; | ||
| start?(port: number): Promise<void>; | ||
| stop?(): Promise<void>; | ||
| } | ||
|
|
||
| type HttpRouteHandler = (request: { body: unknown; headers: Record<string, string | string[]> }) | ||
| => Promise<{ status: number; body?: unknown }>; | ||
| ``` | ||
|
|
||
| <!-- self-managed --> | ||
|
|
||
| ```typescript | ||
| import http from 'http'; | ||
| import express from 'express'; | ||
| import { App, ExpressAdapter } from '@microsoft/teams.apps'; | ||
|
|
||
| // 1. Create your Express app with your own routes | ||
| const expressApp = express(); | ||
| const httpServer = http.createServer(expressApp); | ||
|
|
||
| expressApp.get('/health', (_req, res) => { | ||
| res.json({ status: 'healthy' }); | ||
| }); | ||
|
|
||
| // 2. Wrap it in the ExpressAdapter | ||
| const adapter = new ExpressAdapter(httpServer); | ||
|
|
||
| // 3. Create the Teams app with the adapter | ||
| const app = new App({ httpServerAdapter: adapter }); | ||
|
|
||
| app.on('message', async ({ send, activity }) => { | ||
| await send(`Echo: ${activity.text}`); | ||
| }); | ||
|
|
||
| // 4. Initialize — registers /api/messages on your Express app (does NOT start a server) | ||
| await app.initialize(); | ||
|
|
||
| // 5. Start the server yourself | ||
| httpServer.listen(3978, () => console.log('Server ready on http://localhost:3978')); | ||
| ``` | ||
|
|
||
| > See the full [Express adapter example](https://github.com/microsoft/teams.ts/tree/main/examples/http-adapters/express) | ||
|
|
||
| <!-- custom-adapter --> | ||
|
|
||
| Here is a Restify adapter — only `registerRoute` is needed: | ||
|
|
||
| ```typescript | ||
| import restify from 'restify'; | ||
| import { HttpMethod, IHttpServerAdapter, HttpRouteHandler } from '@microsoft/teams.apps'; | ||
|
|
||
| class RestifyAdapter implements IHttpServerAdapter { | ||
| constructor(private server: restify.Server) { | ||
| this.server.use(restify.plugins.bodyParser()); | ||
| } | ||
|
|
||
| registerRoute(method: HttpMethod, path: string, handler: HttpRouteHandler): void { | ||
| // Teams only sends POST requests to your bot endpoint | ||
| assert(method === 'POST', `Unsupported method: ${method}`); | ||
| this.server.post(path, async (req: restify.Request, res: restify.Response) => { | ||
| const response = await handler({ | ||
| body: req.body, | ||
| headers: req.headers as Record<string, string | string[]>, | ||
| }); | ||
| res.send(response.status, response.body); | ||
| }); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Usage: | ||
|
|
||
| ```typescript | ||
| const server = restify.createServer(); | ||
| const adapter = new RestifyAdapter(server); | ||
| const app = new App({ httpServerAdapter: adapter }); | ||
| await app.initialize(); | ||
| server.listen(3978); | ||
| ``` | ||
|
|
||
| > See the full implementation: [Restify adapter example](https://github.com/microsoft/teams.ts/tree/main/examples/http-adapters/restify) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
teams.md/src/pages/templates/in-depth-guides/server/_category_.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "label": "Server", | ||
| "position": 9, | ||
| "collapsible": true, | ||
| "collapsed": true | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.