8000 Merge pull request #1227 from microsoft/axsuarez/use-bot-state · ShYuPe/botbuilder-python@a94e899 · GitHub
[go: up one dir, main page]

Skip to content

Commit a94e899

Browse files
authored
Merge pull request microsoft#1227 from microsoft/axsuarez/use-bot-state
Adding use_bot_state
2 parents 9ad53ee + d9ffd70 commit a94e899

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
3+
from warnings import warn
4+
35
from botbuilder.core import (
46
BotAdapter,
7+
BotState,
58
Storage,
69
RegisterClassMiddleware,
710
UserState,
@@ -23,6 +26,39 @@ def use_storage(adapter: BotAdapter, storage: Storage) -> BotAdapter:
2326
"""
2427
return adapter.use(RegisterClassMiddleware(storage))
2528

29+
@staticmethod
30+
def use_bot_state(
31+
bot_adapter: BotAdapter, *bot_states: BotState, auto: bool = True
32+
) -> BotAdapter:
33+
"""
34+
Registers bot state object into the TurnContext. The botstate will be available via the turn context.
35+
36+
:param bot_adapter: The BotAdapter on which to register the state objects.
37+
:param bot_states: One or more BotState objects to register.
38+
:return: The updated adapter.
39+
"""
40+
if not bot_states:
41+
raise TypeError("At least one BotAdapter is required")
42+
43+
for bot_state in bot_states:
44+
bot_adapter.use(
45+
RegisterClassMiddleware(
46+
bot_state, AdapterExtensions.fullname(bot_state)
47+
)
48+
)
49+
50+
if auto:
51+
bot_adapter.use(AutoSaveStateMiddleware(bot_states))
52+
53+
return bot_adapter
54+
55+
@staticmethod
56+
def fullname(obj):
57+
module = obj.__class__.__module__
58+
if module is None or module == str.__class__.__module__:
59+
return obj.__class__.__name__ # Avoid reporting __builtin__
60+
return module + "." + obj.__class__.__name__
61+
2662
@staticmethod
2763
def use_state(
2864
adapter: BotAdapter,
@@ -31,7 +67,7 @@ def use_state(
3167
auto: bool = True,
3268
) -> BotAdapter:
3369
"""
34-
Registers user and conversation state objects with the adapter. These objects will be available via
70+
[DEPRECATED] Registers user and conversation state objects with the adapter. These objects will be available via
3571
the turn context's `turn_state` property.
3672
3773
:param adapter: The BotAdapter on which to register the state objects.
@@ -40,6 +76,11 @@ def use_state(
4076
:param auto: True to automatically persist state each turn.
4177
:return: The BotAdapter
4278
"""
79+
warn(
80+
"This method is deprecated in 4.9. You should use the method .use_bot_state() instead.",
81+
DeprecationWarning,
82+
)
83+
4384
if not adapter:
4485
raise TypeError("BotAdapter is required")
4586

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