0
votes

I have a bot that I have built using the bot framework v4. I am accessing the bot using the MS webchat.

I am trying to send an event activity from the bot to the chat client. I get an error when I make the final call to send using SendToConversationAsync.

The code I am using is identical to how I successfully did this in bot framework v3.

Can someone shed some light on what I might be doing wrong here?

This is the code I use to send the event

internal async Task SendSpecialEvent(Activity activity)
    {

        Activity clearMessage = activity.CreateReply();
        clearMessage.Type = "event";
        clearMessage.Value = "executeSpecialEvent";

        var connector = new ConnectorClient(new Uri(activity.ServiceUrl));

        await connector.Conversations.SendToConversationAsync(clearMessage);
    }

The error message is

"Value cannot be null.\r\nParameter name: clientId"

This is the stack trace from the exception. The error has something to do with the appid/password for the bot but I'm not sure why that is. The bot is working perfectly fine up to this point.

at Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential..ctor(String clientId, String clientSecret) at Microsoft.Bot.Connector.Authentication.MicrosoftAppCredentials.<>c__DisplayClass6_0.<.ctor>b__0() in D:\a\1\s\libraries\Microsoft.Bot.Connector\Authentication\MicrosoftAppCredentials.cs:line 71 at System.Lazy1.ViaFactory(LazyThreadSafetyMode mode) at System.Lazy1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) at System.Lazy1.CreateValue() at Microsoft.Bot.Connector.Authentication.MicrosoftAppCredentials.GetTokenAsync(Boolean forceRefresh) in D:\a\1\s\libraries\Microsoft.Bot.Connector\Authentication\MicrosoftAppCredentials.cs:line 158 at Microsoft.Bot.Connector.Authentication.MicrosoftAppCredentials.ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken) in D:\a\1\s\libraries\Microsoft.Bot.Connector\Authentication\MicrosoftAppCredentials.cs:line 143 at Microsoft.Bot.Connector.Conversations.ReplyToActivityWithHttpMessagesAsync(String conversationId, String activityId, Activity activity, Dictionary2 customHeaders, CancellationToken cancellationToken) in D:\a\1\s\libraries\Microsoft.Bot.Connector\Conversations.cs:line 1176 at Microsoft.Bot.Connector.ConversationsExtensions.ReplyToActivityAsync(IConversations operations, String conversationId, String activityId, Activity activity, CancellationToken cancellationToken) in D:\a\1\s\libraries\Microsoft.Bot.Connector\ConversationsExtensions.cs:line 241 at WestRockServiceDeskBot.Utility.ExtServices.SendClearChatHistoryEvent(Activity activity, IWRLogger _logger) in C:\dev\Personal\WestRockServiceDeskBot\WestRockServiceDeskBot\Utility\ExtServices.cs:line 58 at WestRockServiceDeskBot.Dialogs.MainDialog.FinalizeConversation(WaterfallStepContext stepContext, CancellationToken cancellationToken) in C:\dev\Personal\WestRockServiceDeskBot\WestRockServiceDeskBot\Dialogs\Main\MainDialog.cs:line 174 at Microsoft.Bot.Builder.Dialogs.WaterfallDialog.OnStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) in D:\a\1\s\libraries\Microsoft.Bot.Builder.Dialogs\WaterfallDialog.cs:line 166 at Microsoft.Bot.Builder.Dialogs.WaterfallDialog.RunStepAsync(DialogContext dc, Int32 index, DialogReason reason, Object result, CancellationToken cancellationToken) in D:\a\1\s\libraries\Microsoft.Bot.Builder.Dialogs\WaterfallDialog.cs:line 188 at Microsoft.Bot.Builder.Dialogs.WaterfallDialog.ResumeDialogAsync(DialogContext dc, DialogReason reason, Object result, CancellationToken cancellationToken) in D:\a\1\s\libraries\Microsoft.Bot.Builder.Dialogs\WaterfallDialog.cs:line 113 at Microsoft.Bot.Builder.Dialogs.DialogContext.EndDialogAsync(Object result, CancellationToken cancellationToken) in D:\a\1\s\libraries\Microsoft.Bot.Builder.Dialogs\DialogContext.cs:line 192 at Microsoft.Bot.Builder.Dialogs.ComponentDialog.ContinueDialogAsync(DialogContext outerDc, CancellationToken cancellationToken) in D:\a\1\s\libraries\Microsoft.Bot.Builder.Dialogs\ComponentDialog.cs:line 90 at Microsoft.Bot.Builder.Dialogs.DialogContext.ContinueDialogAsync(CancellationToken cancellationToken) in D:\a\1\s\libraries\Microsoft.Bot.Builder.Dialogs\DialogContext.cs:line 145 at Microsoft.Bot.Builder.Dialogs.ComponentDialog.ContinueDialogAsync(DialogContext outerDc, CancellationToken cancellationToken) in D:\a\1\s\libraries\Microsoft.Bot.Builder.Dialogs\ComponentDialog.cs:line 86 at Microsoft.Bot.Builder.Dialogs.DialogContext.ContinueDialogAsync(CancellationToken cancellationToken) in D:\a\1\s\libraries\Microsoft.Bot.Builder.Dialogs\DialogContext.cs:line 145 at WestRockServiceDeskBot.Bot.OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken) in C:\dev\Personal\WestRockServiceDeskBot\WestRockServiceDeskBot\Bot.cs:line 54 at Bot.Builder.Community.Middleware.SpellCheck.SpellCheckMiddleware.OnTurnAsync(ITurnContext context, NextDelegate next, CancellationToken cancellationToken) at Microsoft.Bot.Builder.AutoSaveStateMiddleware.OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken) in D:\a\1\s\libraries\Microsoft.Bot.Builder\AutoSaveStateMiddleware.cs:line 60 at WestRockServiceDeskBot.Middleware.SetLocaleMiddleware.OnTurnAsync(ITurnContext context, NextDelegate next, CancellationToken cancellationToken) in C:\dev\Personal\WestRockServiceDeskBot\WestRockServiceDeskBot\Middleware\SetLocaleMiddleware.cs:line 26 at Microsoft.Bot.Builder.ShowTypingMiddleware.OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken) in D:\a\1\s\libraries\Microsoft.Bot.Builder\ShowTypingMiddleware.cs:line 71 at Microsoft.Bot.Builder.TranscriptLoggerMiddleware.OnTurnAsync(ITurnContext turnContext, NextDelegate nextTurn, CancellationToken cancellationToken) in D:\a\1\s\libraries\Microsoft.Bot.Builder\TranscriptLoggerMiddleware.cs:line 108 at WestRockServiceDeskBot.Middleware.Telemetry.TelemetryLoggerMiddleware.OnTurnAsync(ITurnContext context, NextDelegate nextTurn, CancellationToken cancellationToken) in C:\dev\Personal\WestRockServiceDeskBot\WestRockServiceDeskBot\Middleware\Telemetry\TelemetryLoggerMiddleware.cs:line 115 at Microsoft.Bot.Builder.MiddlewareSet.ReceiveActivityWithStatusAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken cancellationToken) in D:\a\1\s\libraries\Microsoft.Bot.Builder\MiddlewareSet.cs:line 55 at Microsoft.Bot.Builder.BotAdapter.RunPipelineAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken cancellationToken) in D:\a\1\s\libraries\Microsoft.Bot.Builder\BotAdapter.cs:line 167

OK, I found something that works I've change the code to use ReplyToActivityAsync() instead of using the ConnectorClient. This new code looks like this

Activity clearMessage = stepContext.Context.Activity.CreateReply();
clearMessage.Type = "event";
clearMessage.Value = "executeSpecialEvent";               
await stepContext.Context.SendActivityAsync(clearMessage, 
cancellationToken);

I would still like to learn why my original code doesn't work with Botframework v4

1

1 Answers

0
votes

I believe this is due to authentication changes to bot framework SDK, in which this can occasionally happen if the app id and password are not set up in the app web. You can ensure these are set up in your app web settings.

enter image description here

If it is not set up and you need to get those values, you can find out how to do so from steps in this blog.