Actor

vaclavrut/executionid-to-xls-and-send

  • Builds
  • latest 0.0.79 / 2017-10-09
  • Created 2017-09-20
  • Last modified 2017-10-09
  • grade 2

Description

The act takes a JSON input, starts the act which creates a xls from execution id result and after that send the link via mail. The resulting XLSX URL will be following: https://api.apifier.com/v2/key-value-stores/{defaultStoreID}/records/results.xlsx?rawBody=1&disableRedirect=1 Alternatively you can obtain the result from default OUTPUT value where it is stored like this: {output: "result XLSX URL"} The URL is also outputted in the log. Solving how to attach the xls to the mail.


API

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

https://api.apify.com/v2/acts/vaclavrut~executionid-to-xls-and-send/runs?token=<YOUR_API_TOKEN>

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


Example input

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

[{ "hello": 123 }, { "hello": 345 }, { "hello": 567 }]

Source code

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

const XLSX = require('xlsx');
const Apify = require('apify');
const _ = require('underscore');
const Promise = require('bluebird');
const request = require('request-promise');

Apify.main(async () => {
    Apify.setPromisesDependency(Promise);
    console.log("Start");
    const attachments = [];
    
    // get Act input and validate it
    let input = await Apify.getValue('INPUT');
    let finishWebhookData;
    console.log(JSON.stringify(input));
    if(input.data){
        //run from webhook
        finishWebhookData = JSON.parse(input.data);
        finishWebhookData._id = input._id
    }else if(input){
        //manual run from console
        finishWebhookData = input;
    }else{
         throw new Error('Input is missing!');
    }

    console.log("ID is -> "+ finishWebhookData._id)
    console.log("Create XLS from json.");
    
    const run = await Apify.call(
    	'petr_cermak/execution-to-xlsx',
    	{"_id": finishWebhookData._id});
    
   
    const xlsUrl = run.output.body.output;
    console.log(JSON.stringify(run));
    
    const xlsSize = parseFloat(run.output.size);
    console.log("We have XLS url: "+xlsUrl);
    console.log("Size of XLS is: "+xlsSize);
    const runMail = await Apify.call(
    	'apify/send-mail',
        {to: finishWebhookData.reciever,
        subject: finishWebhookData.subjectText,
        text: "Hello, here is the link for XLS result: "+ xlsUrl,
        attachments: attachments
        },
        {});
    
    
    /*
    //Next step to attach the file if the size is ok
    if(xlsSize < 10000000){
      console.log("Creating attachement, size is ok.")
      var attachement = await request({ url: xlsUrl, encoding: null });
      attachments.push({
            filename: `data.xls`,
            data: attachement,
        });
      }else{
        console.log("We cant send it as attachement, file is too big.")    
      }
      */
      /*
    console.log("Now we can send the mail.");
    await Apify.setValue('OUTPUT', {output: xlsUrl});
    const runMail = await Apify.call({
        actId: 'apify/send-mail',
        input: {
            contentType: 'application/json; charset=utf-8',
            body: JSON.stringify({
                to: finishWebhookData.reciever,
                subject: finishWebhookData.subjectText,
                text: "Hello, here is the link for XLS result: "+ xlsUrl,
                attachments: attachments
            })
          
        }
    });
    console.log("Mail sent, done.")
    */
});