Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added multilingial and azure blob upload components for bot composer #528

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Bot.Builder.Community.sln
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bot.Builder.Community.Adapt
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bot.Builder.Community.Adapters.Facebook.Tests", "tests\Bot.Builder.Community.Adapters.Facebook.Tests\Bot.Builder.Community.Adapters.Facebook.Tests.csproj", "{8201DC48-763A-4534-9E51-466E15DF01D8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bot.Builder.Community.Components.Middleware.Multilingual", "libraries\Bot.Builder.Community.Components.Middleware.Multilingual\Bot.Builder.Community.Components.Middleware.Multilingual.csproj", "{0541C224-70D5-4184-AF25-7E658298BFAF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bot.Builder.Community.Components.Azure.BlobUpload", "libraries\Bot.Builder.Community.Components.Azure.BlobUpload\Bot.Builder.Community.Components.Azure.BlobUpload.csproj", "{C57D2BF6-5589-4F20-97E9-F225AD361239}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug - NuGet Packages|Any CPU = Debug - NuGet Packages|Any CPU
Expand Down Expand Up @@ -805,6 +809,22 @@ Global
{8201DC48-763A-4534-9E51-466E15DF01D8}.Documentation|Any CPU.Build.0 = Debug|Any CPU
{8201DC48-763A-4534-9E51-466E15DF01D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8201DC48-763A-4534-9E51-466E15DF01D8}.Release|Any CPU.Build.0 = Release|Any CPU
{0541C224-70D5-4184-AF25-7E658298BFAF}.Debug - NuGet Packages|Any CPU.ActiveCfg = Debug|Any CPU
{0541C224-70D5-4184-AF25-7E658298BFAF}.Debug - NuGet Packages|Any CPU.Build.0 = Debug|Any CPU
{0541C224-70D5-4184-AF25-7E658298BFAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0541C224-70D5-4184-AF25-7E658298BFAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0541C224-70D5-4184-AF25-7E658298BFAF}.Documentation|Any CPU.ActiveCfg = Debug|Any CPU
{0541C224-70D5-4184-AF25-7E658298BFAF}.Documentation|Any CPU.Build.0 = Debug|Any CPU
{0541C224-70D5-4184-AF25-7E658298BFAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0541C224-70D5-4184-AF25-7E658298BFAF}.Release|Any CPU.Build.0 = Release|Any CPU
{C57D2BF6-5589-4F20-97E9-F225AD361239}.Debug - NuGet Packages|Any CPU.ActiveCfg = Debug|Any CPU
{C57D2BF6-5589-4F20-97E9-F225AD361239}.Debug - NuGet Packages|Any CPU.Build.0 = Debug|Any CPU
{C57D2BF6-5589-4F20-97E9-F225AD361239}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C57D2BF6-5589-4F20-97E9-F225AD361239}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C57D2BF6-5589-4F20-97E9-F225AD361239}.Documentation|Any CPU.ActiveCfg = Debug|Any CPU
{C57D2BF6-5589-4F20-97E9-F225AD361239}.Documentation|Any CPU.Build.0 = Debug|Any CPU
{C57D2BF6-5589-4F20-97E9-F225AD361239}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C57D2BF6-5589-4F20-97E9-F225AD361239}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -893,6 +913,8 @@ Global
{428AD1B4-DF58-4D21-9C19-AB4AB6001A90} = {840D4038-9AB8-4750-9FFE-365386CE47E2}
{3348B9A5-E3CE-4AF8-B059-8B4D7971C25A} = {840D4038-9AB8-4750-9FFE-365386CE47E2}
{8201DC48-763A-4534-9E51-466E15DF01D8} = {840D4038-9AB8-4750-9FFE-365386CE47E2}
{0541C224-70D5-4184-AF25-7E658298BFAF} = {1F7F4CAF-CF22-491F-840D-A63835726C12}
{C57D2BF6-5589-4F20-97E9-F225AD361239} = {1F7F4CAF-CF22-491F-840D-A63835726C12}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9FE3B75E-BA2B-45BC-BBF0-DDA8BA10C4F0}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System;
using AdaptiveExpressions.Properties;
using Azure.Storage.Blobs.Models;
using Bot.Builder.Community.Components.Azure.BlobUpload.Upload;
using Microsoft.Bot.Builder.Dialogs;
using Newtonsoft.Json;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;

namespace Bot.Builder.Community.Components.Azure.BlobUpload
{

public class AzureBlobUpload : Dialog
{
[JsonProperty("$Kind")]
public const string Kind = "AzureBlobUpload";

[JsonProperty("FileUrl")]
public StringExpression FileUrl { get; set; }

[JsonProperty("FileName")]
public StringExpression FileName { get; set; }

[JsonProperty("PublicAccessType")]
public EnumExpression<PublicAccessType> PublicAccessType { get; set; }

[JsonProperty("DeleteSnapshotsOption")]
public EnumExpression<DeleteSnapshotsOption> DeleteSnapshotsOption { get; set; }

[JsonProperty("Containers")]
public StringExpression Containers { get; set; }

[JsonProperty("ConnectionString")]
public StringExpression ConnectionString { get; set; }

[JsonProperty("resultProperty")]
public StringExpression ResultProperty { get; set; }

IAzureUpload _dataUpload;

public AzureBlobUpload([CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0) : base()
{
RegisterSourceLocation(sourceFilePath, sourceLineNumber);

}

public override async Task<DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null,
CancellationToken cancellationToken = new CancellationToken())
{

IsPropertyIsValid(dc);

CreateDataStorage(dc);

var getUrl = FileUrl?.GetValue(dc.State);

var fileName = FileName?.GetValue(dc.State);

var result = await _dataUpload.UploadAsync(getUrl, fileName);

if (ResultProperty != null)
{
dc.State.SetValue(this.ResultProperty.GetValue(dc.State), result);
}

return await dc.EndDialogAsync(result: result, cancellationToken: cancellationToken);
}



private void CreateDataStorage(DialogContext dc)
{
if (_dataUpload == null)
return;

var containerName = Containers?.GetValue(dc.State);

var accessType = PublicAccessType?.GetValue(dc.State);

var deleteSnapshotsOption = DeleteSnapshotsOption?.GetValue(dc.State);

Enum.TryParse(accessType.ToString(), out PublicAccessType publicAccessType);

Enum.TryParse(deleteSnapshotsOption.ToString(), out DeleteSnapshotsOption deleteSnapshotsType);

var connectionString = ConnectionString?.GetValue(dc.State);

_dataUpload = new AzureUpload(connectionString, containerName, publicAccessType, deleteSnapshotsType);

}


private void IsPropertyIsValid(DialogContext dc)
{

var getUrl = FileUrl?.GetValue(dc.State);


if (string.IsNullOrEmpty(getUrl))
{
throw new Exception($"{nameof(AzureBlobUpload)} : FileUrl is required");
}

var fileName = FileName?.GetValue(dc.State);

if (string.IsNullOrEmpty(fileName))
{
throw new Exception($"{nameof(AzureBlobUpload)} : FileName is required");
}


var container = Containers?.GetValue(dc.State);

if (string.IsNullOrEmpty(container))
{
throw new Exception($"{nameof(AzureBlobUpload)} : Container name is required");
}

var connectionString = ConnectionString?.GetValue(dc.State);

if (string.IsNullOrEmpty(connectionString))
{
throw new Exception($"{nameof(AzureBlobUpload)} : connection string is required");
}

}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs.Declarative;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Bot.Builder.Community.Components.Azure.BlobUpload
{
public class AzureDataStorageBotComponent : BotComponent
{
public override void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
services.AddSingleton<DeclarativeType>(sp =>
new DeclarativeType<AzureBlobUpload>(AzureBlobUpload.Kind));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove('$(MSBuildThisFileDirectory)../', 'Bot.Builder.Community.sln'))\CommonTargets\library.shared.targets" />


<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Description>Upload file to Azure Blob Storage component for Bot Framework Composer</Description>
<Company>Bot Builder Community</Company>
<PackageLicenseUrl>https://github.com/BotBuilderCommunity/botbuilder-community-dotnet/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/BotBuilderCommunity/botbuilder-community-dotnet/tree/master/libraries/Bot.Builder.Community.Components.Dialogs.Input</PackageProjectUrl>
<Version>0.0.5</Version>
<FileVersion>1.0.0</FileVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageIcon>package-icon.png</PackageIcon>
<RepositoryUrl>https://github.com/BotBuilderCommunity/botbuilder-community-dotnet/</RepositoryUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<ContentTargetFolders>content</ContentTargetFolders>
<PackageTags>composer;botframework;botbuilder;msbot-component;msbot-action</PackageTags>
<NoWarn>$(NoWarn);NU5104</NoWarn>
</PropertyGroup>


<ItemGroup>
<Content Include="**/*.schema" />
<Content Include="**/*.uischema" />
</ItemGroup>


<ItemGroup>
<None Remove="schema\AzureBlobUpload.uischema" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.14.1" />
<PackageReference Include="Microsoft.Bot.Builder.Dialogs.Adaptive" Version="$(Bot_Builder_Version)" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\package-icon.png" Link="package-icon.png">
<PackagePath></PackagePath>
<Pack>True</Pack>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.IO;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs;
using Azure;
using System.Net;
using System.Threading.Tasks;

namespace Bot.Builder.Community.Components.Azure.BlobUpload.Upload
{
internal class AzureUpload : IAzureUpload
{
readonly BlobServiceClient _blobServiceClient;
BlobContainerClient _blobContainerClient;
private readonly PublicAccessType _publicAccessType;
private readonly DeleteSnapshotsOption _deleteSnapshotsOption;

public AzureUpload(string connectionString, string containerName,
PublicAccessType publicAccessType = PublicAccessType.None, DeleteSnapshotsOption deleteSnapshotsOption = DeleteSnapshotsOption.None)
{
_blobServiceClient = new BlobServiceClient(connectionString);
_blobContainerClient = _blobServiceClient.GetBlobContainerClient(containerName);
this._publicAccessType = publicAccessType;
this._deleteSnapshotsOption = deleteSnapshotsOption;

_blobServiceClient = new BlobServiceClient(connectionString);

CreateContainer(containerName);

if (_blobContainerClient == null)
throw new Exception("Create Container is failed");
}
private async void CreateContainer(string containerName)
{
try
{
_blobContainerClient = _blobServiceClient.GetBlobContainerClient(containerName);

if (_blobContainerClient != null)
{
bool isExits = await _blobContainerClient.ExistsAsync();

if (isExits)
return;

await _blobContainerClient.CreateIfNotExistsAsync(publicAccessType: _publicAccessType, cancellationToken: default);
}

}
catch (RequestFailedException)
{

}
}

public async Task<string> UploadAsync(string fileUrl, string fileName)
{
var wc = new WebClient();
var stream = new MemoryStream(wc.DownloadData(fileUrl));

var blob = _blobContainerClient.GetBlobClient(fileName);

if (_deleteSnapshotsOption == DeleteSnapshotsOption.None)
return blob.Uri.AbsoluteUri;

await blob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots);

var result = await blob.UploadAsync(stream);

return blob.Uri.AbsoluteUri;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;

namespace Bot.Builder.Community.Components.Azure.BlobUpload.Upload
{
public interface IAzureUpload
{
Task<string> UploadAsync(string fileUrl, string fileName);
}
}
Loading