1
1
# Copyright (c) Microsoft Corporation. All rights reserved.
2
2
# Licensed under the MIT License.
3
+ from warnings import warn
4
+
3
5
from botbuilder .core import (
4
6
BotAdapter ,
7
+ BotState ,
5
8
Storage ,
6
9
RegisterClassMiddleware ,
7
10
UserState ,
@@ -23,6 +26,39 @@ def use_storage(adapter: BotAdapter, storage: Storage) -> BotAdapter:
23
26
"""
24
27
return adapter .use (RegisterClassMiddleware (storage ))
25
28
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
+
26
62
@staticmethod
27
63
def use_state (
28
64
adapter : BotAdapter ,
@@ -31,7 +67,7 @@ def use_state(
31
67
auto : bool = True ,
32
68
) -> BotAdapter :
33
69
"""
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
35
71
the turn context's `turn_state` property.
36
72
37
73
:param adapter: The BotAdapter on which to register the state objects.
@@ -40,6 +76,11 @@ def use_state(
40
76
:param auto: True to automatically persist state each turn.
41
77
:return: The BotAdapter
42
78
"""
79
+ warn (
80
+ "This method is deprecated in 4.9. You should use the method .use_bot_state() instead." ,
81
+ DeprecationWarning ,
82
+ )
83
+
43
84
if not adapter :
44
85
raise TypeError ("BotAdapter is required" )
45
86
0 commit comments