Email Notification Webhook

  • mnmkng/email-notification-webhook
  • Modified
  • Users 39
  • Runs 35.6k
  • Created by Author's avatarOndra Urban

This actor sends you an email notification with a log file when one of your other actors fails, succeeds, times out, you name it.

Dockerfile

FROM apify/actor-node-basic
# Copy source code
COPY . ./
#COPY src ./src
RUN npm --quiet set progress=false \
&& npm install --only=prod --no-optional \
&& echo "Installed NPM packages:" \
&& npm list || true \
&& echo "Node.js version:" \
&& node --version \
&& echo "NPM version:" \
&& npm --version

main.js

const Apify = require('apify');

const { client, utils: { log } } = Apify;

Apify.main(async () => {
    log.info('Getting IDs of the calling actor and run.');
    let { 
        eventData: { actorId, actorRunId },
        resource: { status },
        notificationEmail,
    } = await Apify.getInput();

    log.info('Getting actor\'s identifier.');
    const { name, username } = await client.acts.getAct({ actId: actorId });
    const identifier = `${username}/${name}`;
    
    log.info(`Using run id: ${actorRunId} to download log file.`);
    const runLog = await client.logs.getLog({ logId: actorRunId });

    if (!notificationEmail) {
        log.info('Getting email of the calling user.');
        const { email } = await client.users.getUser();
        notificationEmail = email;
    }
    
    log.info(`Sending email to: ${notificationEmail}`);
    await Apify.metamorph('e643gqfZae2TfQEbA', { // ID of apify/send-mail
        to: notificationEmail,
        subject: `Apify actor ${identifier} ${status}`,
        html: `<p>Run <strong>${actorRunId}</strong> of the actor <strong>${identifier}</strong>`
         + ` (${actorId}) finished with status: <strong>${status}</strong>.</p>`
         + '<p>You can find the run\'s log in the attached file:</p>\n\n',
        attachments: [{
            filename: 'log.txt',
            data: Buffer.from(runLog).toString('base64'),
        }],
    }, { build: 'latest' });
});

package.json

{
    "name": "email-notification-webhook",
    "version": "0.0.1",
    "description": "This actor sends you an email notification with a log file when one of your other actors fails, succeeds, times out, you name it.",
    "main": "main.js",
    "dependencies": {
        "apify": "latest"
    },
    "scripts": {
        "start": "node main.js"
    },
    "author": "mnmkng"
}