问题
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