Comments (10)
aplDocumentJson and dataDocumentJson would be objects - most likely JObject objects. That could be parsed from a string, loaded from a stream or read from a blob in cloud storage
from alexa.net.apl.
Have you tried directive.Properties.Add("dataSources",sources);
to see if it's the JObject conversion?
from alexa.net.apl.
I tried to make the names descriptive in the example - aplDocumentJson
is the JSON for the APL document, so if you crafted something using the APL online designer, you can send it without having to map it to the APL model (unless you want to be able to alter it, for example)
As you're crafting a directive using JSON - if you wanted both the document and the dataSources, you'd add the document
property as per the example as well as the dataSources
property with your data source JSON in it - there's an example of a JSON directive with data sources here
Hope that makes sense
from alexa.net.apl.
@stoiveyp What was confusing me was that the Add
takes an object
and didn't figure out it is building a JSON document.
Is it something like this?
var aplDocumentJson = " { ... }";
var dataDocumentJson = = "{ ... }";
var directive = new JsonDirective(RenderDocumentDirective.APLDirectiveType);
directive.Properties.Add("document",aplDocumentJson);
directive.Properties.Add("dataSources",dataDocumentJson);
You do make the names descriptive but I'm not yet into the context of the Alexa API. I only post an issue/question as a last resource and I'm very sorry for taking you time.
Thank you so much for developing all these libraries.
from alexa.net.apl.
I succeed in having the APL in a JSON file.
- Added the file to an "APL" subfolder.
- Added the following to the csproj:
<None Update="APL/*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
- Added a parameter
ExecutionContext executionContext
to the function, as explained here. - Added the following code:
var executionPath = Directory.GetParent(executionContext.FunctionDirectory).FullName;
var directive = new JsonDirective(RenderDocumentDirective.APLDirectiveType);
directive.Properties.Add("document", JObject.Parse(await File.ReadAllTextAsync(Path.Combine(executionPath,"APL", "launch.json"))));
directive.Properties.Add("dataSources", JObject.Parse("..."));
from alexa.net.apl.
Okay. Are we okay to close now then?
from alexa.net.apl.
I'm reopening because I cannot make the "dataSources" property appear in the directive. I already tried many variants.
For my latest attempt, I developed this method that creates the APL directive given an JObject
with the template and the data sources, as an array of pairs containing a string
and an ObjectDataSource
:
static JsonDirective CreateAplDirective(JObject apl, params (string Key, ObjectDataSource Value)[] dataSources)
{
var directive = new JsonDirective(RenderDocumentDirective.APLDirectiveType);
directive.Properties.Add("document", apl);
var sources = new Dictionary<string, ObjectDataSource>();
foreach (var (key, dataSource) in dataSources)
sources.Add(key, dataSource);
directive.Properties.Add("dataSources", JObject.FromObject(sources));
return directive;
}
I expected it to serialized to the format specified in the link you shared. The "document" property is serialized with no issues but the "dataSources" property is never serialized.
As a reference, here's the code calling the method:
static async Task<IActionResult> HandleLaunchRequest(APLSkillRequest request, ILogger log, string executionPath)
{
log.LogInformation("Handle LaunchRequest");
var response = ResponseBuilder
.Ask("Welcome to my skill. How can I help", new Reprompt());
if (request.APLSupported())
{
var json = await File.ReadAllTextAsync(Path.Combine(executionPath,"APL", "launch.json"));
var launchTemplateApl = JObject.Parse(json);
var launchTemplateData = new ObjectDataSource
{
ObjectId = "launchScreen",
Properties = new Dictionary<string, object>(),
TopLevelData = new Dictionary<string, object>(),
};
launchTemplateData.Properties.Add("textContent", "My Skill");
launchTemplateData.Properties.Add("hintText", "Try, \"What can you do?\"");
var directive = CreateAplDirective(launchTemplateApl, ("launchTemplateData", launchTemplateData));
response.Response.Directives.Add(directive);
}
return new OkObjectResult(response);
}
Note that the properties Properties
and TopLevelData
for the ObjectDataSource
have to be assigned or, it will throw a null exception.
from alexa.net.apl.
Still doesn't work. But, yes, the JObject.FromObject()
in unnecessary.
from alexa.net.apl.
I've just ran the code you posted with two changes
- I had to remove the if statement (I didn't have a request to use)
- Instead of
OkObjectResult
I usedvar output = JsonConvert.SerializeObject(response);
because I was running it in the Alexa.NET.APL test project.
The output is below - with both a document
and dataSources
property. If you aren't seeing it in your output then there's something happening in the response of your skill stopping that from happening.
What I do notice straight away is that the type
property is wrong - which one of the community has coincidentally raised this morning as a PR into Alexa.NET - you will need to bump the version of Alexa.NET you're using when it's merged in, so you can keep an eye on it here:
timheuer/alexa-skills-dotnet#194
"directives": [
{
"Type": "Alexa.Presentation.APL.RenderDocument",
"document": {
"type": "APL",
"theme": "dark",
"version": "1.2",
"mainTemplate": null
},
"dataSources": {
"launchTemplateData": {
"type": "object",
"objectID": "launchScreen",
"properties": {
"textContent": "My Skill",
"hintText": "Try, \"What can you do?\""
}
}
}
}
]
If you could run the JsonConvert line I've mentioned and see if you also get the dataSources
property appear, that would help us figure out where the problem you're having is.
from alexa.net.apl.
Found it! My fault again... 😒
It looks like Alexa discards anything that does not recognize. The test tab doesn't show what receives. Only parts of it.
I had a copy/paste bug. I copied the variable name into the string so, these was an upper-case S. "dataSources" instead of "datasources"... 😱
Here's the method to generate the APL directive. It may be helpful for others:
static JsonDirective CreateAplDirective(string token, object apl, params (string, ObjectDataSource)[] dataSources)
{
var directive = new JsonDirective(RenderDocumentDirective.APLDirectiveType);
if (!string.IsNullOrEmpty(token))
directive.Properties.Add("token", token);
directive.Properties.Add("document", apl);
if (dataSources.Length != 0)
{
var sources = new Dictionary<string, ObjectDataSource>();
foreach (var (key, dataSource) in dataSources)
sources.Add(key, dataSource);
directive.Properties.Add("datasources", sources);
}
return directive;
}
Having to initialize the properties Properties
and TopLevelData
for the ObjectDataSource
seems to me to be an issue that should be fixed.
from alexa.net.apl.
Related Issues (20)
- Data property on Sequence is a List<object> HOT 7
- Parameters are now a string rather than an object with name property HOT 5
- Events should be an array HOT 3
- Properties that are currently APLAbsoluteDimensionValue prevent using expressions HOT 1
- JSON as APLDocument HOT 9
- Safe Updates to APL Versions with HOT 2
- Container "Data" property cant be data bound
- No ListDataSource HOT 4
- Add opacity to APLcomponent HOT 1
- Filling ListDataSource with Data HOT 1
- How to build "Karaoke Mode" and survive HOT 4
- StyleValue has an empty constructor with a parameter HOT 1
- APLComponent.When has wrong type HOT 2
- Question: How to add APL support in Azure Functions? HOT 2
- Component should implement IEnumerable<IComponent> HOT 2
- Setting slot values from UserEventRequest HOT 2
- Improve readme around referencing saved APL Docs HOT 1
- Container item array is serialized as a single item HOT 1
- Alexa headline footer does not work HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alexa.net.apl.