基于Microsoft Bot框架中的最后一条消息的持续对话

时间:2017-05-30 12:32:08

标签: botframework

我希望在Microsoft Bot框架中进行持续对话聊天。

if(用户说出问候)
{
     回复= what u want to listen hi or hello
----- if(用户说嗨)
----- {
--------回复= hi
-----}
----如果(用户说你好)
- {
-------回复= hello
---}
}

意味着机器人也应该提出问题并相应地回答它。

1 个答案:

答案 0 :(得分:1)

假设这是C#并且您正在使用基于IDialog<T>的对话框,则可以执行以下操作:

private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
    var message = await result;

    if (message.Text.Equals("hello", StringComparison.InvariantCultureIgnoreCase))
    {
        PromptDialog.Text(context, this.ResumeAfterPrompt, "What u want to listen hi or hello");
    }
    else
    {
        // do something
        context.Wait(this.MessageReceivedAsync);
    }
}

private async Task ResumeAfterPrompt(IDialogContext context, IAwaitable<string> result)
{
    try
    {
        var userMessage = await result;

        switch (userMessage.ToLowerInvariant())
        {
            case "hi":
                await context.PostAsync("hi");
                break;
            case "hello":
                await context.PostAsync("hello");
                break;
            default:
                // do something;
                break;
        }
    }
    catch (TooManyAttemptsException)
    {
        // do something with the exception
    }

    context.Wait(this.MessageReceivedAsync);
}