public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default) { var activity = turnContext.Activity; if (!string.IsNullOrEmpty(activity.From.Role) && activity.From.Role.Equals("user", StringComparison.InvariantCultureIgnoreCase)) { var proactiveState = await _proactiveStateAccessor.GetAsync(turnContext, () => new ProactiveModel()).ConfigureAwait(false); ProactiveModel.ProactiveData data; var hashedUserId = MD5Util.ComputeHash(turnContext.Activity.From.Id); var conversationReference = turnContext.Activity.GetConversationReference(); var proactiveData = new ProactiveModel.ProactiveData { Conversation = conversationReference }; if (proactiveState.TryGetValue(hashedUserId, out data)) { data.Conversation = conversationReference; } else { data = new ProactiveModel.ProactiveData { Conversation = conversationReference }; } proactiveState[hashedUserId] = data; await _proactiveStateAccessor.SetAsync(turnContext, proactiveState).ConfigureAwait(false); await _proactiveState.SaveChangesAsync(turnContext).ConfigureAwait(false); } await next(cancellationToken).ConfigureAwait(false); }
public void MD5Util_Test() { var hashString = MD5Util.ComputeHash("Test"); Assert.IsNotNull(hashString); Assert.AreEqual("0cbc6611f5540bd0809a388dc95a615b", hashString); }
public async Task Test_ProactiveEvent() { // Create EventData var data = new EventData { Message = "Test", UserId = "Test" }; var sp = Services.BuildServiceProvider(); var adapter = sp.GetService <TestAdapter>(); var proactiveState = sp.GetService <ProactiveState>(); // Create ProactiveStateAccessor var proactiveStateAccessor = proactiveState.CreateProperty <ProactiveModel>(nameof(ProactiveModel)); // Create EventActivity var eventActivity = new Activity() { ChannelId = "TestProactive", Type = ActivityTypes.Event, Conversation = new ConversationAccount(id: $"{Guid.NewGuid()}"), From = new ChannelAccount(id: $"Notification.TestProactive", name: $"Notification.Proactive"), Recipient = new ChannelAccount(id: $"Notification.TestProactive", name: $"Notification.Proactive"), Name = "BroadcastEvent", Value = JsonConvert.SerializeObject(data) }; // Create turnContext with EventActivity var turnContext = new TurnContext(adapter, eventActivity); // Create Proactive Subscription var proactiveSub = await proactiveStateAccessor.GetAsync( turnContext, () => new ProactiveModel(), CancellationToken.None) .ConfigureAwait(false); // Store Activity and Thread Id proactiveSub[MD5Util.ComputeHash("Test")] = new ProactiveModel.ProactiveData { Conversation = eventActivity.GetConversationReference(), }; // Save changes to proactive model await proactiveStateAccessor.SetAsync(turnContext, proactiveSub).ConfigureAwait(false); await proactiveState.SaveChangesAsync(turnContext).ConfigureAwait(false); // Validate EventActivity Response await GetTestFlow() .Send(eventActivity) .AssertReply(activity => Assert.AreEqual("Test", activity.AsMessageActivity().Text)) .StartTestAsync(); }
public async Task DefaultOptions() { var microsoftAppId = string.Empty; var storage = new MemoryStorage(); var state = new ProactiveStateSharded(storage); var proactiveStateAccessor = state.CreateProperty <ProactiveModel>(nameof(ProactiveModel)); var conversation = TestAdapter.CreateConversation(ProactiveTestConstants.Name); conversation.User.Role = ProactiveTestConstants.User; var adapter = new TestAdapter(conversation) .Use(new ProactiveStateMiddlewareSharded(state)); var response = ProactiveTestConstants.Response; var proactiveResponse = ProactiveTestConstants.ProactiveResponse; var proactiveEvent = new Activity(type: ActivityTypes.Event, value: ProactiveTestConstants.User1, text: proactiveResponse); await new TestFlow(adapter, async(context, cancellationToken) => { if (context.Activity.Type == ActivityTypes.Event) { var proactiveModel = await proactiveStateAccessor.GetAsync(context, () => new ProactiveModel()); var hashedUserId = MD5Util.ComputeHash(context.Activity.Value.ToString()); var conversationReference = proactiveModel[hashedUserId].Conversation; await context.Adapter.ContinueConversationAsync(microsoftAppId, conversationReference, ContinueConversationCallback(context, context.Activity.Text), cancellationToken); } else { await context.SendActivityAsync(context.Activity.CreateReply(response)); } }) .Send("foo") .AssertReply(response) .Send(proactiveEvent) .AssertReply(proactiveResponse) .StartTestAsync(); }
protected override async Task OnEventActivityAsync(ITurnContext <IEventActivity> turnContext, CancellationToken cancellationToken) { var ev = turnContext.Activity.AsEventActivity(); var value = ev.Value?.ToString(); switch (ev.Name) { case Events.Broadcast: { var eventData = JsonConvert.DeserializeObject <EventData>(turnContext.Activity.Value.ToString()); var proactiveModel = await _proactiveStateAccessor.GetAsync(turnContext, () => new ProactiveModel()); var hashedUserId = MD5Util.ComputeHash(eventData.UserId); var conversationReference = proactiveModel[hashedUserId].Conversation; await turnContext.Adapter.ContinueConversationAsync(_appCredentials.MicrosoftAppId, conversationReference, ContinueConversationCallback(turnContext, eventData.Message), cancellationToken); break; } case TokenEvents.TokenResponseEventName: { // Forward the token response activity to the dialog waiting on the stack. await _dialog.RunAsync(turnContext, _dialogStateAccessor, cancellationToken); break; } default: { await turnContext.SendActivityAsync(new Activity(type : ActivityTypes.Trace, text : $"Unknown Event '{ev.Name ?? "undefined"}' was received but not processed.")); break; } } }
private UpcomingEventCallback UpcomingEventCallback(string userId, WaterfallStepContext sc, ProactiveModel proactiveModel) { return(async(eventModel, cancellationToken) => { await sc.Context.Adapter.ContinueConversationAsync(Settings.MicrosoftAppId, proactiveModel[MD5Util.ComputeHash(userId)].Conversation, UpcomingEventContinueConversationCallback(eventModel, sc), cancellationToken); }); }
// Runs when a new event activity comes in. protected override async Task OnEventActivityAsync(DialogContext innerDc, CancellationToken cancellationToken = default) { var ev = innerDc.Context.Activity.AsEventActivity(); var value = ev.Value?.ToString(); switch (ev.Name) { case Events.Location: { var locationObj = new JObject(); locationObj.Add(StateProperties.Location, JToken.FromObject(value)); // Store location for use by skills. var skillContext = await _skillContext.GetAsync(innerDc.Context, () => new SkillContext()); skillContext[StateProperties.Location] = locationObj; await _skillContext.SetAsync(innerDc.Context, skillContext); break; } case Events.TimeZone: { try { var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(value); var timeZoneObj = new JObject(); timeZoneObj.Add(StateProperties.TimeZone, JToken.FromObject(timeZoneInfo)); // Store location for use by skills. var skillContext = await _skillContext.GetAsync(innerDc.Context, () => new SkillContext()); skillContext[StateProperties.TimeZone] = timeZoneObj; await _skillContext.SetAsync(innerDc.Context, skillContext); } catch { await innerDc.Context.SendActivityAsync(new Activity(type : ActivityTypes.Trace, text : $"Received time zone could not be parsed. Property not set.")); } break; } case Events.Broadcast: { var eventData = JsonConvert.DeserializeObject <EventData>(innerDc.Context.Activity.Value.ToString()); var proactiveModel = await _proactiveStateAccessor.GetAsync(innerDc.Context, () => new ProactiveModel()); var hashedUserId = MD5Util.ComputeHash(eventData.UserId); var conversationReference = proactiveModel[hashedUserId].Conversation; await innerDc.Context.Adapter.ContinueConversationAsync(_appCredentials.MicrosoftAppId, conversationReference, ContinueConversationCallback(innerDc.Context, eventData.Message), cancellationToken); break; } case TokenEvents.TokenResponseEventName: { // Forward the token response activity to the dialog waiting on the stack. await innerDc.ContinueDialogAsync(); break; } default: { await innerDc.Context.SendActivityAsync(new Activity(type : ActivityTypes.Trace, text : $"Unknown Event '{ev.Name ?? "undefined"}' was received but not processed.")); break; } } }