8000 commiting changes to pull memory storage fixes. CoreBot partially wor… · rsliang/botbuilder-python@c57c7a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit c57c7a5

Browse files
committed
commiting changes to pull memory storage fixes. CoreBot partially working
1 parent 031055b commit c57c7a5

File tree

17 files changed

+55
-35
lines changed

17 files changed

+55
-35
lines changed

libraries/botbuilder-applicationinsights/tests/test_telemetry_waterfall.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async def step2(step) -> DialogTurnResult:
6969

7070
mydialog = WaterfallDialog('test', [ step1, step2 ])
7171
mydialog.telemetry_client = telemetry
72-
await dialogs.add(mydialog)
72+
dialogs.add(mydialog)
7373

7474
# Initialize TestAdapter
7575
async def exec_test(turn_context: TurnContext) -> None:
@@ -123,7 +123,7 @@ async def step2(step) -> DialogTurnResult:
123123

124124
mydialog = WaterfallDialog('test', [ step1, step2 ])
125125
mydialog.telemetry_client = telemetry
126-
await dialogs.add(mydialog)
126+
dialogs.add(mydialog)
127127

128128
# Initialize TestAdapter
129129
async def exec_test(turn_context: TurnContext) -> None:

libraries/botbuilder-core/botbuilder/core/activity_handler.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ async def on_turn(self, turn_context: TurnContext):
1818
if hasattr(turn_context.activity, 'type') and turn_context.activity.type is None:
1919
raise TypeError('ActivityHandler.on_turn(): turn_context activity must have a non-None type.')
2020

21-
return {
22-
ActivityTypes.message: await self.on_message_activity(turn_context),
23-
ActivityTypes.conversation_update: await self.on_conversation_update_activity(turn_context),
24-
ActivityTypes.event: await self.on_event_activity(turn_context)
25-
}.get(turn_context.activity.type, await self.on_unrecognized_activity_type(turn_context))
21+
if turn_context.activity.type == ActivityTypes.message:
22+
await self.on_message_activity(turn_context)
23+
elif turn_context.activity.type == ActivityTypes.conversation_update:
24+
await self.on_conversation_update_activity(turn_context)
25+
elif turn_context.activity.type == ActivityTypes.event:
26+
await self.on_event_activity(turn_context)
27+
else:
28+
await self.on_unrecognized_activity_type(turn_context)
2629

2730
async def on_message_activity(self, turn_context: TurnContext):
2831
return

libraries/botbuilder-core/botbuilder/core/bot_state.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ async def save_changes(self, turn_context: TurnContext, force: bool = False) ->
100100
if force or (cached_state != None and cached_state.is_changed == True):
101101
storage_key = self.get_storage_key(turn_context)
102102
changes : Dict[str, object] = { storage_key: cached_state.state }
103+
print(changes)
103104
await self._storage.write(changes)
104105
cached_state.hash = cached_state.compute_hash(cached_state.state)
105106

libraries/botbuilder-core/botbuilder/core/conversation_state.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def call_get_storage_key(context):
3434
def get_storage_key(self, context: TurnContext):
3535
activity = context.activity
3636
channel_id = getattr(activity, 'channel_id', None)
37+
#import pdb; pdb.set_trace()
3738
conversation_id = getattr(activity.conversation, 'id', None) if hasattr(activity, 'conversation') else None
3839

3940
storage_key = None

libraries/botbuilder-core/botbuilder/core/memory_storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async def read(self, keys: List[str]):
2525
try:
2626
for key in keys:
2727
if key in self.memory:
28-
data[key] = deepcopy(self.memory[key])
28+
data[key] = self.memory[key]
2929
except TypeError as e:
3030
raise e
3131

@@ -49,7 +49,7 @@ async def write(self, changes: Dict[str, StoreItem]):
4949
elif old_state.e_tag:
5050
old_state_etag = old_state.e_tag
5151

52-
new_state = deepcopy(new_value)
52+
new_state = new_value
5353

5454
# Set ETag if applicable
5555
if isinstance(new_value, StoreItem):

libraries/botbuilder-dialogs/botbuilder/dialogs/dialog.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .dialog_reason import DialogReason
77
from .dialog_turn_status import DialogTurnStatus
88
from .dialog_turn_result import DialogTurnResult
9+
from .dialog_instance import DialogInstance
910

1011

1112
class Dialog(ABC):
@@ -40,15 +41,15 @@ def telemetry_client(self, value: BotTelemetryClient) -> None:
4041
self._telemetry_client = value
4142

4243
@abstractmethod
43-
async def begin_dialog(self, dc, options: object = None):
44+
async def begin_dialog(self, dc: 'DialogContext', options: object = None):
4445
"""
4546
Method called when a new dialog has been pushed onto the stack and is being activated.
4647
:param dc: The dialog context for the current turn of conversation.
4748
:param options: (Optional) additional argument(s) to pass to the dialog being started.
4849
"""
4950
raise NotImplementedError()
5051

51-
async def continue_dialog(self, dc):
52+
async def continue_dialog(self, dc: 'DialogContext'):
5253
"""
5354
Method called when an instance of the dialog is the "current" dialog and the
5455
user replies with a new activity. The dialog will generally continue to receive the user's
@@ -60,7 +61,7 @@ async def continue_dialog(self, dc):
6061
# By default just end the current dialog.
6162
return await dc.end_dialog(None)
6263

63-
async def resume_dialog(self, dc, reason: DialogReason, result: object):
64+
async def resume_dialog(self, dc: 'DialogContext', reason: DialogReason, result: object):
6465
"""
6566
Method called when an instance of the dialog is being returned to from another
6667
dialog that was started by the current instance using `begin_dialog()`.
@@ -76,15 +77,15 @@ async def resume_dialog(self, dc, reason: DialogReason, result: object):
7677
return await dc.EndDialog(result)
7778

7879
# TODO: instance is DialogInstance
79-
async def reprompt_dialog(self, context: TurnContext, instance):
80+
async def reprompt_dialog(self, context: TurnContext, instance: DialogInstance):
8081
"""
8182
:param context:
8283
:return:
8384
"""
8485
# No-op by default
8586
return
8687
# TODO: instance is DialogInstance
87-
async def end_dialog(self, context: TurnContext, instance):
88+
async def end_dialog(self, context: TurnContext, instance: DialogInstance, reason: DialogReason):
8889
"""
8990
:param context:
9091
:return:

libraries/botbuilder-dialogs/botbuilder/dialogs/dialog_context.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ async def begin_dialog(self, dialog_id: str, options: object = None):
103103
instance.id = dialog_id
104104
instance.state = {}
105105

106-
self._stack.append(instance)
106+
self._stack.insert(0,(instance))
107107

108108
# Call dialog's begin_dialog() method
109109
return await dialog.begin_dialog(self, options)
@@ -138,7 +138,7 @@ async def continue_dialog(self):
138138
# Look up dialog
139139
dialog = await self.find_dialog(self.active_dialog.id)
140140
if not dialog:
141-
raise Exception("DialogContext.continue_dialog(): Can't continue dialog. A dialog with an id of '%s' wasn't found." % active_dialog.id)
141+
raise Exception("DialogContext.continue_dialog(): Can't continue dialog. A dialog with an id of '%s' wasn't found." % self.active_dialog.id)
142142

143143
# Continue execution of dialog
144144
return await dialog.continue_dialog(self)
@@ -238,4 +238,4 @@ async def end_active_dialog(self, reason: DialogReason):
238238
await dialog.end_dialog(self.context, instance, reason)
239239

240240
# Pop dialog off stack
241-
self._stack.pop()
241+
self._stack.pop(0)

libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/confirm_prompt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
class ConfirmPrompt(Prompt):
1414
# TODO: Fix to reference recognizer to use proper constants
1515
choice_defaults : Dict[str, object] = {
16-
'English': (Choice("Si"), Choice("No"), ChoiceFactoryOptions(", ", " o ", ", o ", True)),
16+
'Spanish': (Choice("Si"), Choice("No"), ChoiceFactoryOptions(", ", " o ", ", o ", True)),
1717
'Dutch': (Choice("Ja"), Choice("Nee"), ChoiceFactoryOptions(", ", " of ", ", of ", True)),
1818
'English': (Choice("Yes"), Choice("No"), ChoiceFactoryOptions(", ", " or ", ", or ", True)),
1919
'French': (Choice("Oui"), Choice("Non"), ChoiceFactoryOptions(", ", " ou ", ", ou ", True)),
@@ -49,7 +49,7 @@ async def on_prompt(self, turn_context: TurnContext, state: Dict[str, object], o
4949
confirms = self.confirm_choices if self.confirm_choices != None else (defaults[0], defaults[1])
5050
choices = { confirms[0], confirms[1] }
5151
if is_retry == True and options.retry_prompt != None:
52-
prompt = self.append_choices(options.retry_prompt)
52+
prompt = self.append_choices(options.retry_prompt, channel_id, choices, self.style, choice_opts)
5353
else:
5454
prompt = self.append_choices(options.prompt, channel_id, choices, self.style, choice_opts)
5555
turn_context.send_activity(prompt)

libraries/botbuilder-dialogs/botbuilder/dialogs/prompts/prompt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ async def continue_dialog(self, dc: DialogContext):
7070
# Perform base recognition
7171
instance = dc.active_dialog
7272
state = instance.state[self.persisted_state]
73-
options = instance.State[self.persisted_options]
73+
options = instance.state[self.persisted_options]
7474
recognized = await self.on_recognize(dc.context, state, options)
7575

7676
# Validate the return value
@@ -81,7 +81,7 @@ async def continue_dialog(self, dc: DialogContext):
8181
options.number_of_attempts += 1
8282
else:
8383
if recognized.succeeded:
84-
isValid = True
84+
is_valid = True
8585
# Return recognized value or re-prompt
8686
if is_valid:
8787
return await dc.end_dialog(recognized.value)
@@ -115,7 +115,7 @@ async def on_recognize(self, turn_context: TurnContext, state: Dict[str, object]
115115
# TODO: Fix choices to use Choice object when ported.
116116
# TODO: Fix style to use ListStyle when ported.
117117
# TODO: Fix options to use ChoiceFactoryOptions object when ported.
118-
def append_choices(self, prompt: Activity, channel_id: str, choices: object, style: object, options : object ) -> Activity:
118+
def append_choices(self, prompt: Activity, channel_id: str, choices: object, style: object, options : object = None ) -> Activity:
119119
# Get base prompt text (if any)
120120
text = prompt.text if prompt != None and not prompt.text == False else ''
121121

libraries/botbuilder-dialogs/tests/choices/test_choice_factory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ def test_inline_should_render_choices_inline(self):
5252
self.assertEqual("select from: (1) red, (2) green, or (3) blue", activity.text)
5353

5454
def test_ShouldRenderChoicesAsAList(self):
55-
activity = ChoiceFactory.list(ChoiceFactoryTest.color_choices, "select from:")
55+
activity = ChoiceFactory.list_style(ChoiceFactoryTest.color_choices, "select from:")
5656
self.assertEqual(
5757
"select from:\n\n 1. red\n 2. green\n 3. blue", activity.text
5858
)
5959

6060
def test_should_render_unincluded_numbers_choices_as_a_list(self):
61-
activity = ChoiceFactory.list(
61+
activity = ChoiceFactory.list_style(
6262
ChoiceFactoryTest.color_choices,
6363
"select from:",
6464
options=ChoiceFactoryOptions(include_numbers=False),

libraries/botbuilder-dialogs/tests/test_waterfall.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ async def step2(step) -> DialogTurnResult:
7676
return await step.end_dialog('ending WaterfallDialog.')
7777

7878
mydialog = WaterfallDialog('test', [ step1, step2 ])
79-
await dialogs.add(mydialog)
79+
dialogs.add(mydialog)
8080

8181
# Initialize TestAdapter
8282
async def exec_test(turn_context: TurnContext) -> None:
@@ -111,7 +111,7 @@ async def step_callback3(step: WaterfallStepContext) -> DialogTurnResult:
111111
await step.context.send_activity("step3")
112112

113113
steps = [step_callback1, step_callback2, step_callback3]
114-
await dialogs.add(WaterfallDialog("test", steps))
114+
dialogs.add(WaterfallDialog("test", steps))
115115
self.assertNotEqual(dialogs, None)
116116
self.assertEqual(len(dialogs._dialogs), 1)
117117

@@ -125,7 +125,7 @@ async def test_waterfall_with_class(self):
125125
dialog_state = convo_state.create_property("dialogState")
126126
dialogs = DialogSet(dialog_state)
127127

128-
await dialogs.add(MyWaterfallDialog("test"))
128+
dialogs.add(MyWaterfallDialog("test"))
129129
self.assertNotEqual(dialogs, None)
130130
self.assertEqual(len(dialogs._dialogs), 1)
131131

samples/Core-Bot/bots/dialog_and_welcome_bot.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os.path
23

34
from typing import List
45
from botbuilder.core import CardFactory
@@ -26,12 +27,14 @@ async def on_members_added_activity(self, members_added: List[ChannelAccount], t
2627
# Create an attachment message response.
2728
def create_response(self, activity: Activity, attachment: Attachment):
2829
response = create_activity_reply(activity)
29-
response.Attachments = [attachment]
30+
response.attachments = [attachment]
3031
return response
3132

3233
# Load attachment from file.
3334
def create_adaptive_card_attachment(self):
34-
with open('resources/welcomeCard.json') as f:
35+
relative_path = os.path.abspath(os.path.dirname(__file__))
36+
path = os.path.join(relative_path, "resources/welcomeCard.json")
37+
with open(path) as f:
3538
card = json.load(f)
3639

3740
return Attachment(

samples/Core-Bot/bots/dialog_bot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ async def on_turn(self, turn_context: TurnContext):
2323
await super().on_turn(turn_context)
2424

2525
# Save any state changes that might have occured during the turn.
26+
self.dialogState.set(turn_context, turn_context.turn_state['ConversationState'].state['DialogState'])
2627
await self.conversation_state.save_changes(turn_context, False)
2728
await self.user_state.save_changes(turn_context, False)
2829

samples/Core-Bot/dialogs/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
from .cancel_and_help_dialog import CancelAndHelpDialog
33
from .date_resolver_dialog import DateResolverDialog
44
from .main_dialog import MainDialog
5+
from .new_dialog import NewDialog
56

67
__all__ = [
8+
'NewDialog',
79
'BookingDialog',
810
'CancelAndHelpDialog',
911
'DateResolverDialog',

samples/Core-Bot/dialogs/booking_dialog.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ def __init__(self, dialog_id: str = None):
2828
async def destination_step(self, step_context: WaterfallStepContext) -> DialogTurnResult:
2929
booking_details = step_context.options
3030

31+
print('destination_step')
3132
if (booking_details.destination is None):
33+
import pdb; pdb.set_trace()
3234
return await step_context.prompt(TextPrompt.__name__, PromptOptions(prompt= MessageFactory.text('To what city would you like to travel?')))
3335
else:
3436
return await step_context.next(booking_details.destination)
@@ -39,6 +41,7 @@ async def destination_step(self, step_context: WaterfallStepContext) -> DialogTu
3941
async def origin_step(self, step_context: WaterfallStepContext) -> DialogTurnResult:
4042
booking_details = step_context.options
4143

44+
print('origin_step')
4245
# Capture the response to the previous step's prompt
4346
booking_details.destination = step_context.result
4447
if (booking_details.origin is None):
@@ -53,6 +56,7 @@ async def origin_step(self, step_context: WaterfallStepContext) -> DialogTurnRes
5356
async def travel_date_step(self, step_context: WaterfallStepContext) -> DialogTurnResult:
5457
booking_details = step_context.options
55< 10000 /td>58

59+
print('travel_date_step')
5660
# Capture the results of the previous step
5761
booking_details.origin = step_context.result
5862
if (not booking_details.travel_date or self.is_ambiguous(booking_details.travelDate)):
@@ -66,6 +70,7 @@ async def travel_date_step(self, step_context: WaterfallStepContext) -> DialogTu
6670
async def confirm_step(self, step_context: WaterfallStepContext) -> DialogTurnResult:
6771
booking_details = step_context.options
6872

73+
print('confirm_step')
6974
# Capture the results of the previous step
7075
booking_details.travel_date= step_context.result
7176
msg = f'Please confirm, I have you traveling to: { booking_details.destination } from: { booking_details.origin } on: { booking_details.travel_date}.'

samples/Core-Bot/dialogs/main_dialog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async def final_step(self, step_context: WaterfallStepContext) -> DialogTurnResu
5252
# Now we have all the booking details call the booking service.
5353

5454
# If the call to the booking service was successful tell the user.
55-
55+
import pdb; pdb.set_trace()
5656
time_property = TimexProperty(result.TravelDate)
5757
travel_date_msg = time_property.to_natural_language(datetime.now())
5858
msg = f'I have you booked to {result.destination} from {result.origin} on {travel_date_msg}'

samples/Core-Bot/main.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
from botbuilder.core import (BotFrameworkAdapter, BotFrameworkAdapterSettings, TurnContext,
1212
ConversationState, MemoryStorage, UserState)
1313

14-
from dialogs import MainDialog
15-
from bots import DialogAndWelcomeBot
14+
#from dialogs import MainDialog
15+
#from bots import DialogAndWelcomeBot
16+
from dialogs import NewDialog
17+
from bots import DialogBot
1618
from helpers.dialog_helper import DialogHelper
1719

1820
APP_ID = ''
@@ -27,8 +29,8 @@
2729
user_state = UserState(memory)
2830
conversation_state = ConversationState(memory)
2931

30-
dialog = MainDialog({})
31-
bot = DialogAndWelcomeBot(conversation_state, user_state, dialog)
32+
dialog = NewDialog()
33+
bot = DialogBot(conversation_state, user_state, dialog)
3234

3335
async def messages(req: web.Request) -> web.Response:
3436
body = await req.json()
@@ -38,7 +40,8 @@ async def aux_func(turn_context):
3840
await bot.on_turn(turn_context)
3941

4042
try:
41-
return await ADAPTER.process_activity(activity, auth_header, aux_func)
43+
await ADAPTER.process_activity(activity, auth_header, aux_func)
44+
return web.Response(status=200)
4245
except Exception as e:
4346
raise e
4447

0 commit comments

Comments
 (0)
0