Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我们能不能让聊天机器人先打招呼,而不仅仅是作为一种反应_C#_Botframework - Fatal编程技术网

C# 我们能不能让聊天机器人先打招呼,而不仅仅是作为一种反应

C# 我们能不能让聊天机器人先打招呼,而不仅仅是作为一种反应,c#,botframework,C#,Botframework,我正在使用Microsoftt机器人框架和LUIS认知服务开发聊天机器人。 我想要一个初始的欢迎消息,比如“你好,用户,你好!”一旦我的机器人启动 在MessageController中可以执行任何操作 public async Task<HttpResponseMessage> Post([FromBody]Activity activity) { Trace.TraceInformation($"Type={activity.Type} T

我正在使用Microsoftt机器人框架和LUIS认知服务开发聊天机器人。 我想要一个初始的欢迎消息,比如“你好,用户,你好!”一旦我的机器人启动

在MessageController中可以执行任何操作

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            Trace.TraceInformation($"Type={activity.Type} Text={activity.Text}");

            if (activity.Type == ActivityTypes.Message)
            {
                //await Microsoft.Bot.Builder.Dialogs.Conversation.SendAsync(activity, () => new ContactOneDialog());

                await Microsoft.Bot.Builder.Dialogs.Conversation.SendAsync(activity, () =>
                new ExceptionHandlerDialog<object>(new ShuttleBusDialog(), displayException: true));

                //await Microsoft.Bot.Builder.Dialogs.Conversation.SendAsync(activity, () => new ShuttleBusDialog());
            }
            else
            {
                HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(System.Net.HttpStatusCode.OK);
            return response;
        }
公共异步任务发布([FromBody]活动)
{
Trace.TraceInformation($“Type={activity.Type}Text={activity.Text}”);
if(activity.Type==ActivityTypes.Message)
{
//等待Microsoft.Bot.Builder.Dialogs.Conversation.SendAsync(活动,()=>new ContactOneDialog());
等待Microsoft.Bot.Builder.Dialogs.Conversation.SendAsync(活动,()=>
new ExceptionHandlerDialog(new ShuttleBusDialog(),displayException:true));
//等待Microsoft.Bot.Builder.Dialogs.Conversation.SendAsync(活动,()=>new ShuttleBusDialog());
}
其他的
{
HandleSystemMessage(活动);
}
var response=Request.CreateResponse(System.Net.HttpStatusCode.OK);
返回响应;
}

您可能希望探索将消息作为
会话更新
事件的一部分发送。更新您的
HandleSystemMessage
方法,使其如下所示:

    private async Task HandleSystemMessage(Activity message)
    {
        if (message.Type == ActivityTypes.DeleteUserData)
        {
            // Implement user deletion here
            // If we handle user deletion, return a real message
        }
        else if (message.Type == ActivityTypes.ConversationUpdate)
        {
            ConnectorClient client = new ConnectorClient(new Uri(message.ServiceUrl));

            var reply = message.CreateReply();

            reply.Text = "Hello user how are you?"

            await client.Conversations.ReplyToActivityAsync(reply);
        }
        else if (message.Type == ActivityTypes.ContactRelationUpdate)
        {
            // Handle add/remove from contact lists
            // Activity.From + Activity.Action represent what happened
        }
        else if (message.Type == ActivityTypes.Typing)
        {
            // Handle knowing tha the user is typing
        }
        else if (message.Type == ActivityTypes.Ping)
        {
        }
    }

在新版本中,HandleSystemMessage不再是异步的,它返回一个活动,因此这对我来说是有效的:

 private Activity HandleSystemMessage(Activity message)
    {
        if (message.Type == ActivityTypes.DeleteUserData)
        {
            // Implement user deletion here
            // If we handle user deletion, return a real message
        }
        else if (message.Type == ActivityTypes.ConversationUpdate)
        {
            if (message.MembersAdded.Any(o => o.Id == message.Recipient.Id))
            {
                ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));
                Activity reply = message.CreateReply("I am your service provider virtual assistant, How can I help you today? ");
                connector.Conversations.ReplyToActivityAsync(reply);
            }

        }
        else if (message.Type == ActivityTypes.ContactRelationUpdate)
        {
            // Handle add/remove from contact lists
            // Activity.From + Activity.Action represent what happened
        }
        else if (message.Type == ActivityTypes.Typing)
        {
            // Handle knowing tha the user is typing
        }
        else if (message.Type == ActivityTypes.Ping)
        {
        }

        return null;
    }
请注意以下事项:

  • 由于HandleSystemMessage不是异步的,所以您不能“等待”回复
  • 使用收件人id检查以避免重复的欢迎邮件

  • 既然可以在
    Post
    方法中正确地处理它,为什么还要修改
    HandleSystemMessage
    方法呢

    因此,您不必创建新的
    连接器
    和使用不熟悉的
    连接器.Conversations.ReplyToActivityAsync(reply)
    方法

    您可以简单地启动根目录对话框,与回复消息时的操作相同:

        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            // ... some code ...
    
            // this will also handle the beginning of the conversation - 
            // that is when activity.Type equals to ConversationUpdate
            if (activity.Type == ActivityTypes.Message 
                || activity.Type == ActivityTypes.ConversationUpdate)
            {
                // Because ConversationUpdate activity is received twice 
                // (once for the Bot being added to the conversation, and the 2nd time - 
                // for the user), we have to filter one out, if we don't want the dialog 
                // to get started twice. Otherwise the user will receive a duplicate message.
                if (activity.Type == ActivityTypes.ConversationUpdate && 
                   !activity.MembersAdded.Any(r => r.Name == "Bot"))
                    return response;
    
                // start your root dialog here
                await Microsoft.Bot.Builder.Dialogs.Conversation.SendAsync(activity, () =>
                new ExceptionHandlerDialog<object>(new ShuttleBusDialog(), displayException: true));
            }
            // handle other types of activity here (other than Message and ConversationUpdate 
            // activity types)
            else
            {
                HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(System.Net.HttpStatusCode.OK);
            return response;
        }
    
    公共异步任务发布([FromBody]活动)
    {
    //…一些代码。。。
    //这也将处理对话的开始-
    //此时activity.Type等于ConversationUpdate
    if(activity.Type==ActivityTypes.Message)
    ||activity.Type==ActivityTypes.ConversationUpdate)
    {
    //因为ConversationUpdate活动会收到两次
    //(一次用于将机器人添加到对话中,第二次-
    //对于用户),如果我们不想要对话框,我们必须过滤掉一个
    //两次启动。否则用户将收到重复消息。
    如果(activity.Type==ActivityTypes.ConversationUpdate&&
    !activity.MembersAdded.Any(r=>r.Name==“Bot”))
    返回响应;
    //在这里启动根目录对话框
    等待Microsoft.Bot.Builder.Dialogs.Conversation.SendAsync(活动,()=>
    new ExceptionHandlerDialog(new ShuttleBusDialog(),displayException:true));
    }
    //在此处处理其他类型的活动(消息和会话更新除外
    //活动类型)
    其他的
    {
    HandleSystemMessage(活动);
    }
    var response=Request.CreateResponse(System.Net.HttpStatusCode.OK);
    返回响应;
    }
    
    当我使用“WebChat”频道时,下面的代码运行良好。聊天机器人可以启动问候语,而且没有重复

    case ActivityTypes.ConversationUpdate:
    IConversationUpdateActivity update = activity;
    var client = new ConnectorClient(new Uri(activity.ServiceUrl), new MicrosoftAppCredentials());
    if (update.MembersAdded != null && update.MembersAdded.Any())
    {
        foreach (var newMember in update.MembersAdded)
        {
            if (newMember.Id == activity.Recipient.Id)
            {
                var reply = activity.CreateReply();
                reply.Text = $"Welcome {newMember.Name}!";
                await client.Conversations.ReplyToActivityAsync(reply);
            }
        }
    }
    break;
    

    “我反复收到两次回复。你知道我错在哪里吗?”莫汉维夫说