ConversationsEx.cs
1 
2 namespace Microsoft.Bot.Connector
3 {
4  using System;
5  using System.Collections;
6  using System.Collections.Generic;
7  using System.Threading;
8  using System.Threading.Tasks;
9  using Microsoft.Rest;
10 
11 
12  public static partial class ConversationsExtensions
13  {
22  {
23  return Task.Factory.StartNew(s => ((IConversations)s).CreateConversationAsync(GetDirectParameters(bot, user, activity)), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
24  }
25 
34  public static async Task<ConversationResourceResponse> CreateDirectConversationAsync(this IConversations operations, ChannelAccount bot, ChannelAccount user, Activity activity = null, CancellationToken cancellationToken = default(CancellationToken))
35  {
36  var _result = await operations.CreateConversationWithHttpMessagesAsync(GetDirectParameters(bot, user, activity), null, cancellationToken).ConfigureAwait(false);
37  return await _result.HandleErrorAsync<ConversationResourceResponse>().ConfigureAwait(false);
38  }
39 
47  public static ConversationResourceResponse CreateDirectConversation(this IConversations operations, string botAddress, string userAddress, Activity activity = null)
48  {
49  return Task.Factory.StartNew(s => ((IConversations)s).CreateConversationAsync(GetDirectParameters(botAddress, userAddress, activity)), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
50  }
51 
60  public static async Task<ConversationResourceResponse> CreateDirectConversationAsync(this IConversations operations, string botAddress, string userAddress, Activity activity = null, CancellationToken cancellationToken = default(CancellationToken))
61  {
62  var _result = await operations.CreateConversationWithHttpMessagesAsync(GetDirectParameters(botAddress, userAddress, activity), null, cancellationToken).ConfigureAwait(false);
63  return await _result.HandleErrorAsync<ConversationResourceResponse>().ConfigureAwait(false);
64  }
65 
75  public static ResourceResponse SendToConversation(this IConversations operations, Activity activity)
76  {
77  return Task.Factory.StartNew(s => ((IConversations)s).SendToConversationAsync(activity, activity.Conversation.Id), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
78  }
79 
92  public static Task<ResourceResponse> SendToConversationAsync(this IConversations operations, Activity activity, CancellationToken cancellationToken = default(CancellationToken))
93  {
94  return operations.SendToConversationAsync(activity, activity.Conversation.Id, cancellationToken);
95  }
96 
106  public static ResourceResponse ReplyToActivity(this IConversations operations, Activity activity)
107  {
108  return Task.Factory.StartNew(s => ((IConversations)s).ReplyToActivityAsync(activity.Conversation.Id, activity.ReplyToId, activity), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
109  }
110 
123  public static Task<ResourceResponse> ReplyToActivityAsync(this IConversations operations, Activity activity, CancellationToken cancellationToken = default(CancellationToken))
124  {
125  // TEMP TODO REMOVE THIS AFTER SKYPE DEPLOYS NEW SERVICE WHICH PROPERLY IMPLEMENTS THIS ENDPOINT
126  if (activity.ReplyToId == "0")
127  return operations.SendToConversationAsync(activity);
128 
129  if (activity.ReplyToId == null)
130  {
131  throw new ValidationException(ValidationRules.CannotBeNull, "ReplyToId");
132  }
133 
134  return operations.ReplyToActivityAsync(activity.Conversation.Id, activity.ReplyToId, activity, cancellationToken);
135  }
136 
146  public static ResourceResponse UpdateActivity(this IConversations operations, Activity activity)
147  {
148  return Task.Factory.StartNew(s => ((IConversations)s).UpdateActivityAsync(activity.Conversation.Id, activity.Id, activity), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
149  }
150 
163  public static Task<ResourceResponse> UpdateActivityAsync(this IConversations operations, Activity activity, CancellationToken cancellationToken = default(CancellationToken))
164  {
165  return operations.UpdateActivityAsync(activity.Conversation.Id, activity.Id, activity, cancellationToken);
166  }
167 
168 
169  private static ConversationParameters GetDirectParameters(string botId, string userId, Activity activity)
170  {
171  return new ConversationParameters()
172  {
173  Bot = new ChannelAccount(botId),
174  Members = new ChannelAccount[] { new ChannelAccount(userId) },
175  Activity = activity
176  };
177  }
178 
179  private static ConversationParameters GetDirectParameters(ChannelAccount bot, ChannelAccount user, Activity activity)
180  {
181  return new ConversationParameters()
182  {
183  Bot = bot,
184  Members = new ChannelAccount[] { user },
185  Activity = activity
186  };
187  }
188 
189  }
190 }
static ResourceResponse UpdateActivity(this IConversations operations, Activity activity)
Update an activity in an existing conversation
static Task< ResourceResponse > UpdateActivityAsync(this IConversations operations, Activity activity, CancellationToken cancellationToken=default(CancellationToken))
Update an activity in an existing conversation
static ResourceResponse SendToConversation(this IConversations operations, Activity activity)
Send an activity to a conversation
string Id
ID of this activity
Definition: Activity.cs:73
ConversationAccount Conversation
Conversation
Definition: Activity.cs:111
static Task< ResourceResponse > ReplyToActivityAsync(this IConversations operations, Activity activity, CancellationToken cancellationToken=default(CancellationToken))
Reply to an activity in an existing conversation
Parameters for creating a new conversation
static async Task< ConversationResourceResponse > CreateDirectConversationAsync(this IConversations operations, string botAddress, string userAddress, Activity activity=null, CancellationToken cancellationToken=default(CancellationToken))
Create a new direct conversation between a bot and a user
static ConversationResourceResponse CreateDirectConversation(this IConversations operations, string botAddress, string userAddress, Activity activity=null)
Create a new direct conversation between a bot and a user
An Activity is the basic communication type for the Bot Framework 3.0 protocol
Definition: ActivityEx.cs:18
string ReplyToId
The original ID this message is a response to
Definition: Activity.cs:223
A response containing a resource ID
static async Task< ConversationResourceResponse > CreateDirectConversationAsync(this IConversations operations, ChannelAccount bot, ChannelAccount user, Activity activity=null, CancellationToken cancellationToken=default(CancellationToken))
Create a new direct conversation between a bot and a user
static ResourceResponse ReplyToActivity(this IConversations operations, Activity activity)
Replyto an activity in an existing conversation
string Id
Channel id for the user or bot on this channel (Example: joe@smith.com, or or 123456) ...
Task< HttpOperationResponse< object > > CreateConversationWithHttpMessagesAsync(ConversationParameters parameters, Dictionary< string, List< string >> customHeaders=null, CancellationToken cancellationToken=default(CancellationToken))
CreateConversation
static Task< ResourceResponse > SendToConversationAsync(this IConversations operations, Activity activity, CancellationToken cancellationToken=default(CancellationToken))
Send an activity to a conversation
Channel account information needed to route a message
static ConversationResourceResponse CreateDirectConversation(this IConversations operations, ChannelAccount bot, ChannelAccount user, Activity activity=null)
Create a new direct conversation between a bot and a user