I tried to adapt from code that I found on stack overflow but to no avail. I am trying to log what happens on each iteration of my post call. The promises are always logging as pending and I can't seem to get an end result on them. Any help would be greatly appreciated.
var axios = require('axios')
url2 = `https://api.hubapi.com/properties/v1/contacts/properties?hapikey=xxx`
var mainObject = {},
promises = [];
for(var i = 0; i < 10; i++){
//console.log("The number i is: ",i)
promises.push(axios.post(url2,
{
"name": "newcustomproperty" + i,
"label": "A New Custom Property",
"description": "A new property for you",
"groupName": "contactinformation",
"type": "string",
"fieldType": "text",
"formField": true,
"displayOrder": 6,
"options": []
}
))
//console.log(promises)
}
//console.log(promises)
axios.all(promises).then(function(results) {
results.forEach(function(response) {
console.log(response)
mainObject[response.identifier] = response.value;
}).catch(function(response) {
mainObject[response.identifier] = response.value
})
});
Related
I'm currently using JsForce + Express, but I find that when I attempt to manipulate the data in the backend, the API call is extremely slow. Specifically, when I drill down to the Ids/Names of each of the individual objects, it slows down dramatically/fails to load.
It loads in less a second if I use this code:
router.get("/testChain", (req, res) => {
conn
.query(
"SELECT Id, Name, (SELECT Id, Part_Type__c FROM Monster_Parts__r) FROM Monster_Frame__c"
)
.then((result) => {
let gMonster = {};
let frames = result.records;
// Select Frame
let sFrameNo = randomNumber(frames.length);
let sFrame = frames[sFrameNo];
gMonster["Monster_Frame__c"] = sFrame.Id;
// Break into parts
let parts = sFrame.Monster_Parts__r.records;
let sortedParts = parts.reduce((r, o) => {
var k = o.Part_Type__c;
if (r[k] || (r[k] = [])) r[k].push(o);
return r;
}, {});
// Select Part
Object.keys(sortedParts).forEach((key, i, arr) => {
let parts = sortedParts[key];
let partNo = randomNumber(parts.length);
let selectedPart = parts[partNo];
gMonster[key] = selectedPart;
});
// Generate Name
gMonster["Name"] = sFrame.Name + "-" + randomNumber();
return gMonster;
})
.then((result) => {
res.json(result);
});
});
And gives me the following output:
{
"Monster_Frame__c": "a025j000004GsdXAAS",
"Tail__c": {
"attributes": {
"type": "Monster_Parts__c",
"url": "/services/data/v42.0/sobjects/Monster_Parts__c/a015j00000CfGUXAA3"
},
"Id": "a015j00000CfGUXAA3",
"Part_Type__c": "Tail__c"
},
"Leg__c": {
"attributes": {
"type": "Monster_Parts__c",
"url": "/services/data/v42.0/sobjects/Monster_Parts__c/a015j00000CfGUeAAN"
},
"Id": "a015j00000CfGUeAAN",
"Part_Type__c": "Leg__c"
},
"Head__c": {
"attributes": {
"type": "Monster_Parts__c",
"url": "/services/data/v42.0/sobjects/Monster_Parts__c/a015j00000CfGU0AAN"
},
"Id": "a015j00000CfGU0AAN",
"Part_Type__c": "Head__c"
},
"Body__c": {
"attributes": {
"type": "Monster_Parts__c",
"url": "/services/data/v42.0/sobjects/Monster_Parts__c/a015j00000CfGUKAA3"
},
"Id": "a015j00000CfGUKAA3",
"Part_Type__c": "Body__c"
},
"Back__c": {
"attributes": {
"type": "Monster_Parts__c",
"url": "/services/data/v42.0/sobjects/Monster_Parts__c/a015j00000CfGUPAA3"
},
"Id": "a015j00000CfGUPAA3",
"Part_Type__c": "Back__c"
},
"Name": "Serpent-7210"
}
However, if I change this line from:
gMonster[key] = selectedPart;
to populate the specific Id (instead of all the fields):
gMonster[key] = selectedPart.Id;
The API call fails 60% of the time. (Sometimes it returns with the desired output of carrying only the Ids.
Does anyone know how to fix this?
This question already has answers here:
How to update a value in a json file and save it through node.js
(8 answers)
Closed 2 years ago.
I am new to Nodejs and JSON manipulations. I have a jSON that looks like
"ent": [
{
"employee": [
{
"emp_name": "",
"column": "employee",
"emp_id": 123456,
"first name": "Joe",
"last name": "Bloggs",
"email": "",
"ldapid":
} ,
{
"emp_name": "",
"column": "employee",
"emp_id": 123456,
"first name": "Foo",
"last name": "Bars",
"email": "",
"ldapid":
}
]
}
]
I need to fill the email, ldapid and emp_name based on the firstname and last name
The desired output is
"ent": [
{
"employee": [
{
"emp_name": "Joe Bloggs",
"column": "employee",
"emp_id": 123456,
"first name": "Joe",
"last name": "Bloggs",
"email": "jbloggs#mycompemail.com",
"ldapid": "jbloggs"
} ,
{
"emp_name": "Foo Bars",
"column": "employee",
"emp_id": 567891,
"first name": "Foo",
"last name": "Bars",
"email": "fbars#mycompemail.com",
"ldapid": "fbars"
}
]
}
]
Since I am super new to the nodeJS world , I am making some initial steps to get to where I want..
The following is what I have done..
EDITED my POST
Hi All, Thanks for all your responses.
I was hoping to get an answer that did something similar to the below. this may not be a code with best practices, but does what I want, may be experts in this group can make it better.
const fs = require('fs');
/** Method to start
*
*
*/
const main = async () => {
const myJSONObject = require('./people.json');
try {
for (var i = 0; i < myJSONObject.entities.length; i++) {
var entity = myJSONObject.entities[i];
if (entity.People) {
for (var j = 0; j < entity.People.length; j++) {
var people = entity.People[j];
var fn = people["first name"];
var ln = people["last name"];
var email = `${fn.substring(0, 3)}${ln.substring(0, 5)}#mycompmail.com`;
var ldapid = `${fn.substring(0, 3)}${ln.substring(0, 5)}`;
myJSONObject.entities[i].People[j]["email"] = email.toLowerCase();
myJSONObject.entities[i].People[j]["ldap id"] = ldapid.toLowerCase();
myJSONObject.entities[i].People[j]["preferred first name"] = fn;
myJSONObject.entities[i].People[j]["preferred last name"] = ln;
// console.log(`${fn}.${ln}`)
}
}
}
fs.writeFileSync('./new_people.json', JSON.stringify(myJSONObject, 0, 4));
}
catch (error) {
console.log(error);
}
};
(async () => {
await main();
})();
Any help in this is highly appreciated.
Vakichak
From your code snipped I assume, that the JSON is a string in a file.
So the first step you need to do is to import the file contents into a variable. You can do that with fs.readFileSync(). Now you have the string in a variable.
Next you need to do is to convert the string into an object. You can do that with JSON.parse(). Now you have an object that you can manipulate.
To write it the object back into a file, you can use JSON.stringify() to make it a string again and then fs.writeFileSync() to write it to the file.
Full script:
const jsonString = fs.readFileSync('./people.json')
const jsonObject = JSON.parse(jsonString)
// do stuff with jsonObject that is written into newJsonObject
const newJsonString = JSON.stringify(newJsonObject)
fs.writeFileSync('./new_people.json', newJsonObject)
Note: there's also async functions for writing and reading files. The sync functions are okay if you load a config or something like this at the beginning of a script. If you read/write many files during runtime, you should use the async functions.
I am using Microsoft teams bot with nodejs. I am rendering a carousel of adaptive cards with action on each card. My requirement is to delete an individual card out on which the action was clicked. Is it possible?
Current code looks like below. i have given a try to deleteActive but that deletes entire carousel
const {
TurnContext,
TeamsActivityHandler,
CardFactory,
AttachmentLayoutTypes,
ActionTypes
} = require('botbuilder');
class TeamsConversationBot extends TeamsActivityHandler {
constructor() {
super();
this.onMessage(async (context:any, next:any) => {
TurnContext.removeRecipientMention(context.activity);
console.log("context activigty at the begin is:" + JSON.stringify(context.activity))
let msg = context.activity.text
let action = context.activity.value
if(msg.startsWith('lead')){
msg = 'lead'
}
if(action !== undefined){
console.log("user did some action on a card")
msg = action.action
}
switch (msg) {
case 'lead':
await this.lead(context)
break;
case 'qualify_lead':
await this.qualifyLead(context)
break;
}
await next();
});
}
/**
*
* #param context this method does a lead qualification
*/
async qualifyLead(context:any){
console.log("in qualifyLead:" + JSON.stringify(context.activity))
//await context.deleteActivity(context.activity.replyToId)
const leadId = context.activity.value.objectId
console.log("Lead to qualify is:" + leadId)
await context.sendActivity('Lead is qualified')
}
/**
* Search contact by name
* #param context
* #param keyword
*/
async lead(context:any){
console.log("Start of lead with context:" + JSON.stringify(context))
const cardArr = []
let items = [
{"Name": 'x', "LeadId": "1"},
{"Name": 'a', "LeadId": "2"},
{"Name": 'b', "LeadId": "3"},
{"Name": 'c', "LeadId": "4"},
{"Name": 'd', "LeadId": "5"}
]
for(const item of items){
const header = {
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": item.Name
}
const actions = [
{
"type": "Action.Submit",
"title": "Qualify",
"data": { "action" : "qualify_lead", "objectId" : item.LeadId }
}
]
const acard = CardFactory.adaptiveCard(
{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
header,
''
],
"actions": actions
}
)
cardArr.push(acard)
console.log("payload is::::" + JSON.stringify(acard))
}
const reply = {
"attachments" : cardArr,
"attachmentLayout" : AttachmentLayoutTypes.Carousel
}
await context.sendActivity(reply);
}
}
module.exports.TeamsConversationBot = TeamsConversationBot;
As with this other answer, the answer will be similar to this one. I can see you're trying to use TypeScript but your code deviates very little from JavaScript so I'll just write my answer in JavaScript.
First, you'll need a way of saving state for your [carousel] so you can update the [carousel]'s activity.
this.carouselState = this.conversationState.createProperty('carouselState');
You'll want a consistent way to generate your [carousel] that you can use when you send the [carousel] initially and when you update the [carousel].
createCarousel(batchId, leads)
{
const cardArr = [];
let items = [
{ "Name": 'x', "LeadId": 1 },
{ "Name": 'a', "LeadId": 2 },
{ "Name": 'b', "LeadId": 3 },
{ "Name": 'c', "LeadId": 4 },
{ "Name": 'd', "LeadId": 5 }
];
items = items.filter(item => leads.includes(item.LeadId));
for (const item of items) {
const header = {
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": item.Name
};
const actions = [
{
"type": "Action.Submit",
"title": "Qualify",
"data": { [KEYACTION]: ACTIONQUALIFYLEAD, [KEYOBJECTID]: item.LeadId, [KEYBATCHID]: batchId }
}
];
const acard = CardFactory.adaptiveCard(
{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
header
],
"actions": actions
}
);
cardArr.push(acard);
}
return {
"type": "message",
"attachments": cardArr,
"attachmentLayout": AttachmentLayoutTypes.Carousel
};
}
This is similar to your code but there are some important differences. First, I'm filtering the items array to allow for fewer items, which is how you'll end up deleting cards from your carousel. Second, I'm including a "batch ID" in each action's data, which is how your bot will know which activity to update when it receives the action's payload. Also, this isn't relevant to your question but I'm using string constants instead of string literals most everywhere I expect to use that string more than once, which is a practice I follow to avoid typo-related bugs etc.
Using this function, you can send the [carousel] initially like this
async testCarousel(turnContext) {
const batchId = Date.now();
const leads = [1, 2, 3, 4, 5];
const reply = this.createCarousel(batchId, leads);
const response = await turnContext.sendActivity(reply);
const dict = await this.carouselState.get(turnContext, {});
dict[batchId] = {
[KEYACTIVITYID]: response.id,
[KEYLEADS]: leads
};
}
And you can update the [carousel] in response to the card's [qualify] submit action like this
async handleSubmitAction(turnContext) {
const value = turnContext.activity.value;
switch (value[KEYACTION]) {
case ACTIONQUALIFYLEAD:
const dict = await this.carouselState.get(turnContext, {});
const batchId = value[KEYBATCHID];
const info = dict[batchId];
if (info) {
const leads = info[KEYLEADS];
const objectId = value[KEYOBJECTID];
var index = leads.indexOf(objectId);
if (index !== -1) leads.splice(index, 1);
const update = this.createCarousel(batchId, leads);
update.id = info[KEYACTIVITYID];
if (update.attachments.length) {
await turnContext.updateActivity(update);
} else {
await turnContext.deleteActivity(update.id);
}
}
break;
}
}
I am using the Microsoft Bot Framework with Node.js. I have a config file that looks like the following.
{
"server": {
"port": 3978
},
"dialogs": {
"default": {
"text": "This is some sample text.",
"actions": [
{
"title": "Button 1",
"value": "Action 1"
},
{
"title": "Button 2",
"value": "Action 2"
}
]
},
"hello": {
"text": "hello",
"matches": "^hello$"
},
"asdf": {
"text": "asdf",
"matches": "^asdf$"
},
"goodbye": {
"text": "goodbye",
"matches": "^goodbye$"
}
}
}
I want to use a for loop to read through the dialogs and create them so that they respond with the text value and have the trigger action of the matches value.
For example, the bot responds hello to the input of hello, asdf to the input of asdf, and goodbye to the input of goodbye.
The function I have written in an attempt to solve this looks like this.
var create = function(bot, _config) {
var config = JSON.parse(JSON.stringify(_config));
// Create dialogs from config
var keys = Object.keys(config.dialogs);
for(var i = 0; i < keys.length; i++) {
var dialogName = keys[i];
var dialog = config.dialogs[dialogName];
// Skip over default dialog
if(dialogName == "default") continue;
// Create other dialogs
bot.dialog(dialogName, function(session) {
var text = dialog.text;
session.endDialog(text);
}).triggerAction({
matches: new RegExp(dialog.matches, "i")
});
}
}
When I run this, the bot responds with goodbye to the inputs of hello, asdf, and goodbye. However, the console shows that the correct dialogs are being called when they are supposed to. Even when I call the hello dialog by using session.beginDialog('hello');, the bot returns goodbye.
What seems to be causing the problem here?
It's a common “gotchas” of var in javascript. replace var to let should fix your issue.
The issue similar with
for (var i = 0; i < 10; i++) {
setTimeout(function() { console.log(i); }, 100 * i);
}
The root cause is var is function-scoped and let is block-scoped. You can refer to https://www.typescriptlang.org/docs/handbook/variable-declarations.html for details.
Something is messed up with my AMAZON.StopIntent. No matter what I put there (I've tried everything from every tutorial), whenever it's called, I get "There was a problem with the requested skill's response" and the Alexa app shows the error as "speechletresponse cannot be null". My project is in the JSON, not Java format.
If anyone can help, I'd very much appreciate it!
Thanks!
As requested here's what is being sent to Lambda
{
"session": {
"sessionId": "SessionId.XXXXX",
"application": {
"applicationId": "amzn1.ask.skill.XXXXXXX"
},
"attributes": {},
"user": {
"userId": "amzn1.ask.account.XXXXXXX"
},
"new": true
},
"request": {
"type": "IntentRequest",
"requestId": "EdwRequestId.XXXXXX",
"locale": "en-US",
"timestamp": "2017-01-18T22:38:53Z",
"intent": {
"name": "AMAZON.StopIntent",
"slots": {}
}
},
"version": "1.0"
}
And here's the relevent code:
var handlers = {
'LaunchRequest': function () {
this.emit('AMAZON.HelpIntent');
},
'GetNewDogThoughtIntent': function () {
this.emit('GetDogThought');
},
'GetNewCatThoughtIntent': function () {
this.emit('GetCatThought');
},
'GetDogThought': function () {
var dogthoughtIndex = Math.floor(Math.random() * DOGTHOUGHTS.length);
var randomDogThought = DOGTHOUGHTS[dogthoughtIndex];
// Create speech output
var speechOutput = "Your dog is thinking, " + randomDogThought;
this.emit(':tellWithCard', speechOutput, "Your dog was thinking... ", randomDogThought);
},
'GetCatThought': function () {
var catthoughtIndex = Math.floor(Math.random() * CATTHOUGHTS.length);
var randomCatThought = CATTHOUGHTS[catthoughtIndex];
// Create speech output
var speechOutput = "Your cat is thinking, " + randomCatThought;
this.emit(':tellWithCard', speechOutput, "Your cat was thinking... ", randomCatThought);
},
'AMAZON.HelpIntent': function () {
var speechOutput = "You can ask me for what your cat or dog is thinking, or you can say exit... Right now I can only provide thoughts for one cat or dog at a time... What can I help you with?";
var reprompt = "What can I help you with? Make sure to say if your pet is a cat or dog when you ask!";
this.emit(':ask', speechOutput, reprompt);
},
'SessionEndedRequest': function (sessionEndedRequest, session) {
},
"AMAZON.StopIntent": function (shouldEndSession) {
}
I finally got it after consulting the SpaceGeek tutorial again and making some tweaks to it. Basically, here's what worked:
'AMAZON.StopIntent': function () {
this.emit(':tell', "Goodbye!");
}
The key was the ':tell', which I didn't have before. Thanks to everyone who answered and helped!
Can you post your code for the StopIntent? You should be calling a speechlet response in it. For example:
'AMAZON.StopIntent': function (shouldEndSession, response) {
var speechOutput = "Goodbye";
response.tell(speechOutput);
},
Are you building that response properly and passing it?
I have found this link on alexa developer forum. This might help in your issue..
https://forums.developer.amazon.com/questions/49211/system-error-speechletresponse-was-null.html
I am writing this code in php, if that helps
$data = file_get_contents("php://input");
$jsonData = json_decode($data);
if($jsonData->request->type === "IntentRequest"){
$IntentName = $jsonData->request->intent->name;
if($IntentName === "AMAZON.StopIntent"){
$response = '{
"version" : "1.0",
"response" : {
"outputSpeech": {
"type": "PlainText",
"text": ""
},
"shouldEndSession" : false
}
}';
echo $response;
}
}