1const Apify = require('apify');
2
3const { client, utils: { log } } = Apify;
4
5Apify.main(async () => {
6 log.info('Getting IDs of the calling actor and run.');
7 let {
8 eventData: { actorId, actorRunId },
9 resource: { status },
10 notificationEmail,
11 } = await Apify.getInput();
12
13 log.info('Getting actor\'s identifier.');
14 const { name, username } = await client.acts.getAct({ actId: actorId });
15 const identifier = `${username}/${name}`;
16
17 log.info(`Using run id: ${actorRunId} to download log file.`);
18 const runLog = await client.logs.getLog({ logId: actorRunId });
19
20 if (!notificationEmail) {
21 log.info('Getting email of the calling user.');
22 const { email } = await client.users.getUser();
23 notificationEmail = email;
24 }
25
26 log.info(`Sending email to: ${notificationEmail}`);
27 await Apify.metamorph('e643gqfZae2TfQEbA', {
28 to: notificationEmail,
29 subject: `Apify actor ${identifier} ${status}`,
30 html: `<p>Run <strong>${actorRunId}</strong> of the actor <strong>${identifier}</strong>`
31 + ` (${actorId}) finished with status: <strong>${status}</strong>.</p>`
32 + '<p>You can find the run\'s log in the attached file:</p>\n\n',
33 attachments: [{
34 filename: 'log.txt',
35 data: Buffer.from(runLog).toString('base64'),
36 }],
37 }, { build: 'latest' });
38});