Send an triggered Email using Node.Js (From Analysis)

Send an triggered Email using Node.Js (From Analysis)

@Brugnetti brugnetti

Based on your example, I try to send an email from Analysis.
The code is :

/*
 ** Analysis based on Tago Example
 ** Sending dynamic notification
 **
 ** Send Email notifications using analysis.
 **
 */
const { Analysis, Account, Utils, Services } = require('@tago-io/sdk');

async function init(context, scope) {
  if (!scope[0]) return context.log('This analysis must be triggered by an action.');

  context.log('Analysis started');
  // Get the environment variables.
  const environment_variables = Utils.envToJson(context.environment);
  if (!environment_variables.account_token) return context.log('Missing "account_token" environment variable');
  else if (environment_variables.account_token.length !== 36) return context.log('Invalid "account_token" in the environment variable');

  // Instance the Account class
  const account = new Account({ token: environment_variables.account_token });

  // Get the device ID from the scope and retrieve device information.
  const device_id = scope[0].origin;
  const device_info = await account.devices.info(device_id);

  // Get the device name and tags from the device.
  // [TAG KEY]    [TAG VALUE]
  // email        example@tago.io
  //
  // This is just a generic example how to get this information. You can get data from a device, search in tags, or any other way of correlation you have.
  // For example, you can get the email directly from the user_id if it was specified:
  // const { email } = await account.run.userInfo(userID_tag.id);
  const device_name = device_info.name;
  const email_tag = device_info.tags.find(tag => tag.key === 'email');

  // Instance the SMS and Email service usando the analysis token from the context.
  const email_service = new Services({ token: context.token }).email;

  // Send the notifications and output the results to the analysis console.
  if (email_tag) {
    await email_service.send({
      to: email_tag.value,
      subject: 'Notification alert',
      message: `You received a notification for the device: ${device_name}. Variable: ${scope[0].variable}, Value: ${scope[0].value}`,
    }).then(context.log).catch(context.log);
  } else {
    context.log('Email not found for this device.');
  }

  context.log('Script end.');
}

module.exports = new Analysis(init);

Whe running, I obtain these error messages :

[2022-01-05 10:45:01] Analysis finished.
[2022-01-05 10:45:01] Duration: 1.3s Billed Duration: 2s.
[2022-01-05 10:45:01] Email not found for this device.
[2022-01-05 10:45:01] Script end.
[2022-01-05 10:45:01] > TagoIO-SDK: No region or env defined, using fallback as usa-1.```

Could you help me with this problem ?

Thanks