Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.

Commit f415ddd

Browse files
authored
Naming samples and porting to Flask (#282)
* Ported main samples from aiohttp to Flask. Rename and rearrenge of samples to match BotBuilder-Samples
1 parent 3f78ff3 commit f415ddd

File tree

168 files changed

+302
-2003
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

168 files changed

+302
-2003
lines changed
Lines changed: 7 additions & 2 deletions
File renamed without changes.
File renamed without changes.
Lines changed: 11 additions & 8 deletions

samples/06.using-cards/app.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
"""
5+
This sample shows how to use different types of rich cards.
6+
"""
7+
import asyncio
8+
import sys
9+
10+
from flask import Flask, request, Response
11+
from botbuilder.core import (
12+
BotFrameworkAdapter,
13+
BotFrameworkAdapterSettings,
14+
ConversationState,
15+
MemoryStorage,
16+
TurnContext,
17+
UserState,
18+
)
19+
from botbuilder.schema import Activity
20+
21+
from dialogs import MainDialog
22+
from bots import RichCardsBot
23+
24+
LOOP = asyncio.get_event_loop()
25+
APP = Flask(__name__, instance_relative_config=True)
26+
APP.config.from_object("config.DefaultConfig")
27+
28+
SETTINGS = BotFrameworkAdapterSettings(
29+
APP.config["APP_ID"], APP.config["APP_PASSWORD"]
30+
)
31+
ADAPTER = BotFrameworkAdapter(SETTINGS)
32+
33+
# Catch-all for errors.
34+
async def on_error(context: TurnContext, error: Exception):
35+
# This check writes out errors to console log
36+
# NOTE: In production environment, you should consider logging this to Azure
37+
# application insights.
38+
print(f"\n [on_turn_error]: { error }", file=sys.stderr)
39+
# Send a message to the user
40+
await context.send_activity("Oops. Something went wrong!")
41+
# Clear out state
42+
await CONVERSATION_STATE.delete(context)
43+
44+
45+
ADAPTER.on_turn_error = on_error
46+
47+
# Create MemoryStorage, UserState and ConversationState
48+
MEMORY = MemoryStorage()
49+
50+
# Commented out user_state because it's not being used.
51+
USER_STATE = UserState(MEMORY)
52+
CONVERSATION_STATE = ConversationState(MEMORY)
53+
54+
55+
DIALOG = MainDialog()
56+
BOT = RichCardsBot(CONVERSATION_STATE, USER_STATE, DIALOG)
57+
58+
59+
@APP.route("/api/messages", methods=["POST"])
60+
def messages():
61+
"""Main bot message handler."""
62+
if request.headers["Content-Type"] == "application/json":
63+
body = request.json
64+
else:
65+
return Response(status=415)
66+
67+
activity = Activity().deserialize(body)
68+
auth_header = (
69+
request.headers["Authorization"] if "Authorization" in request.headers else ""
70+
)
71+
72+
async def aux_func(turn_context):
73+
await BOT.on_turn(turn_context)
74+
75+
try:
76+
task = LOOP.create_task(
77+
ADAPTER.process_activity(activity, auth_header, aux_func)
78+
)
79+
LOOP.run_until_complete(task)
80+
return Response(status=201)
81+
except Exception as exception:
82+
raise exception
83+
84+
85+
if __name__ == "__main__":
86+
try:
87+
APP.run(debug=False, port=APP.config["PORT"]) # nosec debug
88+
except Exception as exception:
89+
raise exception

0 commit comments

Comments
 (0)