Team, I have developed a bot using bot framework SDK4. I am using Directline channel to communicate with my bot. My requirement is based on channeldata on 'requestWelcomeDialog' message I have to show welcome message.
Code from my bot client:
BotChat.App({
botConnection: botConnection,
user: userOption,
bot: { id: model.botId, name: model.botName },
resize: 'window',
speechOptions: speechOptions,
locale: 'en',
sendTypingIndicator: true,
}, document.getElementById('BotChatElement'));
PostBotConfiguration();
botConnection
.postActivity({
from: user,
name: 'requestWelcomeDialog',
type: 'event',
value: { 'BotType': 'abcd' }
})
.subscribe(function (id) {
setWCScreenChatPosition();
model.botRender = true;
console.log('"trigger requestWelcomeDialog" sent');
});
In the above code i am sending BotType as 'abcd'. I am trying to read this value from my bot.
My code in bot.
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
Utility util = new Utility();
try
{
foreach (var member in membersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
BotChannelData cdata = new BotChannelData();
turnContext.Activity.TryGetChannelData(out cdata);
}
}
}
catch
{
}
}
In this I am always getting null reference exception.
May I know what I am missing in this?
The first problem is that you're using Bot Chat. Bot Chat is Web Chat v3 and it's deprecated. You should be using Web Chat v4 according to the instructions in the repo.
The second problem is that you're trying to respond to a custom event using OnMembersAddedAsync
which is only triggered by conversation update activities. You can see how to send and respond to welcome events by following the instructions in this issue and this sample. The C# equivalent would look like this:
if (turnContext.Activity.Name == "webchat/join")
{
await turnContext.SendActivityAsync("Back Channel Welcome Message!");
}