Access the context variables stored in watson conversation using node.js - node.js

I want to access the context variables saved in watson conversation JSON through an APP using node.js.
I have tried saving the whole conversation log into cloudant and fetched it from there.
Is there a easier way to access the context variables? I am thinking of sending a http request to server to fetch the right variables (I dont know which variables to access).

Depending on your needs you can store context in the browser session. This is what the conversation-simple app does.. https://github.com/watson-developer-cloud/conversation-simple
In this case the JSON context object is passed down to the browser, and passed back up again with subsequent requests.
The alternative is to store this info in a custom store such as Cloudant

The response that one get from Conversation service is in JSON format. So you can take out any context value that are available in the "context" param of this JSON response. Following is a simple response from the Conversation service.
{
"intents": [],
"entities": [],
"input": {
"text": ""
},
"output": {
"text": ["Hello MJ! How can I help you today?"],
"nodes_visited": ["Conversation Start"],
"log_messages": []
},
"context": {
"username": "MJ",
"conversation_id": "5835fa3b-6a1c-4ec5-92f9-22844684670e",
"system": {
"dialog_stack": [{
"dialog_node": "Conversation Start"
}],
"dialog_turn_counter": 1,
"dialog_request_counter": 1,
"_node_output_map": {
"Conversation Start": [0]
}
}
}
}
You will have all your context variables in the context key of the response. If you check the context parameter of this response you will see the "username": "MJ" entry. This is a custom value that I have added to the service context. You can format this response and use it in your application as per your need.

Related

Microsoft Teams bot finding message sender id

We have built a teams app that can be used in the group chat. So, basically any user can do
#
At the server side, we want to get the sending user and respond to the sent text based on who sent it. The code to get users in the conversation looks like below:
const connector = context.adapter.createConnectorClient(context.activity.serviceUrl);
const response = await connector.conversations.getConversationMembers(context.activity.conversation.id);
functions.logger.log("conversation members are:", response)
The response returns an array of all the users in the conversation with below structure
[
{
"id": "29:1a-Xb7uPrMwC2XqjMEHCC7ytV2xb2VUCqTA-n_s-k5ZyMCTKIL-ku2XkgbE167D_5ZbmVaqQxJGIQ13vypSqu-A",
"name": "Neeti Sharma",
"objectId": "718ab805-860c-43ec-8d4e-4af0c543df75",
"givenName": "Neeti",
"surname": "Sharma",
"email": "xxx#xxxx.xxx",
"userPrincipalName": "xxxx#xxxx.xxx",
"tenantId": "xxx-xx-xx-xxxxxx-x",
"userRole": "user"
},
{
...
}
]
The above response does not indicate who is the sender of the message in the group chat. How do we find that?
I'm not sure the exact syntax for Node (I work mostly in C#), but basically on the context.activity object there is a from property (i.e. context.activity.from), which is of type ChannelAccount (DotNet reference here, but it's very similar for Node). That will give you, at least, Name and AadObjectId. What you're using right now is getConversationMembers, which gives you everyone in the entire Channel, not just that particular message/thread.
turnContext.Activity.From.Id is also unique to each user. You can use that property too. Email is tough to get in any other events than the MembersAdded event.

Self-defined Function while creating a Digital twin?

It is possible to add a self-defined function while creating a digital Twin in Ditto as shown below.
"attributes": {
"location": "Germany"
},
"features": {
"temperature": {
"properties": {
"value": 100
}
},
"humidity": {
"properties": {
"value": 100
}
}
},
"BuiltinFuntion": {
if(Temparature > 20){
alert("Some message")
}
}
Note: One solution is to constantly check with Ditto HTTP APIs value and give alert message whenever it cross the threshold value. But I do not want to hit the APIs everytime. So please let me know if there is any alternate solution.
In order not to poll Ditto's API for changes, there are various other APIs supporting push-notifications.
For example, you can use the WebSocket API and use an filter expression defining filter=gt(features/temperature/properties/value,20) when subscribing for events.
Or you can use the SSE (Server Sent Events) API to do the same.
Both, the WebSocket and SSE API may directly be used in the browser - I suppose your alert you want to show is JavaScript, so I assumed your target environment for receiving push notifications is a browser.

Cannot change workitem's parent through the azure devops api

I'm trying to programatically change a workitem's parent using the azure devops api but it's not working as expected.
I tried using update link endpoint and also remove link endpoint but none of them seem to be the correct one given that there is no way I can get a relation ID for the parent-child relationship to use in the request path.
The "relation ID" to send in path: is just the index of the relation being changed or removed in the WorkItemRelation[] on the Work Item being PATCHed.
Use the $expand=Relations argument in the query string of the GET operation for the work item whose parentage you want to change (Get Work Item).
https://dev.azure.com/{YOUR_ORG}/{YOUR_PROJ}/_apis/wit/workitems/{Child_ID}?$expand=Relations&api-version=5.0-preview.2
note: I'm not exactly sure, but I think the {YOUR_PROJ} value can be omitted.
With the resulting workitem object, get the index of the relation where the relation type is Hierarchy-Reverse, and use this as the leaf of the "path": "/relations/{index}" property sent in the PATCH body with op: "remove".
Get response (abbreviated):
{
"rel": "System.LinkTypes.Hierarchy-Reverse",
"url": "https://dev.azure.com/{YOUR_ORG}/_apis/wit/workItems/{Parent_ID}",
"attributes": {
"isLocked": false
}
}
Patch request (body):
[
{
"op": "test",
"path": "/rev",
"value": 1
},
{
"op": "remove",
"path": "/relations/0"
}
]
The examples in the documentation tend to perform a test on the revision of the work item before executing the remove or add operation. This isn't necessary, but it's probably a good idea.

In azure logic app how to get key and value of url encoded data

In azure logic app how to get key and value of URL encoded data. I am not finding details regarding this.
I found way to work with application/x-www-url-formencoded data.
{
"$content-type": "application/x-www-form-urlencoded",
"$content": "<base64EncodedContent>",
"$formdata": [{
"key": "key1",
"value": "value1"
}
to get key in code view #triggerBody()['$formdata'][0]['key'].
to get value in code view #triggerBody()['$formdata'][0]['value'].
Some content types are supported and work with logic apps, but might require manually retrieving the message body by decoding the $content.
For example, suppose you trigger an application/x-www-url-formencoded request where $content is the payload encoded as a base64 string to preserve all data.
Because the request isn't plain text or JSON, the request is stored in the action as follows:
"$content-type": "application/x-www-form-urlencoded",
"$content": "<Base64EncodedContent>",
"$formdata": [{
"key": "ToCountry",
"value": "AU"
}
Being this a Form Data Post request, we can use the function #triggerFormDataValue() to get each of the properties, e.g. #triggerFormDataValue(‘Body’) and #triggerFormDataValue(‘From’).
For more details, you could refer to this blog.

How to set session variables in NodeJS with Google Home (DialogFlow)

I'm writing my first NodeJS app for Google Home (using DialogFlow - formerly API.ai).
I'm looking at the doc on this page: https://developers.google.com/actions/reference/v1/dialogflow-webhook
but I don't see any way to set session variables.
My current test program sets speech like this:
speechText = "I'm not sure that character exists!";
callback(null, {"speech": speechText});
In DialogFlow, my JSON after running looks like this, and it looks like maybe the "contexts" is where the session state would go?
{
"id": "3a66f4d1-830e-48fb-b72d-12711ecb1937",
"timestamp": "2017-11-24T23:03:20.513Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "test word",
"action": "MyAction",
"actionIncomplete": false,
"parameters": {
"WordNumber": "400"
},
"contexts": [],
"metadata": {
"intentId": "a306b829-7c7a-46fb-ae1d-2feb1c309124",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"webhookResponseTime": 752,
"intentName": "MyIntentName"
},
"fulfillment": {
"messages": [{
"type": 0,
"speech": ""
}]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success",
"webhookTimedOut": false
},
"sessionId": "fe0b7d9d-7a55-45db-9be9-75149ff084fe"
}
I just noticed from a chat bot course that I bought that you can set up Contexts like this, but still not sure exactly how the contexts get set and passed back and forth between the response of one call of my program to the request in the next call of my program (defined via "webhook").
When I added the contexts above, DialogFlow wouldn't recognize my utterance any longer and was giving me the DefaultFallback response. When I remove them, my AWS Lambda get's called.
For starters, the documentation page you're looking at refers to a deprecated version of the API. The page that talks about the current version of the api (v2) is https://developers.google.com/actions/dialogflow/webhook. The deprecated version will only be supported for another 6 months or so.
You're on the right track using Contexts! If you were using Google's actions-on-google node.js library, there would be some additional options - but they all use Contexts under the scenes. (And since they do use Contexts under the scenes - you should make sure you pick Context names that are different from theirs.) You can also use the sessionId and keep track of things in a local data store (such as DynamoDB) indexed against that SessionID. But enough about other options...
A Context consists of three elements:
A name.
A lifetime - for how many messages from the user will this context be sent back to you. (But see below about re-sending contexts.)
An object of key-value strings.
You'll set any contexts in the JSON that you return as an additional parameter named contextOut. This will be an array of contexts. So your response may look something like this:
var speechText = "I'm not sure that character exists!";
var sessionContext = {
name: "session_variables",
lifespan: 5,
parameters: {
"remember": "one",
"something": "two"
}
};
var contextOut = [sessionContext];
var response = {
speech: speechText,
contextOut: context
};
callback(null, response);
This will include a context named "session_variables" that stores two such variables. It will be returned for the next 5 messages sent to your webhook. You can, however, add this to every message you send, and the latest lifetime and parameters will be the ones that are sent back next time.
You'll get these contexts in the JSON sent to you in the result.contexts array.
The "Context" field on the Intent screen is used for an additional purpose in Dialogflow beyond just preserving session information. This indicates that the Intent is only triggered if the specified Context exists (lifetime > 0) when the phrase tries to be matched with it (or when handling a fallback intent). If you're using a webhook, the "Context Out" field is ignored if you send back contexts yourself.
This lets you do things like ask a particular question and set a Context (possibly with parameters) to indicates that some answers should be understood as being replies to the question you just asked.

Resources