Act

jancurn/send-email-on-crawler-finish

  • Builds
  • latest 0.0.10 / 2018-06-12
  • Created 2018-04-18
  • Last modified 2018-06-12
  • grade 1

Description

Fetches information about a crawler run and sends it to the user by email. For example, this actor can be used to inform the user that the crawler run finished. To do that, simply put the following URL into "Finish webhook URL" setting of your crawler: https://api.apify.com/v2/acts/jancurn~send-email-on-crawler-finish/runs?token=<YOUR_API_TOKEN> That's it, it does not require any other configuration.


API

To run the act, send a HTTP POST request to:

https://api.apify.com/v2/acts/jancurn~send-email-on-crawler-finish/runs?token=<YOUR_API_TOKEN>

The POST payload will be passed as input for the act. For more information, read the docs.


Example input

Content type: application/json; charset=utf-8

{ "_id": "MY_CRAWLER_EXECUTION_ID" }

Source code

Based on the apify/actor-node-basic Docker image (see docs).

const Apify = require('apify');

Apify.main(async () => {
      
    // Get act input and validate it
    const input = await Apify.getValue('INPUT');
    console.log('Input:')
    console.dir(input);
    if (!input || !input._id) {
        throw new Error('Input is missing the "_id" attribute. Did you start it from the crawler finish webhook?');
    }
    const executionId = input._id;
    
    // Print info about crawler run
    const crawlerRunDetails = await Apify.client.crawlers.getExecutionDetails({ executionId });
    if (!crawlerRunDetails) {
        throw new Error(`There is no crawler run with ID: "${executionId}"`);
    }
    console.log(`Details of the crawler run (ID: ${executionId}):`);
    console.dir(crawlerRunDetails);
    
    console.log('Fetching user\'s email');
    const user = await Apify.client.users.getUser();
    
    // Send email
    console.log('Sending email...');
    await Apify.call('apify/send-mail', {
      "to": user.email,
      "subject": `Crawler run ${executionId} finished`,
      "text": `Info about crawler run:\n\n${JSON.stringify(crawlerRunDetails,null,2)}`
    });
    
    console.log('Done.');
});