-
Notifications
You must be signed in to change notification settings - Fork 305
PrivateConversationState #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
libraries/botbuilder-core/botbuilder/core/private_conversation_state.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from .bot_state import BotState | ||
| from .turn_context import TurnContext | ||
| from .storage import Storage | ||
|
|
||
|
|
||
| class PrivateConversationState(BotState): | ||
| def __init__(self, storage: Storage, namespace: str = ""): | ||
| async def aux_func(context: TurnContext) -> str: | ||
| nonlocal self | ||
| return await self.get_storage_key(context) | ||
|
|
||
| self.namespace = namespace | ||
| super().__init__(storage, aux_func) | ||
|
|
||
| def get_storage_key(self, turn_context: TurnContext) -> str: | ||
| activity = turn_context.activity | ||
| channel_id = activity.channel_id if activity is not None else None | ||
|
|
||
| if not channel_id: | ||
| raise Exception("missing activity.channel_id") | ||
|
|
||
| if activity and activity.conversation and activity.conversation.id is not None: | ||
| conversation_id = activity.conversation.id | ||
| else: | ||
| raise Exception("missing activity.conversation.id") | ||
|
|
||
| if ( | ||
| activity | ||
| and activity.from_property | ||
| and activity.from_property.id is not None | ||
| ): | ||
| user_id = activity.from_property.id | ||
| else: | ||
| raise Exception("missing activity.from_property.id") | ||
|
|
||
| return f"{channel_id}/conversations/{ conversation_id }/users/{ user_id }/{ self.namespace }" | ||
36 changes: 36 additions & 0 deletions
36
libraries/botbuilder-core/tests/test_private_conversation_state.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import aiounittest | ||
|
|
||
| from botbuilder.core import MemoryStorage, TurnContext, PrivateConversationState | ||
| from botbuilder.core.adapters import TestAdapter | ||
| from botbuilder.schema import Activity, ChannelAccount, ConversationAccount | ||
|
|
||
| RECEIVED_MESSAGE = Activity( | ||
| text="received", | ||
| type="message", | ||
| channel_id="test", | ||
| conversation=ConversationAccount(id="convo"), | ||
| from_property=ChannelAccount(id="user"), | ||
| ) | ||
|
|
||
|
|
||
| class TestPrivateConversationState(aiounittest.AsyncTestCase): | ||
| async def test_should_load_and_save_state_from_storage(self): | ||
| storage = MemoryStorage() | ||
| adapter = TestAdapter() | ||
| context = TurnContext(adapter, RECEIVED_MESSAGE) | ||
| private_conversation_state = PrivateConversationState(storage) | ||
|
|
||
| # Simulate a "Turn" in a conversation by loading the state, | ||
| # changing it and then saving the changes to state. | ||
| await private_conversation_state.load(context) | ||
| key = private_conversation_state.get_storage_key(context) | ||
| state = private_conversation_state.get(context) | ||
| assert state == {}, "State not loaded" | ||
| assert key, "Key not found" | ||
| state["test"] = "foo" | ||
| await private_conversation_state.save_changes(context) | ||
|
|
||
| # Check the storage to see if the changes to state were saved. | ||
| items = await storage.read([key]) | ||
| assert key in items, "Saved state not found in storage." | ||
| assert items[key]["test"] == "foo", "Missing test value in stored state." |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The c# doesn't have the /{ self.namespace } part???
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But JS does
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so we are saying this is a bug in the C# can you open an issue?