Apify Discord Mirror

Updated 5 months ago

Pass a variable from pageFunction() via Webhooks JSON

At a glance

The community member is trying to pass a variable from a pageFunction to a webhook, but is unable to get the value. They have tried using {{resource.numberOfTests}} and {{numberOfTests}} in the webhook integration JSON, but one returns a validation error and the other doesn't have access to the data.

In the comments, another community member explains that the webhooks don't have access to the data, and the data needs to be loaded in the code called by the webhook. The community member asks for basic examples on how to do this.

Another community member suggests two options: create a new webhook in the interface and then pull the results with another API call, or attach a dynamic webhook at the end of the run and add the results there directly.

Im trying to pass a returned variable from pageFunctions via webhooks, but I cannot get the value.
My code:
async function pageFunction(context) {
await context.skipLinks();
const $ = context.jQuery;
const pageTitle = $('title').first().text();
const matches = pageTitle.match(/((\d+))/);
const numberOfTests = 0;

if (matches !== null) {
numberOfTests = parseInt(matches[1]);
}
context.log.info(${numberOfTests})

return {
numberOfTests
}
}

Webhooks integration JSON:
{
"tests": {{resource.numberOfTests}}
}

What am I doing wrong? I tried also:
{
"tests": {{numberOfTests}}
}
but this returns a validation error.
L
M
4 comments
The webhooks don't have access to the data, only to resource.defaultDatasetId. You have to load the data in the code called by the webhook
Thank you Could you please point me to some basic examples on how to do this?
Im just trying to scrape a website behind a login (that I've managed to acheive) to see if there is something available, and pass this boolean variable via a webhook to my Home Assistant
You have 2 options:
  1. create a new webhook in the interface, then the webhook sends you info about the run and you have to pull the results with another API call
  2. if you have your own actor, you can attach dynamic webhook at the end of the run and add the results thre directly
Add a reply
Sign up and join the conversation on Discord