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

Commit dc29c80

Browse files
KayMKMbobokids
authored andcommitted
move APIToken from conversation state to turn state (#2569)
1 parent b27a524 commit dc29c80

14 files changed

+149
-35
lines changed

skills/csharp/calendarskill/Dialogs/CalendarSkillDialogBase.cs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using CalendarSkill.Services;
1313
using CalendarSkill.Utilities;
1414
using Luis;
15+
using Microsoft.AspNetCore.Mvc;
1516
using Microsoft.Bot.Builder;
1617
using Microsoft.Bot.Builder.AI.Luis;
1718
using Microsoft.Bot.Builder.Dialogs;
@@ -34,6 +35,8 @@ namespace CalendarSkill.Dialogs
3435
{
3536
public class CalendarSkillDialogBase : ComponentDialog
3637
{
38+
protected const string APITokenKey = "APITokenKey";
39+
3740
private ConversationState _conversationState;
3841

3942
public CalendarSkillDialogBase(
@@ -125,7 +128,15 @@ protected async Task<DialogTurnResult> GetAuthToken(WaterfallStepContext sc, Can
125128
if (sc.Result is ProviderTokenResponse providerTokenResponse)
126129
{
127130
var state = await Accessor.GetAsync(sc.Context);
128-
state.APIToken = providerTokenResponse.TokenResponse.Token;
131+
132+
if (sc.Context.TurnState.TryGetValue(APITokenKey, out var token))
133+
{
134+
sc.Context.TurnState[APITokenKey] = providerTokenResponse.TokenResponse.Token;
135+
}
136+
else
137+
{
138+
sc.Context.TurnState.Add(APITokenKey, providerTokenResponse.TokenResponse.Token);
139+
}
129140

130141
var provider = providerTokenResponse.AuthenticationProvider;
131142

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

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

797809
PersonModel me = null;
798810

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

824836
try
@@ -1497,8 +1509,8 @@ protected async Task<List<PersonModel>> GetContactsAsync(WaterfallStepContext sc
14971509
{
14981510
var result = new List<PersonModel>();
14991511
var state = await Accessor.GetAsync(sc.Context);
1500-
var token = state.APIToken;
1501-
var service = ServiceManager.InitUserService(token, state.EventSource);
1512+
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
1513+
var service = ServiceManager.InitUserService((string)token, state.EventSource);
15021514

15031515
// Get users.
15041516
result = await service.GetContactsAsync(name);
@@ -1509,8 +1521,8 @@ protected async Task<List<PersonModel>> GetPeopleWorkWithAsync(WaterfallStepCont
15091521
{
15101522
var result = new List<PersonModel>();
15111523
var state = await Accessor.GetAsync(sc.Context);
1512-
var token = state.APIToken;
1513-
var service = ServiceManager.InitUserService(token, state.EventSource);
1524+
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
1525+
var service = ServiceManager.InitUserService((string)token, state.EventSource);
15141526

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

15281540
// Get users.
15291541
result = await service.GetUserAsync(name);
@@ -1534,24 +1546,24 @@ protected async Task<List<PersonModel>> GetUserAsync(WaterfallStepContext sc, st
15341546
protected async Task<PersonModel> GetMyManager(WaterfallStepContext sc)
15351547
{
15361548
var state = await Accessor.GetAsync(sc.Context);
1537-
var token = state.APIToken;
1538-
var service = ServiceManager.InitUserService(token, state.EventSource);
1549+
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
1550+
var service = ServiceManager.InitUserService((string)token, state.EventSource);
15391551
return await service.GetMyManagerAsync();
15401552
}
15411553

15421554
protected async Task<PersonModel> GetManager(WaterfallStepContext sc, string name)
15431555
{
15441556
var state = await Accessor.GetAsync(sc.Context);
1545-
var token = state.APIToken;
1546-
var service = ServiceManager.InitUserService(token, state.EventSource);
1557+
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
1558+
var service = ServiceManager.InitUserService((string)token, state.EventSource);
15471559
return await service.GetManagerAsync(name);
15481560
}
15491561

15501562
protected async Task<PersonModel> GetMe(ITurnContext context)
15511563
{
15521564
var state = await Accessor.GetAsync(context);
1553-
var token = state.APIToken;
1554-
var service = ServiceManager.InitUserService(token, state.EventSource);
1565+
context.TurnState.TryGetValue(APITokenKey, out var token);
1566+
var service = ServiceManager.InitUserService((string)token, state.EventSource);
15551567
return await service.GetMeAsync();
15561568
}
15571569

skills/csharp/calendarskill/Dialogs/ChangeEventStatusDialog.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public ChangeEventStatusDialog(
3737
GetAuthToken,
3838
AfterGetAuthToken,
3939
CheckFocusedEvent,
40+
GetAuthToken,
41+
AfterGetAuthToken,
4042
ConfirmBeforeAction,
4143
AfterConfirmBeforeAction,
4244
GetAuthToken,
@@ -145,8 +147,9 @@ public ChangeEventStatusDialog(
145147
{
146148
var state = await Accessor.GetAsync(sc.Context);
147149
var options = (ChangeEventStatusDialogOptions)sc.Options;
150+
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
148151

149-
var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
152+
var calendarService = ServiceManager.InitCalendarService((string)token, state.EventSource);
150153
var deleteEvent = state.ShowMeetingInfor.FocusedEvents[0];
151154
if (options.NewEventStatus == EventStatus.Cancelled)
152155
{
@@ -208,7 +211,8 @@ public ChangeEventStatusDialog(
208211
}
209212
else
210213
{
211-
var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
214+
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
215+
var calendarService = ServiceManager.InitCalendarService((string)token, state.EventSource);
212216
if (options.NewEventStatus == EventStatus.Cancelled)
213217
{
214218
return await sc.PromptAsync(Actions.GetEventPrompt, new GetEventOptions(calendarService, state.GetUserTimeZone())

skills/csharp/calendarskill/Dialogs/CreateEventDialog.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public CreateEventDialog(
5353
CollectStartTime,
5454
CollectDuration,
5555
CollectLocation,
56+
GetAuthToken,
57+
AfterGetAuthToken,
5658
ShowEventInfo,
5759
ConfirmBeforeCreatePrompt,
5860
AfterConfirmBeforeCreatePrompt,
@@ -624,7 +626,8 @@ public CreateEventDialog(
624626
Location = state.MeetingInfor.Location,
625627
};
626628

627-
var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
629+
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
630+
var calendarService = ServiceManager.InitCalendarService((string)token, state.EventSource);
628631
if (await calendarService.CreateEventAysnc(newEvent) != null)
629632
{
630633
var tokens = new StringDictionary

skills/csharp/calendarskill/Dialogs/JoinEventDialog.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ private bool IsValidJoinTime(TimeZoneInfo userTimeZone, EventModel e)
158158
}
159159
else
160160
{
161-
var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
161+
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
162+
var calendarService = ServiceManager.InitCalendarService((string)token, state.EventSource);
162163
return await sc.PromptAsync(Actions.GetEventPrompt, new GetEventOptions(calendarService, state.GetUserTimeZone())
163164
{
164165
Prompt = ResponseManager.GetResponse(JoinEventResponses.NoMeetingToConnect),

skills/csharp/calendarskill/Dialogs/ShowEventsDialog.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public ShowEventsDialog(
8888

8989
var readEvent = new WaterfallStep[]
9090
{
91+
GetAuthToken,
92+
AfterGetAuthToken,
9193
ReadEvent,
9294
PromptForNextActionAfterRead,
9395
HandleNextActionAfterRead,

skills/csharp/calendarskill/Dialogs/TimeRemainingDialog.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,9 @@ public TimeRemainingDialog(
5252
try
5353
{
5454
var state = await Accessor.GetAsync(sc.Context);
55-
if (string.IsNullOrEmpty(state.APIToken))
56-
{
57-
return await sc.EndDialogAsync(true);
58-
}
55+
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
5956

60-
var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
57+
var calendarService = ServiceManager.InitCalendarService((string)token, state.EventSource);
6158

6259
var eventList = await calendarService.GetUpcomingEventsAsync();
6360
var nextEventList = new List<EventModel>();

skills/csharp/calendarskill/Dialogs/UpcomingEventDialog.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ public UpcomingEventDialog(
6262
try
6363
{
6464
var calendarState = await Accessor.GetAsync(sc.Context, () => new CalendarSkillState());
65+
sc.Context.TurnState.TryGetValue(APITokenKey, out var apiToken);
6566

66-
if (!string.IsNullOrWhiteSpace(calendarState.APIToken))
67+
if (!string.IsNullOrWhiteSpace((string)apiToken))
6768
{
6869
var activity = sc.Context.Activity;
6970
var userId = activity.From.Id;
7071

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

7475
_backgroundTaskQueue.QueueBackgroundWorkItem(async (token) =>
7576
{

skills/csharp/calendarskill/Dialogs/UpdateEventDialog.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public UpdateEventDialog(
4242
AfterGetAuthToken,
4343
CheckFocusedEvent,
4444
GetNewEventTime,
45+
GetAuthToken,
46+
AfterGetAuthToken,
4547
ConfirmBeforeUpdate,
4648
AfterConfirmBeforeUpdate,
4749
GetAuthToken,
@@ -96,7 +98,8 @@ public UpdateEventDialog(
9698
}
9799
else
98100
{
99-
var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
101+
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
102+
var calendarService = ServiceManager.InitCalendarService((string)token, state.EventSource);
100103
return await sc.PromptAsync(Actions.GetEventPrompt, new GetEventOptions(calendarService, state.GetUserTimeZone())
101104
{
102105
Prompt = ResponseManager.GetResponse(UpdateEventResponses.NoUpdateStartTime),
@@ -209,7 +212,8 @@ public UpdateEventDialog(
209212
updateEvent.Id = origin.RecurringId;
210213
}
211214

212-
var calendarService = ServiceManager.InitCalendarService(state.APIToken, state.EventSource);
215+
sc.Context.TurnState.TryGetValue(APITokenKey, out var token);
216+
var calendarService = ServiceManager.InitCalendarService((string)token, state.EventSource);
213217
var newEvent = await calendarService.UpdateEventByIdAsync(updateEvent);
214218

215219
var replyMessage = await GetDetailMeetingResponseAsync(sc, newEvent, UpdateEventResponses.EventUpdated);

skills/csharp/calendarskill/Models/CalendarSkillState.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ public class CalendarSkillState
1414

1515
public Luis.General GeneralLuisResult { get; set; }
1616

17-
public string APIToken { get; set; }
18-
1917
public int PageSize { get; set; } = 0;
2018

2119
public EventSource EventSource { get; set; } = EventSource.Other;
@@ -41,7 +39,6 @@ public void Clear()
4139
UserInfo = new UserInformation();
4240
LuisResult = null;
4341
GeneralLuisResult = null;
44-
APIToken = null;
4542
PageSize = 0;
4643
EventSource = EventSource.Other;
4744
MeetingInfor.Clear();

skills/csharp/tests/calendarskill.tests/Flow/CalendarSkillTestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public TestFlow GetTestFlow()
123123
{
124124
var bot = sp.GetService<IBot>();
125125
var state = await CalendarStateAccessor.GetAsync(context, () => new CalendarSkillState());
126-
state.APIToken = "test";
126+
//state.APIToken = "test";
127127
state.EventSource = EventSource.Microsoft;
128128
await bot.OnTurnAsync(context, CancellationToken.None);
129129
});

0 commit comments

Comments
 (0)