Skip to content

Commit c694955

Browse files
committed
Adding use_bot_state
1 parent a837761 commit c694955

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

libraries/botbuilder-core/botbuilder/core/adapter_extensions.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Licensed under the MIT License.
33
from botbuilder.core import (
44
BotAdapter,
5+
BotState,
56
Storage,
67
RegisterClassMiddleware,
78
UserState,
@@ -23,6 +24,39 @@ def use_storage(adapter: BotAdapter, storage: Storage) -> BotAdapter:
2324
"""
2425
return adapter.use(RegisterClassMiddleware(storage))
2526

27+
@staticmethod
28+
def use_bot_state(
29+
bot_adapter: BotAdapter, *bot_states: BotState, auto: bool = True
30+
) -> BotAdapter:
31+
"""
32+
Registers bot state object into the TurnContext. The botstate will be available via the turn context.
33+
34+
:param bot_adapter: The BotAdapter on which to register the state objects.
35+
:param bot_states: One or more BotState objects to register.
36+
:return: The updated adapter.
37+
"""
38+
if not bot_states:
39+
raise TypeError("At least one BotAdapter is required")
40+
41+
for bot_state in bot_states:
42+
bot_adapter.use(
43+
RegisterClassMiddleware(
44+
bot_state, AdapterExtensions.fullname(bot_state)
45+
)
46+
)
47+
48+
if auto:
49+
bot_adapter.use(AutoSaveStateMiddleware(bot_states))
50+
51+
return bot_adapter
52+
53+
@staticmethod
54+
def fullname(obj):
55+
module = obj.__class__.__module__
56+
if module is None or module == str.__class__.__module__:
57+
return obj.__class__.__name__ # Avoid reporting __builtin__
58+
return module + "." + obj.__class__.__name__
59+
2660
@staticmethod
2761
def use_state(
2862
adapter: BotAdapter,

libraries/botbuilder-core/botbuilder/core/register_class_middleware.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ class RegisterClassMiddleware(Middleware):
1010
Middleware for adding an object to or registering a service with the current turn context.
1111
"""
1212

13-
def __init__(self, service):
13+
def __init__(self, service, key: str = None):
1414
self.service = service
15+
self._key = key
1516

1617
async def on_turn(
1718
self, context: TurnContext, logic: Callable[[TurnContext], Awaitable]
1819
):
1920
# C# has TurnStateCollection with has overrides for adding items
2021
# to TurnState. Python does not. In C#'s case, there is an 'Add'
2122
# to handle adding object, and that uses the fully qualified class name.
22-
context.turn_state[self.fullname(self.service)] = self.service
23+
key = self._key or self.fullname(self.service)
24+
context.turn_state[key] = self.service
2325
await logic()
2426

2527
@staticmethod

libraries/botbuilder-dialogs/tests/test_dialogextensions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ async def capture_eoc(
221221
logic, TestAdapter.create_conversation_reference(conversation_id)
222222
)
223223
AdapterExtensions.use_storage(adapter, storage)
224-
AdapterExtensions.use_state(adapter, user_state, convo_state)
224+
AdapterExtensions.use_bot_state(adapter, user_state, convo_state)
225225
adapter.use(TranscriptLoggerMiddleware(ConsoleTranscriptLogger()))
226226

227227
return TestFlow(None, adapter)

0 commit comments

Comments
 (0)