Proactive Bot create PromptDialog.Choice

梦想与她 提交于 2020-01-04 07:44:50

问题


I have a controller that receives post requests from another app.

From that I was able to initiate a conversation with a user that added the bot by doing:

ChannelAccount userAccount = new ChannelAccount();
ChannelAccount botAccount = new ChannelAccount();
IMessageActivity message = Activity.CreateMessageActivity();
userAccount = new ChannelAccount(id: "a", name: "b");
botAccount = new ChannelAccount(id: "28:...", name: "bot1");
var connector = new ConnectorClient("uriexample");
var conversationId = await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount);
message.From = botAccount;
message.Recipient = userAccount;
message.Conversation = new ConversationAccount(id: conversationId.Id);
message.ReplyToId = userAccount.Id;
message.Text = "text";
message.Locale = "en-Us";
await connector.Conversations.SendToConversationAsync((Activity)message);

This allowed me to prompt a message to the user in my ApiController without the user initiating a conversation first with the bot. Next I send this information to another class by calling:

 await Conversation.SendAsync(message, () => new Dialogs.NewDialog());

NewDialog:

[Serializable]
    public class NewDialog : IDialog<object>
    {
        public async Task StartAsync(IDialogContext context)
        {
            ChannelAccount userAccount = new ChannelAccount();
            ChannelAccount botAccount = new ChannelAccount();
            IMessageActivity message = Activity.CreateMessageActivity();
            userAccount = new ChannelAccount(id:"a", name: "b");
            botAccount = new ChannelAccount(id: "28:...", name: "bot1");
            var connector = new ConnectorClient("uriexample");
            var conversationId = await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount);

            PromptOptions<string> options = new PromptOptions<string>("Hello! yes or no?",
                "Not valid",
                "Exceeded",
                true ?
                new List<string>() { AppConstants.YesOption, AppConstants.NoFraudOption, AppConstants.ExitOption, } :
                new List<string>() { AppConstants.YesOption, AppConstants.ExitOption, },
                3);
                PromptDialog.Choice(context, this.OnOptionSelec, options);

         }

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

            context.Wait(MessageReceivedAsync);
        }

        private async Task OnOptionSelec(IDialogContext context, IAwaitable<string> result)
        {
            try
            {
                string optionSelected = await result;

                switch (optionSelected)
                {
                    case AppConstants.YesOption:
                         context.Call(new YesDialog(), this.ResumeAfterOptionDialog);
                         break;

                    case AppConstants.NoOption:
                         context.Call(new NoDialog(), this.ResumeAfterOptionDialog);
                         break;

                    case AppConstants.ExitOption:
                         context.Call(new ExitDialog(), this.ResumeAfterOptionDialog);
                         break;
                }
            }
            catch (TooManyAttemptsException)
            {
            }
        }
    }
}

When I do this I get the following error message even though in my API Controller I did set the ReplyToId message.ReplyToId = userAccount.Id; to the user I previously proactively contacted via skype. :

Microsoft.Rest.ValidationException: 'ReplyToId' cannot be null.
   at Microsoft.Bot.Connector.ConversationsExtensions.ReplyToActivityAsync(IConversations operations, Activity activity, CancellationToken cancellationToken)

来源:https://stackoverflow.com/questions/43572137/proactive-bot-create-promptdialog-choice

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