Botframework-webchat: Web Chat - Bot Initiates Conversation

Created on 23 May 2017  路  3Comments  路  Source: microsoft/BotFramework-WebChat

Hi, Guys.

Is there a way to make the bot in the Web Chat initiate the conversation itself? i.e. say when the page (iframe) loads?

Most helpful comment

In the c# sdk you can respond to the ConversationUpdate activity type:

else if (message.GetActivityType() == 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 a bot!");                            
                await connector.Conversations.ReplyToActivityAsync(reply);
            }
        }
    }
}

Here's a node example as well:

bot.on('conversationUpdate', function (message) {
    if (message.membersAdded && message.membersAdded.length > 0) {
        var membersAdded = message.membersAdded
            .map(function (m) {
                var isSelf = m.id === message.address.bot.id;
                return (isSelf ? message.address.bot.name : m.name) || '' + ' (Id: ' + m.id + ')';
            })
            .join(', ');

        bot.send(new builder.Message()
            .address(message.address)
            .text('Welcome ' + membersAdded));
    }
});

Note: not all channels send a ConversationUpdate event.

All 3 comments

(reference in this site: https://botchattest.herokuapp.com/)

In the c# sdk you can respond to the ConversationUpdate activity type:

else if (message.GetActivityType() == 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 a bot!");                            
                await connector.Conversations.ReplyToActivityAsync(reply);
            }
        }
    }
}

Here's a node example as well:

bot.on('conversationUpdate', function (message) {
    if (message.membersAdded && message.membersAdded.length > 0) {
        var membersAdded = message.membersAdded
            .map(function (m) {
                var isSelf = m.id === message.address.bot.id;
                return (isSelf ? message.address.bot.name : m.name) || '' + ' (Id: ' + m.id + ')';
            })
            .join(', ');

        bot.send(new builder.Message()
            .address(message.address)
            .text('Welcome ' + membersAdded));
    }
});

Note: not all channels send a ConversationUpdate event.

Closing as this question appears to have been answered.

Was this page helpful?
0 / 5 - 0 ratings