我有一个控制器,可以接收来自其他应用的帖子请求。
从那时起,我就能够通过以下方式与添加机器人的用户进行对话:
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);
这允许我在我的ApiController中向用户提示消息,而无需用户首先与机器人发起对话。接下来,我通过调用
将此信息发送给另一个班级 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)
{
}
}
}
}
当我这样做时,我收到以下错误消息,即使在我的API控制器中我确实设置了ReplyToId message.ReplyToId = userAccount.Id
;我之前通过Skype预先主动联系的用户。
:
Microsoft.Rest.ValidationException: 'ReplyToId' cannot be null.
at Microsoft.Bot.Connector.ConversationsExtensions.ReplyToActivityAsync(IConversations operations, Activity activity, CancellationToken cancellationToken)