Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 28, 2022 10:14 am GMT

Free your mind and start creating useful automations with Znote

Originally posted on my blog

Are you more automatic or manual?

Hi all ,
Continuing my contribution to the developer community as app maker, today I propose to discuss how to effectively automate tasks.

So... How many repetitive manual actions do you do each day? Probably a lot!

And why...? Because automating your scattered actions is long and difficult. Don't have time to build and automate a complete process? Neither do I!

Let me highlight an easy way to quickly automate actions without headache with Znote.

The approach is: Incremental Automation.
Write your process down in a note and start coding parts of your tasks. Thus, you can alternate between manual and automated actions.

I don't know about you, but personally I'm more comfortable learning by testing and prototyping. For me, the best way to achieve this was to use a Javascript playground using Markdown.

Concept

I adapted Znote to do like Jupyter did with Python, but transposed to the JS world. Plus a super smooth integration between Node and the JS client as well as the entire ecosystem of NPM libraries, for easily viewing code, results, and taking notes between code blocks.

A note architecture image - code blocks with markdown

Thanks to this architecture, I am able to gradually and painlessly automate an entire process. Manipulate data across multiple applications.
You can smoothly alternate manual and automatic actions, store the results and explain the actions to be taken, and push your automation as far as you want.

Back to our problem

Let's now see with a concrete example how to proceed to build a ready-to-use automation. The best way to do this is to start solving your own problems! So let me expose our first use case.

Our use case

When I have new registered users, I had to regularly

  • connect to my remote database with SSH,
  • manually extract the users with an SQL query,
  • copy the results locally and compare with the existing users.
  • Once the new users were identified,
  • I sent a welcome email to each new member to get their feedback.

SSH - mail new users diagram

Make an SQL query on a remote database

1 - Make an SQL query

async function getSQL(sqlQuery, host, port, user, password, database) { const { Sequelize } = require('sequelize'); const sequelize = new Sequelize(user, password, database, {    dialect: "mysql"/* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */,    host: host,    port: port }); sequelize.authenticate(); const [results, metadata] = await sequelize.query(sqlQuery); sequelize.close() return results;}

2 - Make an SQL query with SSH

async function getRemoteSQL(sqlQuery) {  const { spawn } = require('child_process');  // connect to server (don't forget to copy your ssh-key on the remote server)  const ssh = spawn('ssh', [   '-L', '3307:localhost:3306',   '[email protected]',  ]);  await sleep(4000); // wait connection  // make SQL query (replace with your credentials)  const data = await getSQL(sqlQuery, "localhost", 3307, "USER", "PASSWORD", "DATABASE");  // quit  ssh.kill();  return data;}

You can now just do anywhere in my notes an SQL query against your remote database

const users = await getRemoteSQL("select purchase_email from license;");printJSON(users)

Identify new users

Then, we want to identify all new users since the query was last run.

function getNewUsers(incomingData, filename, key) {  const fs = require('fs');  let refData = [];  try {    refData = JSON.parse(fs.readFileSync(filename, 'utf8'))      .map(element => key(element));  } catch (err){}  const delta = [];   incomingData.forEach(obj => {    if (refData.indexOf(key(obj))===-1) {      delta.push(obj);    }  });  printJSON(delta);  return delta;}

Send mail

Finally, we can send a mail to each new user. With Google, you need to generate a dedicated credential: https://support.google.com/accounts/answer/185833

function sendMail(recipients) {  const nodemailer = require('nodemailer');  const transporter = nodemailer.createTransport({    service: 'gmail',    auth: {      user: '[email protected]', // your gmail      pass: 'XXX', // app credential here   },  });  const result = await transporter.sendMail({   from: '"Anthony" <[email protected]>', // sender address    to: recipients, // list of receivers (,)    subject: "Just to know if everything is fine ", // Subject line    text: "Hi , Being very concerned about the quality of Znote app, I thank you for any feedback you could give me, allowing me to improve my software!",   // plain text body    html: "Being very concerned about the <b>quality</b> of Znote app, I thank you for any feedback you could give me, allowing me to improve my software!",   // html body  });  printJSON(result);}

Plug everything in

It remains to describe our process by calling the different functions.

// get all users in remote databaseconst allUsers = await getRemoteSQL("select purchase_email from license;");// show new users since last execution timeconst newUsers = getNewUsers(allUsers, '/Users/alagrede/Desktop/users.json', it => it.purchase_email);// write all users in filefs.writeFileSync('/Users/alagrede/Desktop/users.json', JSON.stringify(allUsers));// send mailsendMail(newUsers.map(e=>e.purchase_email));

Tada!!

Gmail screenshot

To summarize

You now have a bunch of reusable functions and an automated process. Since functions can be reused on other notes, you can capitalize on your existing work and continue to create additional processes. Then export your script, ready to run it in a cron.

Thanks for reading ! There are a ton of other ways to do it better, but I hope this example inspired you.

Go further

I will continue to add some examples when you need them.

Meanwhile, start turning your daily tasks into something easy and repeatable and your job will be more fun.

Don't forget to check our documentation website.

If you haven't downloaded Znote yet, it's download this app here

znote screenshot


Original Link: https://dev.to/alagrede/free-your-mind-and-start-creating-useful-automations-with-znote-27pc

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To