33
44using System ;
55using System . Collections . Generic ;
6+ using System . Linq ;
67using System . Threading ;
78using System . Threading . Tasks ;
89using Microsoft . Bot . Builder ;
910using Microsoft . Bot . Builder . Dialogs ;
1011using Microsoft . Bot . Builder . Dialogs . Choices ;
12+ using Microsoft . Bot . Builder . Integration . AspNet . Core . Skills ;
1113using Microsoft . Bot . Builder . Teams ;
1214using Microsoft . Bot . Connector ;
15+ using Microsoft . Bot . Connector . Authentication ;
1316using Microsoft . Bot . Schema ;
17+ using Microsoft . Bot . Schema . Teams ;
1418using Microsoft . Bot . Solutions ;
1519using Microsoft . Bot . Solutions . Responses ;
20+ using Microsoft . Bot . Solutions . Skills ;
21+ using Microsoft . Bot . Solutions . Skills . Models ;
22+ using Microsoft . Extensions . Configuration ;
1623using Microsoft . Extensions . DependencyInjection ;
24+ using Microsoft . Extensions . Logging ;
25+ using $safeprojectname $. Extensions ;
1726using $safeprojectname $. Models ;
1827
1928namespace $safeprojectname $. Bots
@@ -27,8 +36,13 @@ public class DefaultActivityHandler<T> : TeamsActivityHandler
2736 private readonly IStatePropertyAccessor < DialogState > _dialogStateAccessor ;
2837 private readonly IStatePropertyAccessor < UserProfileState > _userProfileState ;
2938 private readonly LocaleTemplateManager _templateManager ;
39+ private readonly SkillHttpClient _skillHttpClient ;
40+ private readonly SkillsConfiguration _skillsConfig ;
41+ private readonly IConfiguration _configuration ;
42+ private readonly string _virtualAssistantBotId ;
43+ private readonly ILogger _logger ;
3044
31- public DefaultActivityHandler ( IServiceProvider serviceProvider , T dialog )
45+ public DefaultActivityHandler ( IServiceProvider serviceProvider , ILogger < DefaultActivityHandler < T > > logger , T dialog )
3246 {
3347 _dialog = dialog ;
3448 _dialog . TelemetryClient = serviceProvider . GetService < IBotTelemetryClient > ( ) ;
@@ -37,6 +51,11 @@ public DefaultActivityHandler(IServiceProvider serviceProvider, T dialog)
3751 _dialogStateAccessor = _conversationState . CreateProperty < DialogState > ( nameof ( DialogState ) ) ;
3852 _userProfileState = _userState . CreateProperty < UserProfileState > ( nameof ( UserProfileState ) ) ;
3953 _templateManager = serviceProvider . GetService < LocaleTemplateManager > ( ) ;
54+ _skillHttpClient = serviceProvider . GetService < SkillHttpClient > ( ) ;
55+ _skillsConfig = serviceProvider . GetService < SkillsConfiguration > ( ) ;
56+ _configuration = serviceProvider . GetService < IConfiguration > ( ) ;
57+ _virtualAssistantBotId = _configuration . GetSection ( MicrosoftAppCredentials . MicrosoftAppIdKey ) ? . Value ;
58+ _logger = logger ;
4059 }
4160
4261 public override async Task OnTurnAsync ( ITurnContext turnContext , CancellationToken cancellationToken = default )
@@ -104,9 +123,49 @@ protected override async Task OnEventActivityAsync(ITurnContext<IEventActivity>
104123 }
105124 }
106125
126+ // Invoked when a "task/fetch" event is received to invoke task module.
127+ protected override async Task < TaskModuleResponse > OnTeamsTaskModuleFetchAsync ( ITurnContext < IInvokeActivity > turnContext , TaskModuleRequest taskModuleRequest , CancellationToken cancellationToken )
128+ {
129+ return await this . ProcessTaskModuleInvokeAsync ( turnContext , cancellationToken ) ;
130+ }
131+
132+ // Invoked when a 'task/submit' invoke activity is received for task module submit actions.
133+ protected override async Task < TaskModuleResponse > OnTeamsTaskModuleSubmitAsync ( ITurnContext < IInvokeActivity > turnContext , TaskModuleRequest taskModuleRequest , CancellationToken cancellationToken )
134+ {
135+ return await this . ProcessTaskModuleInvokeAsync ( turnContext , cancellationToken ) ;
136+ }
137+
107138 protected override async Task OnEndOfConversationActivityAsync ( ITurnContext < IEndOfConversationActivity > turnContext , CancellationToken cancellationToken )
108139 {
109140 await _dialog . RunAsync ( turnContext , _dialogStateAccessor , cancellationToken ) ;
110141 }
142+
143+ private async Task < TaskModuleResponse > ProcessTaskModuleInvokeAsync ( ITurnContext < IInvokeActivity > turnContext , CancellationToken cancellationToken )
144+ {
145+ try
146+ {
147+ // Get Skill From TaskInvoke
148+ var skillId = ( turnContext . Activity as Activity ) . AsInvokeActivity ( ) . GetSkillId ( _logger ) ;
149+ var skill = _skillsConfig . Skills . Where ( s => s . Value . AppId == skillId ) . FirstOrDefault ( ) . Value ;
150+
151+ // Forward request to correct skill
152+ var invokeResponse = await _skillHttpClient . PostActivityAsync ( _virtualAssistantBotId , skill , _skillsConfig . SkillHostEndpoint , turnContext . Activity as Activity , cancellationToken ) . ConfigureAwait ( false ) ;
153+
154+ // Temporary workaround to get correct invokeresponse
155+ // issue: https://github.com/microsoft/botframework-sdk/issues/5929
156+ var response = new InvokeResponse ( )
157+ {
158+ Status = invokeResponse . Status ,
159+ Body = ( ( Microsoft . Bot . Builder . InvokeResponse < object > ) invokeResponse ) . Body
160+ } ;
161+
162+ return response . GetTaskModuleResponse ( ) ;
163+ }
164+ catch
165+ {
166+ await turnContext . SendActivityAsync ( _templateManager . GenerateActivityForLocale ( "ErrorMessage" ) ) ;
167+ throw ;
168+ }
169+ }
111170 }
112171}
0 commit comments