简体   繁体   中英

How to display a welcome message from my Bot using Microsoft Bot Framework

I want to display a welcome message whenever someone connects to my bot. I've used the technique from the demo-ContosoFlowers sample on github ( https://github.com/Microsoft/BotBuilder-Samples/tree/master/CSharp/demo-ContosoFlowers ) which works fine in the Bot Framework Emulator, but not in Skype or Facebook Messenger. Specifically, this code in MessageController.HandleSystemMessage doesn't trigger:

        else if (message.Type == ActivityTypes.ConversationUpdate)
        {
            if (message.MembersAdded.Any(o => o.Id == message.Recipient.Id))
            {
                var reply = message.CreateReply(Resources.RootDialog_Welcome_Message);

                ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));

                await connector.Conversations.ReplyToActivityAsync(reply);
            }
        }

Does anyone know how to do this correctly?

I also tried out the ContosoFlowers demo today. I experienced the same behavior you describe: in the emulator, ConversationUpdate code is triggered but in Skype it is not. However, I did notice that the ContactRelationUpdate activity type does fire in Skype (I haven't tried Facebook Messenger). If your goal is to display a welcome message whenever someone "connects" to your bot, you could try using the ContactRelationUpdate activity type like this:

else if (message.Type == ActivityTypes.ContactRelationUpdate)
{
    if(message.Action == "add")
    {
        var reply = message.CreateReply("WELCOME!!!");
        ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));
        await connector.Conversations.ReplyToActivityAsync(reply);
    }
}

Create a class and have this in you callback url of fb. FacebookProfile is my class that holds the name and other information after the call.

 public static async Task<FacebookProfile> GetFacebookProfile(string accessToken)
            {
                var uri = GetUri("https://graph.facebook.com/v2.6/me",
                    Tuple.Create("fields", "name,email,id,first_name,last_name"),
                    Tuple.Create("access_token", accessToken));

                var res = await FacebookRequest<FacebookProfile>(uri);
                return res;
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM