Unable to update the custom_attribute field using the node.js intercom-client - node.js

I am using the official Intercom node.js 'intercom-client' library (latest version -- 4.0, and node version 16.18) to try to add data to the custom_attribute field for an existing conversation, but keep getting an 'attribute does not exist' error.
According to the Intercom API documentation, it is possible to modify the custom_attribute field of a conversation: https://github.com/intercom/intercom-node#update-a-conversation
https://developers.intercom.com/intercom-api-sreference/reference/update-a-conversation
Using the node.js 'intercom-client' library to interact with the API in javascript, I attempted the following:
Passing the following object: { test: 'hope' }; to the client.conversations.update() method using the following code:
const customAttributes = { test: 'hope' };
client.conversations.update({
id: '68',
customAttributes,
});
I get the following error:
errors: [
{
code: 'parameter_invalid',
message: "Conversation attribute 'test' does not exist"
}
]
I would have expected it to modify the conversation object successfully.
Per the official documentation on the git-hub repo, the following should work:
const response = await client.conversations.update({
id,
markRead: true,
customAttributes: {
anything: 'you want',
},
});
but I get the same error, when I use this code exactly as it is written. I have tried to use snake case for the 'customAttributes' field, i.e. tried 'custom_attributes' without success. I have also tried to pass this object to the conversation at time it is created (using client.conversation.create()), given that the error is suggesting that the object should have been passed earlier, but without success (nothing happens...).
Thank you for your help!
Dan

Related

Electron WebRequest events produce UnhandledPromiseRejectionWarning: TypeError: Invalid url pattern

I'm trying to leverage the WebRequest onBeforeSendHeaders API. But I'm not having any luck getting it to trigger. As an experiment I tried the other APIs and I similarly see no activity.
Is there anything obviously wrong with my setup. My code looks like
const view = new BrowserView({
webPreferences: {
nodeIntegration: false,
partition: LOGIN_PARTITION,
enableRemoteModule: true
}
})
....
view.webContents.session.webRequest.onBeforeSendHeaders(filter, (details, callback) => {
logger.info(`onBeforeSendHeaders called for ${details}`)
details.requestHeaders['My-User-Agent'] = 'MyAgent'
callback({ requestHeaders: details.requestHeaders })
})
....
view.webContents.loadURL(url.href)
I've also noticed an unexpected issue. Namely on the documentation it sets a filter like
urls: ['https://*.github.com/*', '*://electron.github.io']
but for me this produces an error, namely
UnhandledPromiseRejectionWarning: TypeError: Invalid url pattern *://electron.github.io: Empty path.
I'm using an old version of Electron (10.1.5) and NodeJS (v10.24.1). I'm wondering if they are causing issues.
Update
Created a repo to demonstrate this issue at https://github.com/sregger/electron-webrequest
Here I'm using Electron 16 and NodeJS 16
Figured out the issue, which I had not included in the sample above. The problem was
const filter = null
which I put in place because using undefined throws
(node:831) UnhandledPromiseRejectionWarning: TypeError: Must pass null or a Function
Based on the following documentation
The filter object has a urls property which is an Array of URL patterns that will be used to filter out the requests that do not match the URL patterns. If the filter is omitted then all requests will be matched.
I assumed the entire object should be undefined, then null due to the above. Instead it should be
const filter = {
urls: []
}
When null is used no url matches. When an empty array is used all urls match.

OpenAI Fine tuned engine not working in Node - "Engine not found"

I have fine-tuned an engine on OpenAI using my own data. I can access the engine in the Playground with no issues, however, when I try to access it programmatically using Node & the openai Node library, I get an "Engine not found" error. The weird thing is, I could have sworn it worked before.
Anyway, here is my code:
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
async function getDream() {
const completion = await openai.createCompletion("davinci:ft-personal-2022-04-09-19-12-54", {
prompt: "I dreamed",
});
console.log(completion.data.choices[0].text);
}
getDream();
Here's the error:
error: {
message: 'Engine not found',
type: 'invalid_request_error',
param: null,
code: null
}
I tried running the same code but with a regular engine (davinci). Works fine that way. I've double-checked that I have the name of my fine-tuned engine right and tried it using the engine ID just in case. Nothing seems to work.
PS - I have run this using the OpenAI CLI as well using openai api completions.create -m davinci:ft-personal-2022-04-09-19-12-54 -p "I dreamed" and that works as well.
I'm back, answering my own question in hopes that it will help others.
It seems that if you are using your own fine-tuned model, you have to use a different method than if you use one of the default models. Instead of createCompletion, you should use createCompletionFromModel. So the function above (getDream) should actually look like this:
async function getDream() {
const response = await openai.createCompletionFromModel({
model: 'davinci:ft-personal-2022-04-09-19-12-54',
prompt: 'I dreamed'
// add other parameters here
});
}
You can add other completion parameters to this below prompt, e.g. temperature, frequency_penalty, etc.

Adding query param to mailchimp request with Node.js client library

I am trying to list out all my interests from the MailChimp api using the #mailchimp/mailchimp_marketing npm library, as that is what they use as examples for node.js in their docs.
Link to the npm library:
https://www.npmjs.com/package/#mailchimp/mailchimp_marketing
Link to the relevant documentation for this specific endpoint: https://mailchimp.com/developer/api/marketing/interests/list-interests-in-category/
Now, I can get my interests just fine with the example code there:
const run = async () => {
const response = await client.lists.listInterestCategoryInterests(
"list_id",
"interest_category_id"
);
console.log(response);
};
run();
The problem is, that by default the MailChimp API only returns the first 10 items in the list, and I need 15. There is an easy way to change this, of course, by adding a query param count=15. However, I can't find any way to pass on a query param with the listInterestCategoryInterests method as provided through the official library.
!TL;DR! So my question is:
Does anybody know how to pass on query params to the mailchimp API through the official node.js npm library, or do I really have to resort to just dropping the library entirely as it does not provide this basic functionality?
You need to list params as a string in an array:
const response = await client.lists.listInterestCategoryInterests({fields:[ "list_id,interest_category_id"]}
);
NOTE: A prefix maybe required as per below:
const response = await mailchimp.reports.getAllCampaignReports({fields:["reports.campaign_title,reports.clicks"]})
Result:
[0] {
[0] reports: [
[0] { campaign_title: 'COACT EMAIL CAMPAIGN', clicks: [Object] },
[0] { campaign_title: '', clicks: [Object] }
[0] ]
[0] }
const response = await mailchimp.lists.getListMembersInfo(listId,
{
count: 1000
});
For everyone coming here hoping to learn how to pass QUERY params into mailchimp marketing library methods:
The query parameters are taken from opts object - the object properties have to be camelCase.
In terms of which parameter for the method the opts object is - it depends on the method and you might need to check the method's source code, but probably second or third parameter.
As for the question for the concrete method, this should be the solution:
await client.lists.listInterestCategoryInterests(
"list_id",
"interest_category_id",
{ count: 15 }
);

Accessing response headers using NodeJS

I'm having a problem right now which I can't seem to find a solution to.
I'm using Uservoice's NodeJS framework to send some requests to UserVoice regarding Feedback posts. A problem I've run into are ratelimits so I want to save the header values X-Rate-Limit-Remaining, X-Rate-Limit-Limit and X-Rate-Limit-Reset locally. I've made a function for updating and getting that value and am calling it like this:
var content = "Test"
c.post(`forums/${config.uservoice.forumId}/suggestions/${id}/comments.json`, {
comment: {
text: content
}
}).then(data => {
rl.updateRL(data.headers['X-Rate-Limit-Limit'],data.headers['X-Rate-Limit-Remaining'],data.headers['X-Rate-Limit-Reset'])
When running this code I get the error Cannot read property 'X-Rate-Limit-Limit' of undefined.
This is not a duplicate, I've also tried it lowercase as described here but had no luck either. Thanks for helping out!
EDIT:
The function takes the following parameters:
module.exports = {
updateRL: (lim, rem, res) {SAVING STUFF HERE}
}
It is defined in the file rates.jsand is imported in the above file as const rl = require('../rates').

Google+ insert moment with nodejs client

Has anyone been able to get the google-api-nodejs-client to successfully insert a moment?
Whatever I try, I get a generic 400 "Invalid value" error but am unable to narrow down the invalid value because the API Explorer doesn't work either.
Would it be because of the missing data-requestvisibleactions parameter? I'm using passport.js's require('passport-google-oauth').OAuth2Strategy for handling oauth access, and that part is working fine, but I have no idea how to incorporate requestvisibleactions into the oauth request flow since this is definitely not originating from a clientside form.
Here's a snippet of what I'm trying to do (using the latest version of googleapis, v1.0.2):
var google = require('googleapis')
var auth = new google.auth.OAuth2()
auth.setCredentials({
'access_token': user.token
})
google.plus('v1').moments.insert({
collection: 'vault',
userId: 'me',
debug: true,
resource: {
type: "http://schemas.google.com/AddActivity",
target: {
type: "http://schema.org/CreativeWork",
url: "...omitted...",
image: "...omitted...",
description: "test",
name: "test"
}
},
auth: auth
}, function (err, response) {
if (err) {
console.error(err)
res.send(err.code, err)
} else {
console.log(response)
res.send(200)
}
})
ref 1 (out-of-date w.r.t. an older version of googleapis)
ref 2 (client-side, where the use of data-requestvisibleactions is more obvious)
As you speculated, you need the request_visible_actions parameter as part of the URL calling the oauth endpoint.
It looks like the current version of passport-google-oauth doesn't support this parameter. Judging by several of the open issues and pull requests, it isn't clear that the author will respond to requests to add it either. You have two possible options:
Switch to using the OAuth support that is included in google-api-nodejs-client
Patch the passport-google-oauth code. (And possibly submit a pull request in the hopes it will be useful to someone else.)
I don't use passport.js or the passport module in question, so I can't test this, but based on the github repository, I think you can insert the following in lib/passport-google-oauth/oauth2.js after line 136 and before the return statement:
if (options.requestVisibleActions) {
// Space separated list of allowed app actions
// as documented at:
// https://developers.google.com/+/web/app-activities/#writing_an_app_activity_using_the_google_apis_client_libraries
// https://developers.google.com/+/api/moment-types/
params['request_visible_actions'] = options.requestVisibleActions;
}

Resources