Skip to content

Commit

Permalink
Merge pull request microsoft#12 from southworkscom/csharp-createnewco…
Browse files Browse the repository at this point in the history
…nversation-exceptionhandling

[CreateNewConversation] Handling HttpOperationException when DirectConversationAsync is not supported
  • Loading branch information
willportnoy authored Oct 1, 2016
2 parents 8c3eefc + 0ff72a9 commit e2fa28d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Builder.Internals.Fibers;
using Microsoft.Bot.Connector;

using Microsoft.Bot.Connector;
using Microsoft.Rest;

[BotAuthentication]
public class MessagesController : ApiController
{
Expand All @@ -34,13 +35,27 @@ public async Task<HttpResponseMessage> Post([FromBody]Activity activity, Cancell
{
ConnectorClient client = new ConnectorClient(new Uri(activity.ServiceUrl));

var conversation = await client.Conversations.CreateDirectConversationAsync(activity.Recipient, activity.From);

var cookie = scope.Resolve<ResumptionCookie>();
cookie.Address = new Address(cookie.Address.BotId, cookie.Address.ChannelId, cookie.Address.UserId, conversation.Id, cookie.Address.ServiceUrl);
ResourceResponse conversation;

var postToBot = scope.Resolve<IPostToBot>();
await postToBot.PostAsync(activity, token);
try
{
conversation = await client.Conversations.CreateDirectConversationAsync(activity.Recipient, activity.From);
}
catch (HttpOperationException ex)
{
var reply = activity.CreateReply();
reply.Text = ex.Message;

await client.Conversations.SendToConversationAsync(reply);

return new HttpResponseMessage(HttpStatusCode.Accepted);
}

var cookie = scope.Resolve<ResumptionCookie>();
cookie.Address = new Address(cookie.Address.BotId, cookie.Address.ChannelId, cookie.Address.UserId, conversation.Id, cookie.Address.ServiceUrl);

var postToBot = scope.Resolve<IPostToBot>();
await postToBot.PostAsync(activity, token);
}
}
else
Expand Down
18 changes: 16 additions & 2 deletions CSharp/core-CreateNewConversation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,28 @@ The minimum prerequisites to run this sample are:
Bot Builder uses dialogs to model a conversational process, the exchange of messages, between bot and user. Conversations are usually initiated by the user but sometimes it might be useful for the bot to proactively start a new dialog to interact with the user.
In this sample starting a new dialog is a two steps process: First, creating the new conversation, and then, passing the control to the new dialog.

Check out the use of the `ConnectorClient.CreateDirectConversationAsync()` method in the [MessagesController.cs](Controllers/MessagesController.cs#L33-L44) class to create a new Bot-to-User conversation. This is then stored in the `ResumptionCookie` for later use.
Check out the use of the `ConnectorClient.CreateDirectConversationAsync()` method in the [MessagesController.cs](Controllers/MessagesController.cs#L34-L59) class to create a new Bot-to-User conversation. This is then stored in the `ResumptionCookie` for later use.

````C#
using (var scope = DialogModule.BeginLifetimeScope(this.scope, activity))
{
ConnectorClient client = new ConnectorClient(new Uri(activity.ServiceUrl));

var conversation = await client.Conversations.CreateDirectConversationAsync(activity.Recipient, activity.From);
ResourceResponse conversation;

try
{
conversation = await client.Conversations.CreateDirectConversationAsync(activity.Recipient, activity.From);
}
catch (HttpOperationException ex)
{
var reply = activity.CreateReply();
reply.Text = ex.Message;

await client.Conversations.SendToConversationAsync(reply);

return new HttpResponseMessage(HttpStatusCode.Accepted);
}

var cookie = scope.Resolve<ResumptionCookie>();
cookie.Address = new Address(cookie.Address.BotId, cookie.Address.ChannelId, cookie.Address.UserId, conversation.Id, cookie.Address.ServiceUrl);
Expand Down

0 comments on commit e2fa28d

Please sign in to comment.