Microsoft bot主动消息传递 - 无法从组转换为字符串 - IMessageActivity转换为字符串

时间:2017-06-17 08:27:23

标签: c# botframework luis

我正在尝试通过根据提示对话框中断来主动向用户发送消息的机器人 https://github.com/Microsoft/BotBuilder-Samples/tree/master/CSharp/core-proactiveMessages

我有以下LUIS方法实现,它完美地工作,它在会话期间中断用户,因为每个定时器设置,这是3秒 - 因此,3秒后,僵尸程序会中断用户另一个提示(未在下面的代码中添加 - 它只是一个向用户提供2个选项的提示 - 保持主动提示或返回上一个对话)。

    [LuisIntent("MyIntent")]
    public async Task MyIntent(IDialogContext context, IAwaitable<IMessageActivity> res, LuisResult result)
    {
        var message = await res;
        try
        {
            await context.PostAsync("I see that you have below options <br/> 1. Do first task <br/> 2. Do second task <br/> 3. Do third task " + count);

            var conversationReference = message.ToConversationReference();
            ConversationStarter.conversationReference = JsonConvert.SerializeObject(conversationReference);

            //Prepare the timer to simulate a background/asynchonous process
            t = new Timer(new TimerCallback(timerEvent));
            t.Change(3000, Timeout.Infinite);

            var url = HttpContext.Current.Request.Url;
            //We echo the message regardless
            await context.PostAsync("In a few seconds I'll interrupt this dialog and bring another one with a prompt.");
            PromptDialog.Text(context, taskdoer, "You can ask me like - <br/>Do task 2<br/>or simply enter 2");
        }
        catch (Exception e)
        {
            await context.PostAsync("Error is <br/> " + e.ToString());
            context.Wait(MessageReceived);
        }
    }

    private async Task taskdoer(IDialogContext context, IAwaitable<string> result)
    {
        string strTaskNumber = await result;
                if (strTaskNumber == "2")
                {
                    await context.PostAsync("So, you have entered  " + strTaskNumber);
                    await context.PostAsync("This is Task 2");
                    context.Wait(MessageReceived);
                }
                if (strTaskNumber == "3")
                {
                    await context.PostAsync("So, you have entered  " + strTaskNumber);
                    await context.PostAsync("This is Task 3");
                    context.Wait(MessageReceived);
                }
    }

我想要实现的是,当用户输入2时,在taskdoer方法中实现主动对话的计时器,如下所示 -

    private async Task taskdoer(IDialogContext context,IAwaitable<IMessageActivity> res, IAwaitable<string> result)
    {
        string strTaskNumber = await result;
                if (strTaskNumber == "2")
                {

                    await context.PostAsync("So, you have entered  " + strTaskNumber);
                    await context.PostAsync("This is Task 2");
        var message = await res;
            await context.PostAsync("I see that you have below options <br/> 1. Do first task <br/> 2. Do second task <br/> 3. Do third task " + count);

            var conversationReference = message.ToConversationReference();
            ConversationStarter.conversationReference = JsonConvert.SerializeObject(conversationReference);

            //Prepare the timer to simulate a background/asynchonous process
            t = new Timer(new TimerCallback(timerEvent));
            t.Change(3000, Timeout.Infinite);

            var url = HttpContext.Current.Request.Url;
            //We echo the message regardless
            await context.PostAsync("In a few seconds I'll interrupt this dialog and bring another one with a prompt.");
            PromptDialog.Text(context, taskdoer, "You can ask me like - <br/>Do task 2<br/>or simply enter 2");            
                    context.Wait(MessageReceived);
                }
                ....
    }

当我这样做时,我收到错误&#34;无法转换来自&#39;方法组&#39;到&#39;恢复后&#39;&#34; 所以,我无法在taskdoer中使用IMessageActivity。有关如何在MyIntent方法中根据用户输入2在taskdoer方法中实现主动消息的任何线索?

1 个答案:

答案 0 :(得分:0)

ResumeAfter<T>的{​​{1}}方法必须始终只有两个参数:PromptIDialogContext(其中IAwaitable<T>的结果将是)。在您的情况Prompt中,由于您使用的是PromptDialog.Text,因此是T

所以你不能通过string res。如果由于某种原因您需要原始活动,则可以使用IAwaitable<IMessageActivity>