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

一世执手 提交于 2019-12-20 02:33:34

问题


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?


回答1:


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);
    }
}



回答2:


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;
            }


来源:https://stackoverflow.com/questions/42943665/how-to-display-a-welcome-message-from-my-bot-using-microsoft-bot-framework

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!