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

Remove AdaptiveCard dependency from Teams packages #2532

Merged
merged 1 commit into from
Sep 17, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AdaptiveCards" Version="1.0.0" />
<PackageReference Include="Microsoft.Bot.Builder" Condition=" '$(PackageVersion)' == '' " Version="4.0.0-local" />
<PackageReference Include="Microsoft.Bot.Builder" Condition=" '$(PackageVersion)' != '' " Version="$(PackageVersion)" />

Expand Down
124 changes: 0 additions & 124 deletions libraries/Microsoft.Bot.Schema.Teams/CardExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AdaptiveCards" Version="1.0.0" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.12" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
<PackageReference Include="Microsoft.Bot.Schema" Condition=" '$(PackageVersion)' == '' " Version="4.0.0-local" />
Expand Down
37 changes: 33 additions & 4 deletions tests/Teams/CardActions/Bots/CardActionsBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivi
var action3 = new CardAction(ActionTypes.MessageBack, "message back local echo", null, "text received by bots", "display text message back", JObject.Parse(@"{ ""key"" : ""value"" }"));
var action4 = new CardAction("invoke", "invoke", null, null, null, JObject.Parse(@"{ ""key"" : ""value"" }"));

//adaptiveCard.Actions.Add(action1.ToAdaptiveCardAction());
//adaptiveCard.Actions.Add(action2.ToAdaptiveCardAction());
//adaptiveCard.Actions.Add(action3.ToAdaptiveCardAction());
//adaptiveCard.Actions.Add(action4.ToAdaptiveCardAction());
adaptiveCard.Actions.Add(action1.ToAdaptiveCardAction());
adaptiveCard.Actions.Add(action2.ToAdaptiveCardAction());
adaptiveCard.Actions.Add(action3.ToAdaptiveCardAction());
adaptiveCard.Actions.Add(action4.ToAdaptiveCardAction());

var replyActivity = MessageFactory.Attachment(adaptiveCard.ToAttachment());
await turnContext.SendActivityAsync(replyActivity, cancellationToken);
Expand Down Expand Up @@ -78,5 +78,34 @@ protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivi
}
}
}

protected override async Task<TaskModuleResponse> OnTeamsTaskModuleFetchAsync(ITurnContext<IInvokeActivity> turnContext, CancellationToken cancellationToken)
{
var reply = MessageFactory.Text("OnTeamsTaskModuleFetchAsync Value: " + turnContext.Activity.Value.ToString());
await turnContext.SendActivityAsync(reply, cancellationToken);

var adaptiveCard = new AdaptiveCard();
adaptiveCard.Body.Add(new AdaptiveTextBlock("This is an Adaptive Card within a Task Module"));

return new TaskModuleResponse
{
Task = new TaskModuleContinueResponse()
{
Value = new TaskModuleTaskInfo()
{
Card = adaptiveCard.ToAttachment(),
Height = 200,
Width = 400,
Title = "Task Module Example",
},
},
};
}

protected override async Task<InvokeResponse> OnTeamsCardActionInvokeAsync(ITurnContext<IInvokeActivity> turnContext, CancellationToken cancellationToken)
{
await turnContext.SendActivityAsync(MessageFactory.Text($"OnTeamsCardActionInvokeAsync value: {turnContext.Activity.Value}"), cancellationToken);
return new InvokeResponse() { Status = 200 };
}
}
}
1 change: 1 addition & 0 deletions tests/Teams/CardActions/CardActions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AdaptiveCards" Version="1.0.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>
Expand Down
61 changes: 61 additions & 0 deletions tests/Teams/CardActions/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Microsoft.Bot.Schema;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Microsoft.BotBuilderSamples
{
public static class Extensions
{
/// <summary>
/// Creates a new attachment from AdaptiveCard.
/// </summary>
/// <param name="card"> The instance of AdaptiveCard.</param>
/// <returns> The generated attachment.</returns>
public static Attachment ToAttachment(this AdaptiveCards.AdaptiveCard card)
{
return new Attachment
{
Content = card,
ContentType = AdaptiveCards.AdaptiveCard.ContentType,
};
}

/// <summary>
/// Wrap BotBuilder action into AdaptiveCard submit action.
/// </summary>
/// <param name="action"> The instance of adaptive card submit action.</param>
/// <param name="targetAction"> Target action to be adapted.</param>
public static void RepresentAsBotBuilderAction(this AdaptiveCards.AdaptiveSubmitAction action, CardAction targetAction)
{
var wrappedAction = new CardAction
{
Type = targetAction.Type,
Value = targetAction.Value,
Text = targetAction.Text,
DisplayText = targetAction.DisplayText,
};

JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
serializerSettings.NullValueHandling = NullValueHandling.Ignore;

string jsonStr = action.DataJson == null ? "{}" : action.DataJson;
JToken dataJson = JObject.Parse(jsonStr);
dataJson["msteams"] = JObject.FromObject(wrappedAction, JsonSerializer.Create(serializerSettings));

action.Title = targetAction.Title;
action.DataJson = dataJson.ToString();
}

/// <summary>
/// Wrap BotBuilder action into AdaptiveCard submit action.
/// </summary>
/// <param name="action"> Target bot builder aciton to be adapted.</param>
/// <returns> The wrapped adaptive card submit action.</returns>
public static AdaptiveCards.AdaptiveSubmitAction ToAdaptiveCardAction(this CardAction action)
{
var adaptiveCardAction = new AdaptiveCards.AdaptiveSubmitAction();
adaptiveCardAction.RepresentAsBotBuilderAction(action);
return adaptiveCardAction;
}
}
}
9 changes: 8 additions & 1 deletion tests/Teams/CardsOffice365/Bots/CardsBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ public class CardsBot : TeamsActivityHandler
{
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
await turnContext.SendActivityAsync(MessageFactory.Attachment(Cards.CreateSampleO365ConnectorCard().ToAttachment()));
var card = Cards.CreateSampleO365ConnectorCard();
var cardAttachment = new Attachment
{
Content = card,
ContentType = O365ConnectorCard.ContentType,
};

await turnContext.SendActivityAsync(MessageFactory.Attachment(cardAttachment));
}

protected override async Task OnTeamsO365ConnectorCardActionAsync(ITurnContext<IInvokeActivity> turnContext, O365ConnectorCardActionQuery query, CancellationToken cancellationToken)
Expand Down
23 changes: 16 additions & 7 deletions tests/Teams/FileUpload/Bots/TeamsFileBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,16 @@ private async Task SendFileCardAsync(ITurnContext turnContext, string filename,
DeclineContext = consentContext,
};

var replyActivity = turnContext.Activity.CreateReply();
replyActivity.Attachments = new List<Attachment>()
var asAttachment = new Attachment
{
fileCard.ToAttachment(filename),
Content = fileCard,
ContentType = FileConsentCard.ContentType,
Name = filename,
};

var replyActivity = turnContext.Activity.CreateReply();
replyActivity.Attachments = new List<Attachment>() { asAttachment };

await turnContext.SendActivityAsync(replyActivity, cancellationToken);
}

Expand Down Expand Up @@ -123,13 +127,18 @@ private async Task FileUploadCompletedAsync(ITurnContext turnContext, FileConsen
FileType = fileConsentCardResponse.UploadInfo.FileType,
};

var asAttachment = new Attachment
{
Content = downloadCard,
ContentType = FileInfoCard.ContentType,
Name = fileConsentCardResponse.UploadInfo.Name,
ContentUrl = fileConsentCardResponse.UploadInfo.ContentUrl,
};

var reply = turnContext.Activity.CreateReply();
reply.TextFormat = "xml";
reply.Text = $"<b>File uploaded.</b> Your file <b>{fileConsentCardResponse.UploadInfo.Name}</b> is ready to download";
reply.Attachments = new List<Attachment>
{
downloadCard.ToAttachment(fileConsentCardResponse.UploadInfo.Name, fileConsentCardResponse.UploadInfo.ContentUrl),
};
reply.Attachments = new List<Attachment> { asAttachment };

await turnContext.SendActivityAsync(reply, cancellationToken);
}
Expand Down
18 changes: 12 additions & 6 deletions tests/Teams/TaskModule/Bots/TaskModuleBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class TaskModuleBot : TeamsActivityHandler
{
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
var reply = MessageFactory.Attachment(this.GetHeroCard());
var reply = MessageFactory.Attachment(this.GetTaskModuleHeroCard());
await turnContext.SendActivityAsync(reply);
}

Expand All @@ -31,7 +31,7 @@ protected override async Task<TaskModuleResponse> OnTeamsTaskModuleFetchAsync(IT
{
Value = new TaskModuleTaskInfo()
{
Card = this.GetAdaptiveCard(),
Card = this.GetTaskModuleAdaptiveCard(),
Height = 200,
Width = 400,
Title = "Adaptive Card: Inputs",
Expand All @@ -55,7 +55,7 @@ protected override async Task<TaskModuleResponse> OnTeamsTaskModuleSubmitAsync(I
};
}

private Attachment GetHeroCard()
private Attachment GetTaskModuleHeroCard()
{
return new HeroCard()
{
Expand All @@ -68,9 +68,9 @@ private Attachment GetHeroCard()
}.ToAttachment();
}

private Attachment GetAdaptiveCard()
private Attachment GetTaskModuleAdaptiveCard()
{
return new AdaptiveCard(new AdaptiveSchemaVersion("1.0"))
var card = new AdaptiveCard(new AdaptiveSchemaVersion("1.0"))
{
Body = new List<AdaptiveElement>()
{
Expand All @@ -87,7 +87,13 @@ private Attachment GetAdaptiveCard()
{
new AdaptiveSubmitAction() { Title = "Submit" },
},
}.ToAttachment();
};

return new Attachment
{
Content = card,
ContentType = AdaptiveCards.AdaptiveCard.ContentType,
};
}
}
}
1 change: 1 addition & 0 deletions tests/Teams/TaskModule/TaskModule.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AdaptiveCards" Version="1.0.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

Expand Down