Skip to content

Commit 49c7f08

Browse files
authored
Merge pull request #29 from tompaana/development_nuget
Official version 1.0
2 parents 8a41c97 + fd6c275 commit 49c7f08

File tree

11 files changed

+103
-102
lines changed

11 files changed

+103
-102
lines changed

IntermediatorBotSample/CommandHandling/CommandCardFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public static HeroCard CreateRequestCard(Party requestorParty, string botName =
115115
}
116116

117117
string requestorChannelAccountName = string.IsNullOrEmpty(requestorParty.ChannelAccount.Name)
118-
? CommandMessageHandler.NoUserNamePlaceholder : requestorParty.ChannelAccount.Name;
118+
? StringAndCharConstants.NoUserNamePlaceholder : requestorParty.ChannelAccount.Name;
119119

120120
string requestorChannelId = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(requestorParty.ChannelId);
121121
string requestorChannelAccountId = requestorParty.ChannelAccount.Id;
@@ -216,7 +216,7 @@ public static HeroCard CreateAcceptOrRejectCardForMultipleRequests(IList<Party>
216216
}
217217

218218
string requestorChannelAccountName = string.IsNullOrEmpty(requestorParty.ChannelAccount.Name)
219-
? CommandMessageHandler.NoUserNamePlaceholder : requestorParty.ChannelAccount.Name;
219+
? StringAndCharConstants.NoUserNamePlaceholder : requestorParty.ChannelAccount.Name;
220220

221221
string requestorChannelId = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(requestorParty.ChannelId);
222222
string requestorChannelAccountId = requestorParty.ChannelAccount.Id;

IntermediatorBotSample/CommandHandling/CommandMessageHandler.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ namespace IntermediatorBotSample.CommandHandling
1717
/// </summary>
1818
public class CommandMessageHandler
1919
{
20-
public static readonly string NoUserNamePlaceholder = "<no user name>";
21-
2220
private MessageRouterManager _messageRouterManager;
2321
private MessageRouterResultHandler _messageRouterResultHandler;
2422

@@ -176,8 +174,6 @@ public async virtual Task<bool> HandleCommandAsync(Activity activity)
176174
#if DEBUG
177175
case string baseCommand when (baseCommand.Equals(Commands.CommandDeleteAllRoutingData)):
178176
// DELETE ALL ROUTING DATA
179-
await _messageRouterManager.BroadcastMessageToAggregationChannelsAsync(
180-
string.Format(ConversationText.DeletingAllDataWithCommandIssuer, senderParty.ChannelAccount?.Name));
181177
replyActivity = activity.CreateReply(ConversationText.DeletingAllData);
182178
_messageRouterManager.RoutingDataManager.DeleteAll();
183179
wasHandled = true;

IntermediatorBotSample/Controllers/MessagesController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
7979
}
8080
}
8181

82+
// Uncomment to see the result in a reply (may be useful for debugging)
83+
//await MessagingUtils.ReplyToActivityAsync(activity, messageRouterResult.ToString());
84+
8285
// Handle the result, if required
8386
await messageRouterResultHandler.HandleResultAsync(messageRouterResult);
8487
}

IntermediatorBotSample/Dialogs/RootDialog.cs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
using Microsoft.Bot.Connector;
55
using System;
66
using System.Threading.Tasks;
7-
using Underscore.Bot.MessageRouting;
87

98
namespace IntermediatorBotSample.Dialogs
109
{
1110
/// <summary>
12-
/// Simple echo dialog that tries to connect with a human, if the message contains the specific command.
11+
/// Simple dialog that will only ever provide simple instructions.
1312
/// </summary>
1413
[Serializable]
1514
public class RootDialog : IDialog<object>
@@ -21,8 +20,7 @@ public Task StartAsync(IDialogContext dialogContext)
2120
}
2221

2322
/// <summary>
24-
/// Responds back to the sender with the instructions or in case the message contains
25-
/// the specific command, will try to connect with a human (in 1:1 conversation).
23+
/// Responds back to the sender with the simple instructions.
2624
/// </summary>
2725
/// <param name="dialogContext">The dialog context.</param>
2826
/// <param name="result">The result containing the message sent by the user.</param>
@@ -33,23 +31,13 @@ private async Task OnMessageReceivedAsync(IDialogContext dialogContext, IAwaitab
3331

3432
if (!string.IsNullOrEmpty(messageText))
3533
{
36-
if (messageText.ToLower().Contains(Commands.CommandRequestConnection))
37-
{
38-
MessageRouterResult messageRouterResult =
39-
WebApiConfig.MessageRouterManager.RequestConnection(
40-
(messageActivity as Activity), WebApiConfig.Settings.RejectConnectionRequestIfNoAggregationChannel);
41-
await WebApiConfig.MessageRouterResultHandler.HandleResultAsync(messageRouterResult);
42-
}
43-
else
44-
{
45-
messageActivity = dialogContext.MakeMessage();
34+
messageActivity = dialogContext.MakeMessage();
4635

47-
messageActivity.Text =
48-
$"* {string.Format(ConversationText.OptionsCommandHint, $"{Commands.CommandKeyword} {Commands.CommandListOptions}")}"
49-
+ $"\n\r* {string.Format(ConversationText.ConnectRequestCommandHint, Commands.CommandRequestConnection)}";
36+
messageActivity.Text =
37+
$"* {string.Format(ConversationText.OptionsCommandHint, $"{Commands.CommandKeyword} {Commands.CommandListOptions}")}"
38+
+ $"\n\r* {string.Format(ConversationText.ConnectRequestCommandHint, Commands.CommandRequestConnection)}";
5039

51-
await dialogContext.PostAsync(messageActivity);
52-
}
40+
await dialogContext.PostAsync(messageActivity);
5341
}
5442

5543
dialogContext.Wait(OnMessageReceivedAsync);

IntermediatorBotSample/IntermediatorBotSample.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
<Reference Include="Autofac, Version=4.6.2.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
4747
<HintPath>$(SolutionDir)\packages\Autofac.4.6.2\lib\net45\Autofac.dll</HintPath>
4848
</Reference>
49-
<Reference Include="BotMessageRouting, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
50-
<HintPath>$(SolutionDir)\packages\Underscore.Bot.MessageRouting.1.0.0\lib\net46\BotMessageRouting.dll</HintPath>
49+
<Reference Include="BotMessageRouting, Version=1.0.2.0, Culture=neutral, processorArchitecture=MSIL">
50+
<HintPath>$(SolutionDir)\packages\Underscore.Bot.MessageRouting.1.0.2\lib\net46\BotMessageRouting.dll</HintPath>
5151
</Reference>
5252
<Reference Include="Chronic, Version=0.3.2.0, Culture=neutral, PublicKeyToken=3bd1f1ef638b0d3c, processorArchitecture=MSIL">
5353
<HintPath>$(SolutionDir)\packages\Chronic.Signed.0.3.2\lib\net40\Chronic.dll</HintPath>

IntermediatorBotSample/MessageRouting/MessageRouterResultHandler.cs

Lines changed: 45 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace IntermediatorBotSample.MessageRouting
1313
{
14+
/// <summary>
15+
/// Handles results from operations executed by the message router mamanger.
16+
/// </summary>
1417
public class MessageRouterResultHandler
1518
{
1619
private MessageRouterManager _messageRouterManager;
@@ -84,15 +87,8 @@ protected virtual async Task HandleErrorAsync(MessageRouterResult messageRouterR
8487

8588
if (messageRouterResult.ConversationOwnerParty != null)
8689
{
87-
try
88-
{
89-
await _messageRouterManager.SendMessageToPartyByBotAsync(
90-
messageRouterResult.ConversationOwnerParty, errorMessage);
91-
}
92-
catch (UnauthorizedAccessException e)
93-
{
94-
System.Diagnostics.Debug.WriteLine($"Failed to send message: {e.Message}");
95-
}
90+
await _messageRouterManager.SendMessageToPartyByBotAsync(
91+
messageRouterResult.ConversationOwnerParty, errorMessage);
9692
}
9793
}
9894

@@ -161,63 +157,57 @@ protected virtual async Task HandleConnectionChangedResultAsync(MessageRouterRes
161157
Party conversationOwnerParty = messageRouterResult.ConversationOwnerParty;
162158
Party conversationClientParty = messageRouterResult.ConversationClientParty;
163159

164-
string conversationOwnerName = conversationOwnerParty?.ChannelAccount.Name;
165-
string conversationClientName = conversationClientParty?.ChannelAccount.Name;
160+
string conversationOwnerName =
161+
string.IsNullOrEmpty(conversationOwnerParty?.ChannelAccount.Name)
162+
? StringAndCharConstants.NoUserNamePlaceholder
163+
: conversationOwnerParty?.ChannelAccount.Name;
164+
165+
string conversationClientName =
166+
string.IsNullOrEmpty(conversationClientParty?.ChannelAccount.Name)
167+
? StringAndCharConstants.NoUserNamePlaceholder
168+
: conversationClientParty?.ChannelAccount.Name;
166169

167170
string messageToConversationOwner = string.Empty;
168171
string messageToConversationClient = string.Empty;
169172

170173
if (messageRouterResult.Type == MessageRouterResultType.ConnectionRequested)
171174
{
172-
if (conversationClientParty == null || conversationClientParty.ChannelAccount == null)
173-
{
174-
await _messageRouterManager.BroadcastMessageToAggregationChannelsAsync(
175-
ConversationText.ConnectionRequestMadeButRequestorIsNull);
176-
throw new NullReferenceException(ConversationText.ConnectionRequestMadeButRequestorIsNull);
177-
}
175+
bool conversationClientPartyMissing =
176+
(conversationClientParty == null || conversationClientParty.ChannelAccount == null);
178177

179178
foreach (Party aggregationParty in _messageRouterManager.RoutingDataManager.GetAggregationParties())
180179
{
181-
Party botParty = routingDataManager
182-
.FindBotPartyByChannelAndConversation(aggregationParty.ChannelId, aggregationParty.ConversationAccount);
180+
Party botParty = routingDataManager.FindBotPartyByChannelAndConversation(
181+
aggregationParty.ChannelId, aggregationParty.ConversationAccount);
183182

184183
if (botParty != null)
185184
{
186-
IMessageActivity messageActivity = Activity.CreateMessageActivity();
187-
messageActivity.Conversation = aggregationParty.ConversationAccount;
188-
messageActivity.Recipient = aggregationParty.ChannelAccount;
189-
messageActivity.Attachments = new List<Attachment>
185+
if (conversationClientPartyMissing)
190186
{
191-
CommandCardFactory.CreateRequestCard(
192-
conversationClientParty, botParty.ChannelAccount?.Name).ToAttachment()
193-
};
194-
195-
try
196-
{
197-
await _messageRouterManager.SendMessageToPartyByBotAsync(aggregationParty, messageActivity);
187+
await _messageRouterManager.SendMessageToPartyByBotAsync(
188+
aggregationParty, ConversationText.RequestorDetailsMissing);
198189
}
199-
catch (UnauthorizedAccessException e)
190+
else
200191
{
201-
System.Diagnostics.Debug.WriteLine($"Failed to broadcast message: {e.Message}");
202-
}
203-
}
204-
else
205-
{
206-
try
207-
{
208-
await _messageRouterManager.BroadcastMessageToAggregationChannelsAsync(
209-
string.Format(
210-
ConversationText.FailedToFindBotOnAggregationChannel,
211-
aggregationParty.ConversationAccount.Name));
212-
}
213-
catch (UnauthorizedAccessException e)
214-
{
215-
System.Diagnostics.Debug.WriteLine($"Failed to send message: {e.Message}");
192+
IMessageActivity messageActivity = Activity.CreateMessageActivity();
193+
messageActivity.Conversation = aggregationParty.ConversationAccount;
194+
messageActivity.Recipient = aggregationParty.ChannelAccount;
195+
messageActivity.Attachments = new List<Attachment>
196+
{
197+
CommandCardFactory.CreateRequestCard(
198+
conversationClientParty, botParty.ChannelAccount?.Name).ToAttachment()
199+
};
200+
201+
await _messageRouterManager.SendMessageToPartyByBotAsync(
202+
aggregationParty, messageActivity);
216203
}
217204
}
218205
}
219206

220-
messageToConversationClient = ConversationText.NotifyClientWaitForRequestHandling;
207+
if (!conversationClientPartyMissing)
208+
{
209+
messageToConversationClient = ConversationText.NotifyClientWaitForRequestHandling;
210+
}
221211
}
222212
else if (messageRouterResult.Type == MessageRouterResultType.ConnectionAlreadyRequested)
223213
{
@@ -239,30 +229,18 @@ await _messageRouterManager.BroadcastMessageToAggregationChannelsAsync(
239229
messageToConversationClient = string.Format(ConversationText.NotifyClientDisconnected, conversationOwnerName);
240230
}
241231

242-
if (!string.IsNullOrEmpty(messageToConversationOwner) && conversationOwnerParty != null)
232+
if (conversationOwnerParty != null
233+
&& !string.IsNullOrEmpty(messageToConversationOwner))
243234
{
244-
try
245-
{
246-
await _messageRouterManager.SendMessageToPartyByBotAsync(
247-
conversationOwnerParty, messageToConversationOwner);
248-
}
249-
catch (UnauthorizedAccessException e)
250-
{
251-
System.Diagnostics.Debug.WriteLine($"Failed to send message: {e.Message}");
252-
}
235+
await _messageRouterManager.SendMessageToPartyByBotAsync(
236+
conversationOwnerParty, messageToConversationOwner);
253237
}
254238

255-
if (!string.IsNullOrEmpty(messageToConversationClient) && conversationClientParty != null)
239+
if (conversationClientParty != null
240+
&& !string.IsNullOrEmpty(messageToConversationClient))
256241
{
257-
try
258-
{
259-
await _messageRouterManager.SendMessageToPartyByBotAsync(
260-
conversationClientParty, messageToConversationClient);
261-
}
262-
catch (UnauthorizedAccessException e)
263-
{
264-
System.Diagnostics.Debug.WriteLine($"Failed to send message: {e.Message}");
265-
}
242+
await _messageRouterManager.SendMessageToPartyByBotAsync(
243+
conversationClientParty, messageToConversationClient);
266244
}
267245
}
268246
}

IntermediatorBotSample/MessageRouting/MessageRoutingUtils.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,34 @@ namespace IntermediatorBotSample.MessageRouting
1414
/// </summary>
1515
public class MessageRoutingUtils
1616
{
17-
private const string EmulatorChannelId = "emulator";
18-
private const string SkypeChannelId = "skype";
17+
private const string ChannelIdEmulator = "emulator";
18+
private const string ChannelIdFacebook = "facebook";
19+
private const string ChannelIdSkype = "skype";
20+
21+
/// <summary>
22+
/// Do not try to create direct conversations when the owner is on one of these channels
23+
/// </summary>
24+
private readonly IList<string> NoDirectConversationsWithChannels = new List<string>()
25+
{
26+
ChannelIdEmulator,
27+
ChannelIdFacebook,
28+
ChannelIdSkype
29+
};
30+
31+
/// <summary>
32+
/// Broadcasts the given message to all aggregation channels.
33+
/// </summary>
34+
/// <param name="messageRouterManager">The message router manager instance.</param>
35+
/// <param name="messageText">The message to broadcast.</param>
36+
public static async Task BroadcastMessageToAggregationChannelsAsync(
37+
MessageRouterManager messageRouterManager, string messageText)
38+
{
39+
foreach (Party aggregationChannel in
40+
messageRouterManager.RoutingDataManager.GetAggregationParties())
41+
{
42+
await messageRouterManager.SendMessageToPartyByBotAsync(aggregationChannel, messageText);
43+
}
44+
}
1945

2046
/// <summary>
2147
/// Tries to accept/reject a pending request.
@@ -85,18 +111,14 @@ public async Task<string> AcceptOrRejectRequestAsync(
85111
}
86112
else
87113
{
88-
// Do not try to create direct conversation in emulator or Skype
89114
bool createNewDirectConversation =
90-
!(senderParty.ChannelId.Contains(SkypeChannelId)
91-
|| senderParty.ChannelId.Contains(EmulatorChannelId));
115+
!(NoDirectConversationsWithChannels.Contains(senderParty.ChannelId.ToLower()));
92116

93117
// Try to accept
94118
messageRouterResult = await messageRouterManager.ConnectAsync(
95119
senderParty,
96120
partyToAcceptOrReject,
97121
createNewDirectConversation);
98-
99-
await messageRouterResultHandler.HandleResultAsync(messageRouterResult);
100122
}
101123
}
102124
else

IntermediatorBotSample/Strings/ConversationText.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

IntermediatorBotSample/Strings/ConversationText.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,7 @@
294294
<data name="Users" xml:space="preserve">
295295
<value>Users</value>
296296
</data>
297+
<data name="RequestorDetailsMissing" xml:space="preserve">
298+
<value>Request was made, but the details of the requestor party are missing</value>
299+
</data>
297300
</root>

IntermediatorBotSample/Strings/StringAndCharConstants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace IntermediatorBot.Strings
44
{
55
public class StringAndCharConstants
66
{
7+
public static readonly string NoUserNamePlaceholder = "<no user name>";
8+
79
public static readonly string LineBreak = "\n\r";
810
public static readonly char QuotationMark = '"';
911

IntermediatorBotSample/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@
3535
<package id="System.Linq.Queryable" version="4.3.0" targetFramework="net46" />
3636
<package id="System.Net.Requests" version="4.3.0" targetFramework="net46" />
3737
<package id="System.Spatial" version="5.8.3" targetFramework="net46" />
38-
<package id="Underscore.Bot.MessageRouting" version="1.0.0" targetFramework="net46" />
38+
<package id="Underscore.Bot.MessageRouting" version="1.0.2" targetFramework="net46" />
3939
<package id="WindowsAzure.Storage" version="8.7.0" targetFramework="net46" />
4040
</packages>

0 commit comments

Comments
 (0)