11# Copyright (c) Microsoft Corporation. All rights reserved.
22# Licensed under the MIT License.
3+ from warnings import warn
4+
35from 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
0 commit comments