如何使用Microsoft Bot Framework从我的Bot显示欢迎消息

时间:2017-03-22 05:45:39

标签: c# botframework

我想在有人连接到我的机器人时显示欢迎信息。我在github(https://github.com/Microsoft/BotBuilder-Samples/tree/master/CSharp/demo-ContosoFlowers)上使用了demo-ContosoFlowers示例中的技术,它在Bot框架模拟器中工作正常,但在Skype或Facebook Messenger中没有。具体来说,MessageController.HandleSystemMessage中的此代码不会触发:

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

有谁知道如何正确地做到这一点?

2 个答案:

答案 0 :(得分:5)

我今天也尝试了ContosoFlowers演示。我遇到了您描述的相同行为:在模拟器中,ConversationUpdate代码被触发但在Skype中却没有。但是,我确实注意到ContactRelationUpdate活动类型在Skype中触发(我还没试过Facebook Messenger)。如果您的目标是在有人“连接”时显示欢迎信息。对于您的机器人,您可以尝试使用ContactRelationUpdate活动类型,如下所示:

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

答案 1 :(得分:0)

创建一个类,并在你的fb回调网址中有这个。 FacebookProfile是我的班级,在通话结束后保存姓名和其他信息。

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