When User Sends Message to My Bot, He Receives Welcome Message. But When User Respond to That, Bot Sends Welcome Message Again. How to Fix This

When user sends message to my bot, he receives Welcome message. But when user respond to that, bot sends Welcome message again. How can I fix this?

I believe this could be related to a change that was rolled out a few days ago; where Direct Line will send more ConversationUpdate messages than it used to.

Check the announcement and a related issue (similar to yours, but in node.js).

The first ConversationUpdate is sent when the bot is added to the conversation.
After that, each additional ConversationUpdate is sent when a new user joins the conversation.

So, I think the solution here will be to check the members added (activity.MembersAdded)

    else if (message.Type == ActivityTypes.ConversationUpdate)
{
if (message.MembersAdded.Any(o => o.Id == message.Recipient.Id))
{
// logic
}
}

Identical Threads in Microsoft Bot are Duplicating Welcoming Message

it's possible that you are returning a message for the bot joining chat and the user as well. It's hard to tell without seeing the code in your conversation update part of the if-else statement in root dialog. You can use the following code to post just a single message

else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
IConversationUpdateActivity iConversationUpdated = message as IConversationUpdateActivity;
if (iConversationUpdated != null)
{
ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));

foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())
{
// if the bot is added, then
if (member.Id == iConversationUpdated.Recipient.Id)
{
var reply = ((Activity) iConversationUpdated).CreateReply(
$"Hi! I'm Botty McBotface and this is a welcome message");
await connector.Conversations.ReplyToActivityAsync(reply);
}
}
}
}

chatbot is working for welcome message fine in local bot framework Emulator, but not work in azure chatbot

You can refer this Code. It will help you.

IConversationUpdateActivity iConversationUpdated = message as IConversationUpdateActivity;
if (iConversationUpdated != null)
{
ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));

foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())
{
// if the bot is added, then
if (member.Id == iConversationUpdated.Recipient.Id)
{
var reply = ((Activity)iConversationUpdated).CreateReply(
$"Hello Bot");
connector.Conversations.ReplyToActivityAsync(reply);
}
}
}

Welcome Message not firing for Microsoft Bot (using framework v4) hosted in LivePerson using Direct Line (NOT WebChat)

The answer is summed up in the blog post I wrote after I figured it out:
https://www.michaelgmccarthy.com/2021/03/13/sending-a-welcome-message-in-the-v4-bot-framework-via-direct-line-and-liveperson/

Sending a greeting/welcome message from the bot as soon as the Webchat control is loaded

For the webchat component, you can use the backchannel functionality to send a hidden message to your bot, in order to launch the greetings.

Heere is a sample of the implementation on the webchat side:

<!DOCTYPE html>
<html>
<head>
<link href="https://cdn.botframework.com/botframework-webchat/latest/botchat.css" rel="stylesheet" />
</head>
<body>
<div id="bot" />
<script src="https://cdn.botframework.com/botframework-webchat/latest/botchat.js"></script>
<script>
// Get parameters from query
const params = BotChat.queryParams(location.search);
// Language definition
var chatLocale = params['locale'] || window.navigator.language;

// Connection settings
const botConnectionSettings = new BotChat.DirectLine({
domain: params['domain'],
secret: 'YOUR_SECRET',
webSocket: params['webSocket'] && params['webSocket'] === 'true'
});

// Webchat init
BotChat.App({
botConnection: botConnectionSettings,
user: { id: 'userid' },
bot: { id: 'botid' },
locale: chatLocale,
resize: 'detect'
}, document.getElementById('bot'));

// Send hidden message to do what you want
botConnectionSettings.postActivity({
type: 'event',
from: { id: 'userid' },
locale: chatLocale,
name: 'myCustomEvent',
value: 'test'
}).subscribe(function (id) { console.log('event sent'); });
</script>
</body>
</html>

On your bot side, you will get this event on your Message Controlelr:

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
// DEMO PURPOSE: echo all incoming activities
Activity reply = activity.CreateReply(Newtonsoft.Json.JsonConvert.SerializeObject(activity, Newtonsoft.Json.Formatting.None));

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

// Process each activity
if (activity.Type == ActivityTypes.Message)
{
await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
}
// Webchat: getting an "event" activity for our js code
else if (activity.Type == ActivityTypes.Event && activity.ChannelId == "webchat")
{
var receivedEvent = activity.AsEventActivity();

if ("myCustomEvent".Equals(receivedEvent.Name, StringComparison.InvariantCultureIgnoreCase))
{
// DO YOUR GREETINGS FROM HERE
}
}
// Sample for Skype: in ContactRelationUpdate event
else if (activity.Type == ActivityTypes.ContactRelationUpdate && activity.ChannelId == "skype")
{
// DO YOUR GREETINGS FROM HERE
}
// Sample for emulator, to debug locales
else if (activity.Type == ActivityTypes.ConversationUpdate && activity.ChannelId == "emulator")
{
foreach (var userAdded in activity.MembersAdded)
{
if (userAdded.Id == activity.From.Id)
{
// DO YOUR GREETINGS FROM HERE
}
}
}

var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}

I made a working demo using this functionality to send the user locale, it's here on Github

Incorrect message processing of PromptDialog.Choice()?

As replied in comment, the solution for your case is to launch your Dialog containing the PromptChoice only when you are notified that a user is joining the conversation.

For that case you can check the MemberAdded info inside conversationUpdate message, and launch your dialog only when the added member is not the bot, that is to say the Id of the member is not equal to the Id of the bot, which is message.Recipient.Id:

private Activity HandleSystemMessage(Activity message)
{
if (message.Type == ActivityTypes.ConversationUpdate)
{
if (message.MembersAdded.Any(o => o.Id != message.Recipient.Id))
{
// Your PromptChoiceDialog here
}
}
return message;
}

Discord bot won't trigger off of a specific user anymore

Hello I found the error and solved it.

It turns out it was due to me setting an embed fields to null instead of []. So when the bot was trying to respond with an embed it would return from the messageSend function.



Related Topics



Leave a reply



Submit