Jokes from API in Excel send in Email and all this by Integray

Hello,
here is my solution:

I use this two APIs
https://api.chucknorris.io/jokes/categories (list of exist categories )
https://api.chucknorris.io/jokes/random?category=something (one Chuck Norris joke selected by category)

First step is Node.js processor with output String.

Here is the code, I use fetch function:

const response1 = await fetch("https://api.chucknorris.io/jokes/categories", {
  method: "get",
}).then(response => {
  return response.json()
});

let response2 = response1.map(function (x) {
  return {
   "Text": x
  }
});

return response2;

Second step is Node.js processor with input and output String, Input processing
is Bulk

maximum = inputData.length - 1 
//number of rows = number of jokes in Excel
//here we select three pseudo-random categories and save them to the data schema
return [  
    {"Text": inputData[Math.floor(Math.random() * maximum)].Text},
    {"Text": inputData[Math.floor(Math.random() * maximum)].Text},
    {"Text": inputData[Math.floor(Math.random() * maximum)].Text}
];

Third step is Node.js processor with input String and output JSON, Input processing
is Row by row

//here we call API with three pseudo-random categories from the previous step 
//and save them to the new JSON data schema
const response1 = await fetch("https://api.chucknorris.io/jokes/random?category="+inputData[0].category, {
  method: "get",
}).then(response => {
  return response.json()
});

return [
  {
    Json1: response1,
  }
];

Fourth step is JSON Parser with input JSON and output String columns with parse JSON

Fifth step is Excel Serializer with input String columns and output Excel Serializer Output (1.0.0), Input processing is Bulk
Here I simply assign the columns from the data schema correctly

Sixth step is JS Mapper with input Excel Serializer Output (1.0.0) and output E-Mail Send (1.0.1)
Simple code is:

return [
  {
  	"Subject": "Jokes with Chuck Norris theme",
    "From": [{
      "Name": "Community integray",
      "Address": "something@mail.com"
    }],
    "To": [{
      "Name": "Receiver Alex Green",
      "Address": "something2@mail.com"
    }],
    "BodyType": "html",
    "Body": "<center><h2><p><a href='https://integray.com/'>via integray</a></p></h2></center>",
    "Attachment": [{
      "Name": inputData[0].Filename,
      "Content": inputData[0].Data
    }]
  }
];

Seventh step is Seznam SMTP Sender Connector
Here we enter the login data for the mail that will send the mail

Modifications: Of course, you can modify the task, not draw categories and send only one random mail by this API: https://api.chucknorris.io/jokes/random
If you try it, definitely write about it on the forum, we are curious about your solution.

Thanks for your attention :slight_smile:

3 Likes

Not every integration has to be useful… But that’s what makes it better! :wink:

1 Like

Great example how easily get JSON data, serialize it to excel and send an email with a such great tool which Intergay is and all is done in few minutes!!!

1 Like

Optimization for Second step with loop and function :slight_smile:


let jokeCat = []; //target field definition

maximum = inputData.length - 1;

//function which generate random category
function getRandomCategory(x) {
    return inputData[Math.floor(Math.random() * x)].Text
};

//number of rows = number of jokes in Excel
//here we select three pseudo-random categories and save them to the data schema
for (let a = 0; a <= 2; a++) { //loop fills the field with values
    let obj = {
        "Text": getRandomCategory(maximum)
    };
    jokeCat.push(obj);
}

return jokeCat;