How to check api response on regular interval after hitting it once - node.js

I am creating NestJs application where I am hitting to some third party api form where I am getting resposne some like this:
{
"success": true,
"data": {
"status": "PENDING"
}
}
In this resposne status has two values ALLOWED or PENDING.
Now when I am hitting api for the very first time its shows above response where status is pending.Now I don't want to hit api again and again manually to check whether status is allowed or not.
Is there any way so that it can automatically check whether status in response is ALLOWED or not.

Given that is a third party API over which you have no control, unless they expose some sort of persistent socket connection, the short answer is NO.
At the moment, it seems like your only choice is to poll the API at a certain interval and check.
EDIT: If it is not required to send a response when status changes to ALLOWED, then you can perform CRON jobs as well, but the basic concept remains the same regardless, you're gonna have to hit the API at a certain interval to get updated values.

Related

Twilio Rest API / Node Js call status no working

I'm creating a tool that can automatically call someone when I click a button.
So far, I've tested three solutions: The first one was using the rest API call, the second one was using node js SDK that is available in the Twilio console using the function & assets feature, and the last one was using Twilio Studio feature.
All of them are not giving me the satisfaction of creating something that can be used in production.
The main problem is the call status given to the StatusCallback url parameter.
For example:
if you set a timeout value by default or something higher than the duration of the ringing time, it will send you a completed status. The timeout value needs to be smaller than the maximum number of ringtone that your carrier set by default if you want to receive a no-answered status.
If your phone is on "airplane mode" and you make a call to it using Twilio, the statusCallback will be sent to the URL as "completed".
If you make the same phone call and imagine that the user rejects the call by clicking on the red phone instead of the green phone, the statusCallback will send you a completed status.
So it's been a week since I've coded a NodeJS solution using Twilio Functions, making POST requests using Postman, and also creating a flow to test it using Twilio Studio. Every time that I've tried those three solutions, with the three same scenarios described above, I've never been able to obtain a real valuable/informative status that can help me to resend the call until someone answers or, based on the status, send an SMS to the person to inform him that we have tried to call him and that he either was not available or rejected the call.
Has one of you already tested that and maybe succeeded in having anything else than a "completed" status? That is no use in case you need to handle those cases.
Thank you for your help.

Response to the Source from Zap with Webhook Trigger

When I have a zap of more than 1 step, and the trigger is Webhooks by Zapier with Catch Hook. When I send the response to the webhook, it provides back to the source this kind of payload response:
{"id": "548299ef-bb3a-4769-885a-fb001407c049", "request_id": "5f197542-b118-4fe9-9b45-f1e10b4ac710", "attempt": "5f197542-b118-4fe9-9b45-f1e10b4ac710", "status": "success"}
Is there a way that the connection of the Zap is kept alive and rather than providing the response up, it provides the response of the last step of the Zap?
Short answer: no, it's not possible. You'll always get the json payload you posted when you send a webhook (unless you disable it, but then you just get nothing).
Long answer: No, because the zap execution happens separately from the webhook ingestion. Think of this operation less like a webserver (where the caller is waiting for a response) and more asynchronously (where the caller asks that a thing be done, the server confirms it will do it, and then the thing happens eventually).
What you want to use here is Make, as they support webhook responses.
Check out https://www.make.com/en/help/tools/webhooks
No affiliation, I just assumed Zapier could do this (and am slightly annoyed they can't).

Microsoft graph, batch request's nextLink

I'm currently implementing a sync queue service to sync a webapp's customers to Outlook's contacts.
I'm using the Graph API for the job. The creation and updating of contacts is done using graph's batch request.
There's a part in the docs about the response that I don't fully understand and pretty much ignored. I just want to make sure my implementation is correct.
In addition to the responses property, there might be a nextLink
property in the batch response. This allows Microsoft Graph to return
a batch response as soon as any of the individual requests has
completed. To ensure that all individual responses have been received,
continue to follow the nextLink as long as it exists.
I was wondering about the following:
when does nextLink show up? I've tried sending different requests but never received it. It's not really clear from the docs but my guess is that it appears when for some reason some of the requests in the batch did not complete in time?
Would the pending requests show up as errors in the response or would they just be missing from it?
Will the nextLink be in form of #odata.nextLink like in pagination requests? It does not specify that in the docs.
How should I handle it when/if it does appear? Can I safely ignore it and just count on the next invocation of service (every 15mins) to retry and sync the pending requests?
The paging mechanism mostly applies when you are querying Graph for data.
The nextLink shows up if whatever query you had as part of one of your batch requests requires pagination (just as if you ran the request directly). For example this request as part of your batch job would cause one to appear, provided the targeted user has more than 10 folders:
{
"id":"1",
"method":"GET",
"url":"users/user#domain.tld/mailFolders"
}
The response shows up as normal (with the first page of data included in the response body, along with the nextLink to get to the next page).
Correct. In the above example, the nextLink shows up like this:
"#odata.nextLink":"https://graph.microsoft.com/beta/users/user#domain.tld/mailFolders?$skip=10
You will need to follow the nextLink to get the rest of the data.

Make Web Activity work with response that's not a proper Json

I'm using Web Activity to make a POST request to a slack webhook.
The problem is that the slack webhook responds with a 200 and an "ok" string as the body of the response and the web activity doesn't seems to like that because I'm getting the error:
{
"errorCode": "2108",
"message": "Response Content is not a valid JObject",
"failureType": "UserError",
"target": "activityName"
}
So my question is if there is a workaround for this or is there is an alternative to use web activity. For the time being I know I can use a custom activity and even I'm thinking of setting up an azure function that acts as a proxy and thus returns a proper Json.
Note: I'm using this slack integration to alert when something goes wrong. Of course I'm also open to suggestions on how to alert my team when something is not working in ADF (sending an email wouldn't work because our inboxes are completely cluttered, so slack seemed like a good alternative for a change).
You can use a lookup activity with an HTTP dataset. You can define the dataset to be a REST call, and I don't think the lookup activity is picky about the response format.
Another option might be to use an Azure Logic App to update Slack. You can either use it as a 'proxy' to be called by ADF, or see if it can be set up to perform the monitoring you want as well.
According to the official doc, REST endpoints that the web activity invokes must return a response of type JSON. The activity will timeout at 1 minute with an error if it does not receive a response from the endpoint.
So currently I think your workaround should be the right way.
And besides of email, I think ADF also provides other approaches to monitor your data factory. Please find this doc for more details.

Caching response for API.ai Node.js webhook

I have a webhook designed in Node.js for API.ai that interacts with multiple API's to gather information and give response to user.
Since, I am interacting with multiple API's the response time taken is more than 5 secs which is causing the API.ai request to timeout.
To overcome this, I am trying to implement caching into the node.js webhook which saves the response from API's until a certain amount of time. This will remove timeout until the max-age header time is reached.
Edit: What is the best node module that I can use to cache the API responses for subsequest requests.
Note: I am using request node module for http requests but it doesnt seem to provide a way to cache the response.
All of the answers given are reasonable for tackling the cache problem on the request side. But since you specified API.AI and Actions, you might also be able to, or need to, store information while the conversation is in progress. You can do this using an API.AI context.
It may even be that if you limit it to just one remote call for each response from the user, you might be able to fit it in the timeframe.
For example, if you were having a conversation about movie times and ticket ordering, the conversation may go something like:
User: "I want to see a movie."
[You use an API to lookup the nearest theater, store the theater's location in a context and reply] "Your nearest theater is the Mall Megaplex. Are you interested in one there?"
User: "Sure"
[You now already have the theater, so you query for what it is playing with another API call and store it in a context] "There are seven different movies playing, including Star Wars and Jaws. Do those sound interesting?"
User: "No"
[You already have the data in the context, so you don't need another call.] "How about Rocky or..."
In this way you're making the same number of calls (generally), but storing the user's results in the session as you go as opposed to collecting all the information for the user, or all the possible results, and then narrowing them.
Finally decided to use the below module:
https://www.npmjs.com/package/memory-cache
This served my scenario better. Might try using Redis soon when i get some time.

Resources