Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
move APIToken from conversation state to turn state (#2569)
Browse files Browse the repository at this point in the history
  • Loading branch information
KayMKM authored and bobokids committed Oct 25, 2019
1 parent b27a524 commit dc29c80
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 35 deletions.
48 changes: 30 additions & 18 deletions skills/csharp/calendarskill/Dialogs/CalendarSkillDialogBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using CalendarSkill.Services;
using CalendarSkill.Utilities;
using Luis;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.AI.Luis;
using Microsoft.Bot.Builder.Dialogs;
Expand All @@ -34,6 +35,8 @@ namespace CalendarSkill.Dialogs
{
public class CalendarSkillDialogBase : ComponentDialog
{
protected const string APITokenKey = "APITokenKey";

private ConversationState _conversationState;

public CalendarSkillDialogBase(
Expand Down Expand Up @@ -125,7 +128,15 @@ protected async Task<DialogTurnResult> GetAuthToken(WaterfallStepContext sc, Can
if (sc.Result is ProviderTokenResponse providerTokenResponse)
{
var state = await Accessor.GetAsync(sc.Context);
state.APIToken = providerTokenResponse.TokenResponse.Token;

if (sc.Context.TurnState.TryGetValue(APITokenKey, out var token))
{
sc.Context.TurnState[APITokenKey] = providerTokenResponse.TokenResponse.Token;
}
else
{
sc.Context.TurnState.Add(APITokenKey, providerTokenResponse.TokenResponse.Token);
}

var provider = providerTokenResponse.AuthenticationProvider;

Expand Down Expand Up @@ -162,7 +173,8 @@ protected async Task<DialogTurnResult> GetAuthToken(WaterfallStepContext sc, Can
try
{
var state = await Accessor.GetAsync(sc.Context);
var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
var calendarService = ServiceManager.InitCalendarService((string)token, state.EventSource);

// search by time without cancelled meeting
if (!state.ShowMeetingInfor.ShowingMeetings.Any())
Expand Down Expand Up @@ -791,8 +803,8 @@ protected List<EventModel> GetFilteredEvents(CalendarSkillState state, string us
protected async Task<string> GetMyPhotoUrlAsync(ITurnContext context)
{
var state = await Accessor.GetAsync(context);
var token = state.APIToken;
var service = ServiceManager.InitUserService(token, state.EventSource);
context.TurnState.TryGetValue(APITokenKey, out var token);
var service = ServiceManager.InitUserService((string)token, state.EventSource);

PersonModel me = null;

Expand All @@ -817,8 +829,8 @@ protected async Task<string> GetMyPhotoUrlAsync(ITurnContext context)
protected async Task<string> GetUserPhotoUrlAsync(ITurnContext context, EventModel.Attendee attendee)
{
var state = await Accessor.GetAsync(context);
var token = state.APIToken;
var service = ServiceManager.InitUserService(token, state.EventSource);
context.TurnState.TryGetValue(APITokenKey, out var token);
var service = ServiceManager.InitUserService((string)token, state.EventSource);
var displayName = attendee.DisplayName ?? attendee.Address;

try
Expand Down Expand Up @@ -1497,8 +1509,8 @@ protected async Task<List<PersonModel>> GetContactsAsync(WaterfallStepContext sc
{
var result = new List<PersonModel>();
var state = await Accessor.GetAsync(sc.Context);
var token = state.APIToken;
var service = ServiceManager.InitUserService(token, state.EventSource);
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
var service = ServiceManager.InitUserService((string)token, state.EventSource);

// Get users.
result = await service.GetContactsAsync(name);
Expand All @@ -1509,8 +1521,8 @@ protected async Task<List<PersonModel>> GetPeopleWorkWithAsync(WaterfallStepCont
{
var result = new List<PersonModel>();
var state = await Accessor.GetAsync(sc.Context);
var token = state.APIToken;
var service = ServiceManager.InitUserService(token, state.EventSource);
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
var service = ServiceManager.InitUserService((string)token, state.EventSource);

// Get users.
result = await service.GetPeopleAsync(name);
Expand All @@ -1522,8 +1534,8 @@ protected async Task<List<PersonModel>> GetUserAsync(WaterfallStepContext sc, st
{
var result = new List<PersonModel>();
var state = await Accessor.GetAsync(sc.Context);
var token = state.APIToken;
var service = ServiceManager.InitUserService(token, state.EventSource);
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
var service = ServiceManager.InitUserService((string)token, state.EventSource);

// Get users.
result = await service.GetUserAsync(name);
Expand All @@ -1534,24 +1546,24 @@ protected async Task<List<PersonModel>> GetUserAsync(WaterfallStepContext sc, st
protected async Task<PersonModel> GetMyManager(WaterfallStepContext sc)
{
var state = await Accessor.GetAsync(sc.Context);
var token = state.APIToken;
var service = ServiceManager.InitUserService(token, state.EventSource);
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
var service = ServiceManager.InitUserService((string)token, state.EventSource);
return await service.GetMyManagerAsync();
}

protected async Task<PersonModel> GetManager(WaterfallStepContext sc, string name)
{
var state = await Accessor.GetAsync(sc.Context);
var token = state.APIToken;
var service = ServiceManager.InitUserService(token, state.EventSource);
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
var service = ServiceManager.InitUserService((string)token, state.EventSource);
return await service.GetManagerAsync(name);
}

protected async Task<PersonModel> GetMe(ITurnContext context)
{
var state = await Accessor.GetAsync(context);
var token = state.APIToken;
var service = ServiceManager.InitUserService(token, state.EventSource);
context.TurnState.TryGetValue(APITokenKey, out var token);
var service = ServiceManager.InitUserService((string)token, state.EventSource);
return await service.GetMeAsync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public ChangeEventStatusDialog(
GetAuthToken,
AfterGetAuthToken,
CheckFocusedEvent,
GetAuthToken,
AfterGetAuthToken,
ConfirmBeforeAction,
AfterConfirmBeforeAction,
GetAuthToken,
Expand Down Expand Up @@ -145,8 +147,9 @@ public ChangeEventStatusDialog(
{
var state = await Accessor.GetAsync(sc.Context);
var options = (ChangeEventStatusDialogOptions)sc.Options;
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);

var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
var calendarService = ServiceManager.InitCalendarService((string)token, state.EventSource);
var deleteEvent = state.ShowMeetingInfor.FocusedEvents[0];
if (options.NewEventStatus == EventStatus.Cancelled)
{
Expand Down Expand Up @@ -208,7 +211,8 @@ public ChangeEventStatusDialog(
}
else
{
var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
var calendarService = ServiceManager.InitCalendarService((string)token, state.EventSource);
if (options.NewEventStatus == EventStatus.Cancelled)
{
return await sc.PromptAsync(Actions.GetEventPrompt, new GetEventOptions(calendarService, state.GetUserTimeZone())
Expand Down
5 changes: 4 additions & 1 deletion skills/csharp/calendarskill/Dialogs/CreateEventDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public CreateEventDialog(
CollectStartTime,
CollectDuration,
CollectLocation,
GetAuthToken,
AfterGetAuthToken,
ShowEventInfo,
ConfirmBeforeCreatePrompt,
AfterConfirmBeforeCreatePrompt,
Expand Down Expand Up @@ -624,7 +626,8 @@ public CreateEventDialog(
Location = state.MeetingInfor.Location,
};

var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
var calendarService = ServiceManager.InitCalendarService((string)token, state.EventSource);
if (await calendarService.CreateEventAysnc(newEvent) != null)
{
var tokens = new StringDictionary
Expand Down
3 changes: 2 additions & 1 deletion skills/csharp/calendarskill/Dialogs/JoinEventDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ private bool IsValidJoinTime(TimeZoneInfo userTimeZone, EventModel e)
}
else
{
var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
var calendarService = ServiceManager.InitCalendarService((string)token, state.EventSource);
return await sc.PromptAsync(Actions.GetEventPrompt, new GetEventOptions(calendarService, state.GetUserTimeZone())
{
Prompt = ResponseManager.GetResponse(JoinEventResponses.NoMeetingToConnect),
Expand Down
2 changes: 2 additions & 0 deletions skills/csharp/calendarskill/Dialogs/ShowEventsDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public ShowEventsDialog(

var readEvent = new WaterfallStep[]
{
GetAuthToken,
AfterGetAuthToken,
ReadEvent,
PromptForNextActionAfterRead,
HandleNextActionAfterRead,
Expand Down
7 changes: 2 additions & 5 deletions skills/csharp/calendarskill/Dialogs/TimeRemainingDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,9 @@ public TimeRemainingDialog(
try
{
var state = await Accessor.GetAsync(sc.Context);
if (string.IsNullOrEmpty(state.APIToken))
{
return await sc.EndDialogAsync(true);
}
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);

var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
var calendarService = ServiceManager.InitCalendarService((string)token, state.EventSource);

var eventList = await calendarService.GetUpcomingEventsAsync();
var nextEventList = new List<EventModel>();
Expand Down
5 changes: 3 additions & 2 deletions skills/csharp/calendarskill/Dialogs/UpcomingEventDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ public UpcomingEventDialog(
try
{
var calendarState = await Accessor.GetAsync(sc.Context, () => new CalendarSkillState());
sc.Context.TurnState.TryGetValue(APITokenKey, out var apiToken);

if (!string.IsNullOrWhiteSpace(calendarState.APIToken))
if (!string.IsNullOrWhiteSpace((string)apiToken))
{
var activity = sc.Context.Activity;
var userId = activity.From.Id;

var proactiveState = await _proactiveStateAccessor.GetAsync(sc.Context, () => new ProactiveModel());
var calendarService = ServiceManager.InitCalendarService(calendarState.APIToken, calendarState.EventSource);
var calendarService = ServiceManager.InitCalendarService((string)apiToken, calendarState.EventSource);

_backgroundTaskQueue.QueueBackgroundWorkItem(async (token) =>
{
Expand Down
8 changes: 6 additions & 2 deletions skills/csharp/calendarskill/Dialogs/UpdateEventDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public UpdateEventDialog(
AfterGetAuthToken,
CheckFocusedEvent,
GetNewEventTime,
GetAuthToken,
AfterGetAuthToken,
ConfirmBeforeUpdate,
AfterConfirmBeforeUpdate,
GetAuthToken,
Expand Down Expand Up @@ -96,7 +98,8 @@ public UpdateEventDialog(
}
else
{
var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
var calendarService = ServiceManager.InitCalendarService((string)token, state.EventSource);
return await sc.PromptAsync(Actions.GetEventPrompt, new GetEventOptions(calendarService, state.GetUserTimeZone())
{
Prompt = ResponseManager.GetResponse(UpdateEventResponses.NoUpdateStartTime),
Expand Down Expand Up @@ -209,7 +212,8 @@ public UpdateEventDialog(
updateEvent.Id = origin.RecurringId;
}

var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
var calendarService = ServiceManager.InitCalendarService((string)token, state.EventSource);
var newEvent = await calendarService.UpdateEventByIdAsync(updateEvent);

var replyMessage = await GetDetailMeetingResponseAsync(sc, newEvent, UpdateEventResponses.EventUpdated);
Expand Down
3 changes: 0 additions & 3 deletions skills/csharp/calendarskill/Models/CalendarSkillState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public class CalendarSkillState

public Luis.General GeneralLuisResult { get; set; }

public string APIToken { get; set; }

public int PageSize { get; set; } = 0;

public EventSource EventSource { get; set; } = EventSource.Other;
Expand All @@ -41,7 +39,6 @@ public void Clear()
UserInfo = new UserInformation();
LuisResult = null;
GeneralLuisResult = null;
APIToken = null;
PageSize = 0;
EventSource = EventSource.Other;
MeetingInfor.Clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public TestFlow GetTestFlow()
{
var bot = sp.GetService<IBot>();
var state = await CalendarStateAccessor.GetAsync(context, () => new CalendarSkillState());
state.APIToken = "test";
//state.APIToken = "test";
state.EventSource = EventSource.Microsoft;
await bot.OnTurnAsync(context, CancellationToken.None);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ await this.GetTestFlow()
.Send(ChangeMeetingStatusTestUtterances.DeleteMeetingWithStartTime)
.AssertReply(this.ShowAuth())
.Send(this.GetAuthResponse())
.AssertReply(this.ShowAuth())
.Send(this.GetAuthResponse())
.AssertReply(this.ShowCalendarList())
.Send(Strings.Strings.ConfirmYes)
.AssertReply(this.ShowAuth())
Expand All @@ -54,6 +56,8 @@ await this.GetTestFlow()
.Send(ChangeMeetingStatusTestUtterances.DeleteMeetingWithTitle)
.AssertReply(this.ShowAuth())
.Send(this.GetAuthResponse())
.AssertReply(this.ShowAuth())
.Send(this.GetAuthResponse())
.AssertReply(this.ShowCalendarList())
.Send(Strings.Strings.ConfirmYes)
.AssertReply(this.ShowAuth())
Expand All @@ -74,6 +78,8 @@ await this.GetTestFlow()
.Send(this.GetAuthResponse())
.AssertReply(this.ShowCalendarList())
.Send(GeneralTestUtterances.ChooseOne)
.AssertReply(this.ShowAuth())
.Send(this.GetAuthResponse())
.AssertReply(this.ShowCalendarList())
.Send(Strings.Strings.ConfirmYes)
.AssertReply(this.ShowAuth())
Expand All @@ -90,6 +96,8 @@ await this.GetTestFlow()
.Send(ChangeMeetingStatusTestUtterances.AcceptMeetingWithStartTime)
.AssertReply(this.ShowAuth())
.Send(this.GetAuthResponse())
.AssertReply(this.ShowAuth())
.Send(this.GetAuthResponse())
.AssertReply(this.ShowCalendarList())
.Send(Strings.Strings.ConfirmYes)
.AssertReply(this.ShowAuth())
Expand Down
Loading

0 comments on commit dc29c80

Please sign in to comment.