Skip to content

Commit

Permalink
Merge pull request #29 from tompaana/development_nuget
Browse files Browse the repository at this point in the history
Official version 1.0
  • Loading branch information
tompaana authored Jan 25, 2018
2 parents 8a41c97 + fd6c275 commit 49c7f08
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 102 deletions.
4 changes: 2 additions & 2 deletions IntermediatorBotSample/CommandHandling/CommandCardFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static HeroCard CreateRequestCard(Party requestorParty, string botName =
}

string requestorChannelAccountName = string.IsNullOrEmpty(requestorParty.ChannelAccount.Name)
? CommandMessageHandler.NoUserNamePlaceholder : requestorParty.ChannelAccount.Name;
? StringAndCharConstants.NoUserNamePlaceholder : requestorParty.ChannelAccount.Name;

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

string requestorChannelAccountName = string.IsNullOrEmpty(requestorParty.ChannelAccount.Name)
? CommandMessageHandler.NoUserNamePlaceholder : requestorParty.ChannelAccount.Name;
? StringAndCharConstants.NoUserNamePlaceholder : requestorParty.ChannelAccount.Name;

string requestorChannelId = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(requestorParty.ChannelId);
string requestorChannelAccountId = requestorParty.ChannelAccount.Id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ namespace IntermediatorBotSample.CommandHandling
/// </summary>
public class CommandMessageHandler
{
public static readonly string NoUserNamePlaceholder = "<no user name>";

private MessageRouterManager _messageRouterManager;
private MessageRouterResultHandler _messageRouterResultHandler;

Expand Down Expand Up @@ -176,8 +174,6 @@ public async virtual Task<bool> HandleCommandAsync(Activity activity)
#if DEBUG
case string baseCommand when (baseCommand.Equals(Commands.CommandDeleteAllRoutingData)):
// DELETE ALL ROUTING DATA
await _messageRouterManager.BroadcastMessageToAggregationChannelsAsync(
string.Format(ConversationText.DeletingAllDataWithCommandIssuer, senderParty.ChannelAccount?.Name));
replyActivity = activity.CreateReply(ConversationText.DeletingAllData);
_messageRouterManager.RoutingDataManager.DeleteAll();
wasHandled = true;
Expand Down
3 changes: 3 additions & 0 deletions IntermediatorBotSample/Controllers/MessagesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
}
}

// Uncomment to see the result in a reply (may be useful for debugging)
//await MessagingUtils.ReplyToActivityAsync(activity, messageRouterResult.ToString());

// Handle the result, if required
await messageRouterResultHandler.HandleResultAsync(messageRouterResult);
}
Expand Down
26 changes: 7 additions & 19 deletions IntermediatorBotSample/Dialogs/RootDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
using Microsoft.Bot.Connector;
using System;
using System.Threading.Tasks;
using Underscore.Bot.MessageRouting;

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

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

if (!string.IsNullOrEmpty(messageText))
{
if (messageText.ToLower().Contains(Commands.CommandRequestConnection))
{
MessageRouterResult messageRouterResult =
WebApiConfig.MessageRouterManager.RequestConnection(
(messageActivity as Activity), WebApiConfig.Settings.RejectConnectionRequestIfNoAggregationChannel);
await WebApiConfig.MessageRouterResultHandler.HandleResultAsync(messageRouterResult);
}
else
{
messageActivity = dialogContext.MakeMessage();
messageActivity = dialogContext.MakeMessage();

messageActivity.Text =
$"* {string.Format(ConversationText.OptionsCommandHint, $"{Commands.CommandKeyword} {Commands.CommandListOptions}")}"
+ $"\n\r* {string.Format(ConversationText.ConnectRequestCommandHint, Commands.CommandRequestConnection)}";
messageActivity.Text =
$"* {string.Format(ConversationText.OptionsCommandHint, $"{Commands.CommandKeyword} {Commands.CommandListOptions}")}"
+ $"\n\r* {string.Format(ConversationText.ConnectRequestCommandHint, Commands.CommandRequestConnection)}";

await dialogContext.PostAsync(messageActivity);
}
await dialogContext.PostAsync(messageActivity);
}

dialogContext.Wait(OnMessageReceivedAsync);
Expand Down
4 changes: 2 additions & 2 deletions IntermediatorBotSample/IntermediatorBotSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
<Reference Include="Autofac, Version=4.6.2.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>$(SolutionDir)\packages\Autofac.4.6.2\lib\net45\Autofac.dll</HintPath>
</Reference>
<Reference Include="BotMessageRouting, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>$(SolutionDir)\packages\Underscore.Bot.MessageRouting.1.0.0\lib\net46\BotMessageRouting.dll</HintPath>
<Reference Include="BotMessageRouting, Version=1.0.2.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>$(SolutionDir)\packages\Underscore.Bot.MessageRouting.1.0.2\lib\net46\BotMessageRouting.dll</HintPath>
</Reference>
<Reference Include="Chronic, Version=0.3.2.0, Culture=neutral, PublicKeyToken=3bd1f1ef638b0d3c, processorArchitecture=MSIL">
<HintPath>$(SolutionDir)\packages\Chronic.Signed.0.3.2\lib\net40\Chronic.dll</HintPath>
Expand Down
112 changes: 45 additions & 67 deletions IntermediatorBotSample/MessageRouting/MessageRouterResultHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace IntermediatorBotSample.MessageRouting
{
/// <summary>
/// Handles results from operations executed by the message router mamanger.
/// </summary>
public class MessageRouterResultHandler
{
private MessageRouterManager _messageRouterManager;
Expand Down Expand Up @@ -84,15 +87,8 @@ protected virtual async Task HandleErrorAsync(MessageRouterResult messageRouterR

if (messageRouterResult.ConversationOwnerParty != null)
{
try
{
await _messageRouterManager.SendMessageToPartyByBotAsync(
messageRouterResult.ConversationOwnerParty, errorMessage);
}
catch (UnauthorizedAccessException e)
{
System.Diagnostics.Debug.WriteLine($"Failed to send message: {e.Message}");
}
await _messageRouterManager.SendMessageToPartyByBotAsync(
messageRouterResult.ConversationOwnerParty, errorMessage);
}
}

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

string conversationOwnerName = conversationOwnerParty?.ChannelAccount.Name;
string conversationClientName = conversationClientParty?.ChannelAccount.Name;
string conversationOwnerName =
string.IsNullOrEmpty(conversationOwnerParty?.ChannelAccount.Name)
? StringAndCharConstants.NoUserNamePlaceholder
: conversationOwnerParty?.ChannelAccount.Name;

string conversationClientName =
string.IsNullOrEmpty(conversationClientParty?.ChannelAccount.Name)
? StringAndCharConstants.NoUserNamePlaceholder
: conversationClientParty?.ChannelAccount.Name;

string messageToConversationOwner = string.Empty;
string messageToConversationClient = string.Empty;

if (messageRouterResult.Type == MessageRouterResultType.ConnectionRequested)
{
if (conversationClientParty == null || conversationClientParty.ChannelAccount == null)
{
await _messageRouterManager.BroadcastMessageToAggregationChannelsAsync(
ConversationText.ConnectionRequestMadeButRequestorIsNull);
throw new NullReferenceException(ConversationText.ConnectionRequestMadeButRequestorIsNull);
}
bool conversationClientPartyMissing =
(conversationClientParty == null || conversationClientParty.ChannelAccount == null);

foreach (Party aggregationParty in _messageRouterManager.RoutingDataManager.GetAggregationParties())
{
Party botParty = routingDataManager
.FindBotPartyByChannelAndConversation(aggregationParty.ChannelId, aggregationParty.ConversationAccount);
Party botParty = routingDataManager.FindBotPartyByChannelAndConversation(
aggregationParty.ChannelId, aggregationParty.ConversationAccount);

if (botParty != null)
{
IMessageActivity messageActivity = Activity.CreateMessageActivity();
messageActivity.Conversation = aggregationParty.ConversationAccount;
messageActivity.Recipient = aggregationParty.ChannelAccount;
messageActivity.Attachments = new List<Attachment>
if (conversationClientPartyMissing)
{
CommandCardFactory.CreateRequestCard(
conversationClientParty, botParty.ChannelAccount?.Name).ToAttachment()
};

try
{
await _messageRouterManager.SendMessageToPartyByBotAsync(aggregationParty, messageActivity);
await _messageRouterManager.SendMessageToPartyByBotAsync(
aggregationParty, ConversationText.RequestorDetailsMissing);
}
catch (UnauthorizedAccessException e)
else
{
System.Diagnostics.Debug.WriteLine($"Failed to broadcast message: {e.Message}");
}
}
else
{
try
{
await _messageRouterManager.BroadcastMessageToAggregationChannelsAsync(
string.Format(
ConversationText.FailedToFindBotOnAggregationChannel,
aggregationParty.ConversationAccount.Name));
}
catch (UnauthorizedAccessException e)
{
System.Diagnostics.Debug.WriteLine($"Failed to send message: {e.Message}");
IMessageActivity messageActivity = Activity.CreateMessageActivity();
messageActivity.Conversation = aggregationParty.ConversationAccount;
messageActivity.Recipient = aggregationParty.ChannelAccount;
messageActivity.Attachments = new List<Attachment>
{
CommandCardFactory.CreateRequestCard(
conversationClientParty, botParty.ChannelAccount?.Name).ToAttachment()
};

await _messageRouterManager.SendMessageToPartyByBotAsync(
aggregationParty, messageActivity);
}
}
}

messageToConversationClient = ConversationText.NotifyClientWaitForRequestHandling;
if (!conversationClientPartyMissing)
{
messageToConversationClient = ConversationText.NotifyClientWaitForRequestHandling;
}
}
else if (messageRouterResult.Type == MessageRouterResultType.ConnectionAlreadyRequested)
{
Expand All @@ -239,30 +229,18 @@ await _messageRouterManager.BroadcastMessageToAggregationChannelsAsync(
messageToConversationClient = string.Format(ConversationText.NotifyClientDisconnected, conversationOwnerName);
}

if (!string.IsNullOrEmpty(messageToConversationOwner) && conversationOwnerParty != null)
if (conversationOwnerParty != null
&& !string.IsNullOrEmpty(messageToConversationOwner))
{
try
{
await _messageRouterManager.SendMessageToPartyByBotAsync(
conversationOwnerParty, messageToConversationOwner);
}
catch (UnauthorizedAccessException e)
{
System.Diagnostics.Debug.WriteLine($"Failed to send message: {e.Message}");
}
await _messageRouterManager.SendMessageToPartyByBotAsync(
conversationOwnerParty, messageToConversationOwner);
}

if (!string.IsNullOrEmpty(messageToConversationClient) && conversationClientParty != null)
if (conversationClientParty != null
&& !string.IsNullOrEmpty(messageToConversationClient))
{
try
{
await _messageRouterManager.SendMessageToPartyByBotAsync(
conversationClientParty, messageToConversationClient);
}
catch (UnauthorizedAccessException e)
{
System.Diagnostics.Debug.WriteLine($"Failed to send message: {e.Message}");
}
await _messageRouterManager.SendMessageToPartyByBotAsync(
conversationClientParty, messageToConversationClient);
}
}
}
Expand Down
36 changes: 29 additions & 7 deletions IntermediatorBotSample/MessageRouting/MessageRoutingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,34 @@ namespace IntermediatorBotSample.MessageRouting
/// </summary>
public class MessageRoutingUtils
{
private const string EmulatorChannelId = "emulator";
private const string SkypeChannelId = "skype";
private const string ChannelIdEmulator = "emulator";
private const string ChannelIdFacebook = "facebook";
private const string ChannelIdSkype = "skype";

/// <summary>
/// Do not try to create direct conversations when the owner is on one of these channels
/// </summary>
private readonly IList<string> NoDirectConversationsWithChannels = new List<string>()
{
ChannelIdEmulator,
ChannelIdFacebook,
ChannelIdSkype
};

/// <summary>
/// Broadcasts the given message to all aggregation channels.
/// </summary>
/// <param name="messageRouterManager">The message router manager instance.</param>
/// <param name="messageText">The message to broadcast.</param>
public static async Task BroadcastMessageToAggregationChannelsAsync(
MessageRouterManager messageRouterManager, string messageText)
{
foreach (Party aggregationChannel in
messageRouterManager.RoutingDataManager.GetAggregationParties())
{
await messageRouterManager.SendMessageToPartyByBotAsync(aggregationChannel, messageText);
}
}

/// <summary>
/// Tries to accept/reject a pending request.
Expand Down Expand Up @@ -85,18 +111,14 @@ public async Task<string> AcceptOrRejectRequestAsync(
}
else
{
// Do not try to create direct conversation in emulator or Skype
bool createNewDirectConversation =
!(senderParty.ChannelId.Contains(SkypeChannelId)
|| senderParty.ChannelId.Contains(EmulatorChannelId));
!(NoDirectConversationsWithChannels.Contains(senderParty.ChannelId.ToLower()));

// Try to accept
messageRouterResult = await messageRouterManager.ConnectAsync(
senderParty,
partyToAcceptOrReject,
createNewDirectConversation);

await messageRouterResultHandler.HandleResultAsync(messageRouterResult);
}
}
else
Expand Down
9 changes: 9 additions & 0 deletions IntermediatorBotSample/Strings/ConversationText.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions IntermediatorBotSample/Strings/ConversationText.resx
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,7 @@
<data name="Users" xml:space="preserve">
<value>Users</value>
</data>
<data name="RequestorDetailsMissing" xml:space="preserve">
<value>Request was made, but the details of the requestor party are missing</value>
</data>
</root>
2 changes: 2 additions & 0 deletions IntermediatorBotSample/Strings/StringAndCharConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace IntermediatorBot.Strings
{
public class StringAndCharConstants
{
public static readonly string NoUserNamePlaceholder = "<no user name>";

public static readonly string LineBreak = "\n\r";
public static readonly char QuotationMark = '"';

Expand Down
2 changes: 1 addition & 1 deletion IntermediatorBotSample/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
<package id="System.Linq.Queryable" version="4.3.0" targetFramework="net46" />
<package id="System.Net.Requests" version="4.3.0" targetFramework="net46" />
<package id="System.Spatial" version="5.8.3" targetFramework="net46" />
<package id="Underscore.Bot.MessageRouting" version="1.0.0" targetFramework="net46" />
<package id="Underscore.Bot.MessageRouting" version="1.0.2" targetFramework="net46" />
<package id="WindowsAzure.Storage" version="8.7.0" targetFramework="net46" />
</packages>

0 comments on commit 49c7f08

Please sign in to comment.