Actor picture

Email Notification Webhook

mnmkng/email-notification-webhook

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

Author's avatarOndra Urban
  • Modified
  • Users31
  • Runs21,638
Actor picture

Email Notification Webhook

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

README.md

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

## How to use
Choose the actor you'd like to get notifications for and simply
create a webhook for that actor and copy the following URL into
the webhook's URL field, replacing `{YOUR_API_TOKEN}` with your token.

```
https://api.apify.com/v2/acts/mnmkng~email-notification-webhook/runs?token={YOUR_API_TOKEN}
```

Select the desired event types and save the webhook. You should now receive
an email notification including a log file whenever the selected events take place.
The email will be sent to the email associated with your account by default.
If you wish to send the notification to a different email, update the default webhook
payload template with the desired `notificationEmail`:

```
{
    "notificationEmail": "my.notifications@email.com"
    "userId": {{userId}},
    "createdAt": {{createdAt}},
    "eventType": {{eventType}},
    "eventData": {{eventData}},
    "resource": {{resource}}
}
```

## Why is it useful
It's great to know immediately when something goes wrong with one of your actors.
I suggest setting up the webhook for the `ACTOR.RUN.FAILED` and `ACTOR.RUN.TIMED_OUT`
events. This way, you'll always know right away and you can even check the
log file immediately to see if the failure requires your attention.

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"
}