I'm trying to make sense of session, but cannot get any data out of it - node.js

Currently using LUIS in a bot that connects to Slack. Right now I'm using interactive messages and trying to respond to user input correctly. When I click an item from the drop down LUIS receives it as a message. I can get the text with session.message.text, however I need to get the callback_id of the attachment as well as the channel it was sent from.
I've used console.log(session) to get an idea of what session looks like. From there I've seen that session.message.sourceEvent contains the data I need, however I can't use indexOf() or contains() to actual extrapolate the data. I've also tried session.message.sourceEvent.Payload but end up getting "[object [Object]]". I've tried searching for documentation on session formatting but to no avail.
Below is a snippet of what is returned when I run console.log(session.message.sourceEvent).
{ Payload:
action_ts: '1513199773.200354',
is_app_unfurl: false,
subtype: 'bot_message',
team: { id: 'T03QR2PHH', domain: 'americanairlines' },
user: { id: 'U6DT58F2T', name: 'john.cerreta' },
message_ts: '1513199760.000073',
attachment_id: '1',
ts: '1513199760.000073' },
actions: [ [Object] ],
callback_id: 'map_selection1',
original_message:
username: 'Rallybot',
response_url: 'https://hooks.slack.com/actions/T03QR2PHH/287444348935/Y6Yye3ijlC6xfmn8qjMK4ttB',
type: 'message',
{ type: 'interactive_message',
channel: { id: 'G6NN0DT88', name: 'privategroup' },
token: 'removed for security',
{ text: 'Please choose the Rally and Slack team you would like to map below.',
bot_id: 'B7WDX03UM',
attachments: [Array],
trigger_id: '285857445393.3841091595.085028141d2b8190b38f1bf0ca47dd88' },
ApiToken: 'removed for security' }

session.message.sourceEvent is a javascript Object, however indexOf or contains are functions of String or Array types.
Any info you required in the object, you should direct use the code <object>.<key> to invoke that value. You can try session.message.sourceEvent.Payload.action_ts for example.
Also, you can use Object.keys(session.message.sourceEvent) to get all the keys in this object.

Related

Stripe warning - Unrecognized token creation parameter

I'm just starting with stripe, and I noticed I get a warning in chrome saying:
Unrecognized token creation parameter parameter: company is not a
recognized parameter. This may cause issues with your integration in
the future.
This is the code.
stripe.createToken("account", {
company: {
name: "bbb",
address: {
line1: "77",
city: "abc",
state: "aa",
postal_code: "e2e"
}
},
tos_shown_and_accepted: true
}).then(function(result) {
debugger;
console.log(result);
});
I'm pretty much following the docs here (step 2)
https://stripe.com/docs/connect/account-tokens
It creates a token OK though.
The docs in the API reference suggest company is an object it should know:
https://stripe.com/docs/api/tokens/create_account
It basically tells you what is wrong.
It says that company is not a parameter that is recognized by that stripe endpoint. It creates the token, but ignores your passed parameter

Instagram - media user and caption user stored separately

I'm playing around with instagram's API and got a JSON object as below, assume it's stored in variable media
{
id:'1222XXXXXX_XXXXX',
created_time: "1459991213",
user: {
username: "user1",
profile_picture: "http://link",
id: "7xxxx",
full_name: "SomeName"
},
caption: {
created_time: "1459991213",
text: "hello",
from: {
username: "user1",
profile_picture: "http://link",
id: "7xxxx",
full_name: "SomeName"
},
id: "1222831055034209838"
}
images:{...}
}
Notice that
media.user and media.caption.from are same user info.
media.created_time and media.caption.created_time are the same.
I'm curious why Instagram duplicate these information and if it's possible for them to have different values.
I tried to upload an image without a caption and added caption later but the media.caption.created_time simply inherits the value from media.created_time.
I don't know how to let other people to add/edit caption of my own image, so now sure if media.caption.from can be different from media.user.
Could anyone advise on this?

node-slack outgoing web-hook is missing the trigger property

My current hook that my application receives from slack looks like the following. Missing the trigger_word property.
{
token: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
team_id: 'XXXXXXXXXXX',
channel_id: 'XXXXXXXXX',
channel_name: 'channel',
timestamp: Invalid Date,
user_id: 'XXXXXXX',
user_name: 'user',
text: 'massive string'
}
The text property can be extrapolated to figure what the trigger value but this seems like a bit of a hassle. How can I make it so that it has the trigger_word as shown in the documentation?
The req.body had all the properties, but the node-slack module parsed through the hook and filtered out some useful ones.

Sails.js & MongoDB: duplicate key error index

I'm using Sails.js (0.9.8) and MongoDB (via the sails-mongo adaptor) to create a collection of pages that can be positioned in a tree-view. I would like to store the path of a page in an array of UUIDs
My model:
module.exports = {
schema: true,
attributes: {
uuid: {
type: 'string',
unique: true,
required: true,
uuidv4: true
},
name: {
type: 'string',
required: true,
empty: false
},
path: {
type: 'array',
required: true,
array: true
}
}
}
It works well when I save a 'root' page (the 'path' property has just one item because it's a root page. Here is what it was saved in MongoDB:
{
_id: ObjectId("52f853e9609fb6c0341bdfcc"),
createdAt: ISODate("2014-02-10T04:22:01.828Z"),
name: "Home Page",
path: [
"a2b23e1f-954b-49a3-91f1-4d62d209a093"
],
updatedAt: ISODate("2014-02-10T04:22:01.833Z"),
uuid: "a2b23e1f-954b-49a3-91f1-4d62d209a093"
}
But when I want to create a 'subpage' below my previous created page (Home Page/Products), I get this error:
MongoError: E11000 duplicate key error index: cms-project.item.$path_1
dup key: { : "a2b23e1f-954b-49a3-91f1-4d62d209a093" }
Here is the data I sent:
{ name: 'Products',
uuid: 'a004ee54-7e42-49bf-976c-9bb93c118038',
path:
[ 'a2b23e1f-954b-49a3-91f1-4d62d209a093',
'a004ee54-7e42-49bf-976c-9bb93c118038' ] }
I probably missed something but I don't know what.
If I store the path in a string instead of an array, it work well, but I find it much less elegant and handy.
Not sure of all the Sails / Waterline parts myself as I've never played with it. But by the error the problem is there is a unique index on your array field.
When you are inserting your second document, you already have one of the values (the parent) in your path field in another document. The unique constraint is not going to allow this. Most certainly for what you are modelling, you do not want this and the index cannot be unique.
I hope that you set this up yourself under the assumption that it meant unique within the array contained in the document. If you did then you know where to look and what to change now. If this is being automatically deployed somehow, then I'm not the one to help.
Change the index to not be unique. You can confirm this through the mongo shell:
use cms-project
db.item.getIndices()
Good luck

Asana API POST to Tasks leads to Server Error

I'm using node.js and the api key for auth. All my get actions work, and I've been able to post a new project, but new tasks always return 'server error'. Here's the object I'm sending to the /tasks endpoint:
data: {
name: 'Figure this out',
notes: '',
assignee: null,
completed: false,
assignee_status: 'later',
completed_at: null,
due_on: null,
tags: [],
parent: null,
followers: [ { id: 5622986387066 }, { id: 5622895459066 } ],
projects: [ 6156399090409 ],
workspace: 1707039411020
}
Any ideas? I've tried passing those ID values a variety of ways, and I've tried creating a more simple task, always fails with a 'server error' response.
Seems like it's the "parent": null that's causing the unhelpful Server Error, which definitely seems like a bug on our side - I've filed it and will hopefully get time to look into it soon. Trim that out, and it gives you actual error messages.
Just to save you some time: you can't set completed_at or tags, and followers should be just an array of integers ("followers": [ 5622986387066, 5622895459066 ]).
You can set completed: [true/false], and the completed_at will be set to the time at which it was marked complete. Not being able to attach tags to a task is a known issue, and one we're hoping to rectify.
Additionally, it's just a little annoying that the format of a response doesn't map 1-1 to the format for posting/updating. We're hoping to do a pass on the overall design of the API to unify those parts a bit more.

Resources