How to Implement a Session Variable on MS Bot Framework - botframework

I'm searching for guidance about how to implement a session-level variable on bot framework.
I've created a class to store global vars that is working fine.
However, these variables are persisted across all bot sessions which is not what I need now.
Thanks in advance,

I'm not sure of you are using the Node or the C# SDK. You can easily store data like this in the Databags provided by the SDK. UserData, ConversationData, and PrivateConversationData
For the purposes of just demonstrating how to use this i'm only going to use UserData but any databag could be used the same way
Node
For node, you would set values as if you were using a dictionary or hashmap in other languages:
You can use either dot notation or bracket notation:
session.userData.SomeProperty = "someValue";
session.userData["SomeProperty"] = "someValue"
And then to use the data later, again you can use either dot notation or bracket notation:
var foo = session.userData.SomeProperty;
var foo = session.userData["SomeProperty"]
C#
To set Data:
context.UserData.SetValue("SomeProperty", "SomeValue");
To get data:
context.UserData.GetValue<string>("SomeProperty");
In C# if you need to do this in a place you do not have access to the context object refer to this SO answer

Related

FHIR Observation DSTU2 resource

I need some help getting the value and unit of a result from the FHIR Observation DSTU2 resource. I want to map these values to strings but it looks like Observation.value[x] can have different type of data. Any thoughts on how to do this in C#? I tried a few ways but no luck so far because the sandbox I'm using contains results as strings, Quantity and CodeableConcept.
http://hl7.org/fhir/observation-definitions.html#Observation.value_x_
For the Observation.value field you indeed have a choice of type, so the data in the FHIR resource can hold any of the choices listed for that field.
If you use the Hl7.Fhir.Dstu2 library - the official C# reference implementation available through NuGet, you can use it to easily retrieve the resources from your sandbox and get them into a POCO. Here's an example:
using Hl7.Fhir.Model;
using Hl7.Fhir.Rest;
var client = new FhirClient("<your sandbox url>");
var obs = client.Read<Observation>("Observation/<technical id>");
// now you can access obs.Value regardless of the type in it
if you need to serialize the data to xml or json, you use the serializer:
using Hl7.Fhir.Serialization;
var serializer = new FhirJsonSerializer();
Console.WriteLine(serializer.SerializeToString(obs));

Describe enums in Bot Framework & FormFlow with JSON Schema

I'd like to create a Bot using the FormFlow with JSON Schema approach. However, I need a bit more flexibility for displaying the answer options, since those need to be whole sentences and not only single words.
Is it possible to extend the enums specified inside the JSON file with descriptions that will be offered as options instead of the enum itself?
As I understand this is possible in code by using the Describe-Attribute.
You could use the "Define" property with custom script. The Sandwich Bot example is doing it this way (from json-schema-example):
"Define": "field.SetType(null).AddDescription(\"cookie\", DynamicSandwich.FreeCookie).AddTerms(\"cookie\", Language.GenerateTerms(DynamicSandwich.FreeCookie, 2)).AddDescription(\"drink\", DynamicSandwich.FreeDrink).AddTerms(\"drink\", Language.GenerateTerms(DynamicSandwich.FreeDrink, 2)); return true;",

Typescript Window Setting Variable

I have part of typescript code
var root = window['appPath']
window.location.replace(root + '.... some url
Is the variable windows part of typescript? How do I assign it? Is it a key/value and what can I store in it?
Thanks
Is the variable windows part of typescript
Its a part of browser spec : http://www.w3schools.com/jsref/obj_window.asp
TypeScript defines these common globals for you in a file lib.d.ts : https://github.com/Microsoft/TypeScript/blob/master/bin/lib.d.ts
How do I assign it?
You shouldn't and actually browsers will not let you.
Is it a key/value and what can I store in it?
All JavaScript objects (I'm being lose here) act like key/value stores. The only allowed key is string. Value can be any other JavaScript object.
That said. DONT PUT STUFF ON WINDOW. MILLIONS OF KITTENS WILL DIE

Moving SP-Linq queries from test site to production site

I see articles on using SPMetal to generate the .cs file that allows LINQ to work properly. The file I'm talking about inherits from the Microsoft.SharePoint.Linq.DataContext class. How can I use LINQ without recompiling on my production environment, since I would need to regenerate this file using SPMetal on my production environment? I suspect the answer is going to be "can't do it".
I guess I'll use a CAML query instead unless there is some easier way to use LINQ that I am missing.
If the objective is just to query lists using LINQ and you want to avoid such recompilations, do not use SPMetal.
LINQ can be directly used on SPListItemCollection
e.g.
var FindCustomer = from SPListItem Item in Customers.Items
where Item["Orders"] as int == 5
select Item;
//or select new{Title = Item["Title"]}
This does not have hard coded entities but is more flexible. And as long as your list column names remain same, code can be deployed on any environment even if other lists are changing.
Also you can choose to retrieve few chosen field's data instead of retrieving data of all the fields every time.
There is no problem I guess. Personally I have been using Linq for good amount of time. I never generated the cs specifically for production. Is your site different across environments?
Im not sure if I'm missing the point or not, but the DataContext object takes the URL as apart of the constructor, so you should retrieve the URL from config somewhere E.g. database
DataContext teamSite = new DataContext("http://MarketingServer/SalesTeam");
OR use the SPContext object, if your code has a SharePoint context. E.g. in a web part
DataContext teamSite = new DataContext(SPContext.Current.Web.Url);

OrderBy("it." + sort) -- Hard coding in LINQ to Entity framework?

I have been trying to use dynamic LINQ to Entity in my application for specifying the OrderBy attribute at runtime. However when using the code as described in the majority of documentation:
var query = context.Customer.OrderBy("Name");
I received the following exception:
System.Data.EntitySqlException: 'Name' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly.
After much searching I found this MSDN page:
http://msdn.microsoft.com/en-us/library/bb358828.aspx
Which included the following code example:
ObjectQuery<Product> productQuery2 = productQuery1.OrderBy("it.ProductID");
This prompted me to change my code to the following:
var query = context.Customer.OrderBy("it.Name");
After this the code works perfectly. Would anyone be able to confirm that this is indeed the correct way to get OrderBy working with LINQ to Entity? I can’t believe that the framework would have been implemented in this way, perhaps I have overlooked something?
Thanks, Matt
The it.Name syntax is ESQL and is indeed specific to the EF. There are good reasons to use this sometimes (e.g., collation specifiers), but it's not what I normally do.
Usually I use standard LINQ expressions:
var query = context.Customer.OrderBy(p => p.Name);
You can also use System.Linq.Dynamic, if you download it from Code Gallery, and then your original query:
var query = context.Customer.OrderBy("Name");
...will work.
No nice way, so far
My answer to this question was to create a stored procedure which has parameter to control sorting.

Resources