Comparing version 0.1.7 to 0.2.0
#!/usr/bin/env node | ||
const path = require("path"); | ||
const DEFAULT_CONFIG_NAME = "simplert.cfg.json" | ||
const DEFAULT_CONFIG_NAME = "simplert.json"; | ||
let config_name = DEFAULT_CONFIG_NAME; | ||
const fs = require('fs'); | ||
const readline = require('readline').createInterface({ | ||
const fs = require("fs"); | ||
const readline = require("readline").createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}) | ||
}); | ||
readline.question(`Name your config file (${DEFAULT_CONFIG_NAME}) `, (filename) => { | ||
if(filename) config_name = filename; | ||
readline.question( | ||
`Name your config file (${DEFAULT_CONFIG_NAME}) `, | ||
filename => { | ||
if (filename) config_name = filename; | ||
fs.copyFile(path.resolve(__dirname + "/..") + `/${DEFAULT_CONFIG_NAME}`, config_name, fs.constants.COPYFILE_EXCL, (err) => { | ||
if (err) throw err; | ||
console.log(`${config_name} was successfully generated`); | ||
}); | ||
fs.copyFile( | ||
path.resolve(__dirname + "/..") + `/${DEFAULT_CONFIG_NAME}`, | ||
config_name, | ||
fs.constants.COPYFILE_EXCL, | ||
err => { | ||
if (err) throw err; | ||
console.log(`${config_name} was successfully generated`); | ||
} | ||
); | ||
readline.close() | ||
}) | ||
readline.close(); | ||
} | ||
); |
83
index.js
const path = require("path"); | ||
const Discord = require("discord.js"); | ||
const discord_client = new Discord.Client(); | ||
const { google } = require("googleapis"); | ||
let user_config = {}; | ||
let discordLoggedIn = false; | ||
let gmailAuth = ""; | ||
@@ -11,3 +13,3 @@ exports.configure = function(file) { | ||
user_config = require(path.resolve(`${__dirname}/../../${file}`)); | ||
template_config = require(__dirname + "/simplert.cfg.json"); | ||
template_config = require(__dirname + "/simplert.json"); | ||
@@ -19,6 +21,23 @@ user_keys = getAllKeys(user_config); | ||
throw "Your simplert config file is not up to date, use the generate command to get the most up to date version"; | ||
if (user_config.email.gmail.enabled) { | ||
const { | ||
client_secret, | ||
client_id, | ||
redirect_uris | ||
} = user_config.email.gmail.credentials.installed; | ||
const oAuth2Client = new google.auth.OAuth2( | ||
client_id, | ||
client_secret, | ||
redirect_uris[0] | ||
); | ||
oAuth2Client.setCredentials(user_config.email.gmail.token); | ||
gmailAuth = oAuth2Client; | ||
} | ||
} catch (e) { | ||
if (e.code === "MODULE_NOT_FOUND") { | ||
throw new Error( | ||
"Could not file specified. Make sure file path is correct." | ||
`Could not file specified. Make sure file path is correct. ${path.resolve( | ||
__dirname + "/../../" + file | ||
)}` | ||
); | ||
@@ -70,2 +89,62 @@ } else { | ||
exports.email = function( | ||
body, | ||
send_to = user_config.email.gmail.send_to, | ||
subject = user_config.email.gmail.subject, | ||
send_from = user_config.email.gmail.send_from | ||
) { | ||
return new Promise(async (resolve, reject) => { | ||
let error; | ||
if (!user_config.email.gmail.enabled) | ||
error = | ||
"Gmail alert is not enabled. You need to enable it in your simplert config file"; | ||
else if (!body) | ||
error = | ||
".email() requires at least 1 argument. Pass it a message to send in the email body"; | ||
else if (!send_to) | ||
error = | ||
"You need to specify a receipient email either in your simplert config file or passed into .email()"; | ||
else if (!subject) | ||
error = | ||
"You need to specify an email subject either in your simplert config file or passed into .email()"; | ||
else if (!send_from) | ||
error = | ||
"You need to specify a sender email either in your simplert config file or passed into .email()"; | ||
if (error) reject(new Error(error)); | ||
const gmail = google.gmail({ version: "v1", auth: gmailAuth }); | ||
const utf8Subject = `=?utf-8?B?${Buffer.from(subject).toString( | ||
"base64" | ||
)}?=`; | ||
const messageParts = [ | ||
`From: ${send_from}`, | ||
`To: ${send_to}`, | ||
"Content-Type: text/html; charset=utf-8", | ||
"MIME-Version: 1.0", | ||
`Subject: ${utf8Subject}`, | ||
"", | ||
`${body}` | ||
]; | ||
const message = messageParts.join("\n"); | ||
const encodedMessage = Buffer.from(message) | ||
.toString("base64") | ||
.replace(/\+/g, "-") | ||
.replace(/\//g, "_") | ||
.replace(/=+$/, ""); | ||
const email = await gmail.users.messages.send({ | ||
userId: "me", | ||
requestBody: { | ||
raw: encodedMessage | ||
} | ||
}); | ||
resolve(email); | ||
}); | ||
}; | ||
function getAllKeys(data) { | ||
@@ -72,0 +151,0 @@ const parent_keys = Object.keys(data); |
{ | ||
"name": "simplert", | ||
"version": "0.1.7", | ||
"version": "0.2.0", | ||
"description": "Simple way to send alerts via Slack, Email, Discord, SMS, etc.", | ||
@@ -8,9 +8,34 @@ "main": "index.js", | ||
"bin": { | ||
"generate-config": "./bin/generate-config.js" | ||
"generate-config": "./bin/generate-config.js", | ||
"generate-gmail": "./bin/generate-gmail-creds.js" | ||
}, | ||
"author": "Nicholas Dangles", | ||
"license": "ISC", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/ndangles/simplert.git" | ||
}, | ||
"keywords": [ | ||
"simple alerts", | ||
"simple events", | ||
"simplert", | ||
"alerts", | ||
"events", | ||
"discord alerts", | ||
"gmail alerts", | ||
"gmail", | ||
"send alert", | ||
"easy alerts", | ||
"send email", | ||
"discord" | ||
], | ||
"bugs": { | ||
"url": "https://github.com/ndangles/simplert/issues" | ||
}, | ||
"readme": "README.md", | ||
"homepage": "https://github.com/ndangles/simplert#readme", | ||
"dependencies": { | ||
"discord.js": "^11.5.1" | ||
"discord.js": "^11.5.1", | ||
"googleapis": "^45.0.0" | ||
} | ||
} |
# Simplert | ||
Simple way to send alerts via Slack, Email, Discord, SMS, etc. Simplert is currently geared towards personal and smaller apps where you just want a simple way to alert on some event like when a new user signs up. I would advise against using this in any critical production apps as of now. | ||
Simple way to send alerts via Slack, Email, Discord, SMS, etc. Simplert is currently geared towards personal and smaller apps where you just want a simple way to alert on some event like when a new user signs up. The idea is that you can just drop your simplert config file in any of your projects and quickly starting alerting on events. I would advise against using this in any critical production apps as of now. | ||
@@ -14,43 +14,67 @@ ## Getting Started | ||
"send_to": "" | ||
}, | ||
"email": { | ||
"gmail": { | ||
"enabled": false, | ||
"send_to": "", | ||
"send_from": "", | ||
"subject": "", | ||
"token": {}, | ||
"credentials": {} | ||
} | ||
} | ||
} | ||
***enabled*** - whether you want this type of alerting enabled or not | ||
***token*** - the token of your discord bot | ||
***send_to*** - the name of the discord channel you want to send your alerts too. Make sure your bot has permission on the server to send messages | ||
***enabled*** - whether you want this type of alerting enabled or not | ||
***discord:token*** - the token of your discord bot | ||
***discord:send_to*** - the name of the discord channel you want to send your alerts too. Make sure your bot has permission on the server to send messages | ||
***email:gmail:send_to*** - the default receipient email you want to send to, otherwise passed into the email function | ||
***email:gmail:send_from*** - The default email you want to send from. You may have a Gsuite account with multiple aliases that you can specifiy here which one you want to send from. Otherwise passed into the email function | ||
***email:gmail:subject*** - A default email subject for every alert, otherwise passed into the email function | ||
***email:gmail:token*** - Used for authorization, can be generated with `npx simplert generate-gmail` assuming you have your `email:gmail:credentials` populated correctly. | ||
***email:gmail:credentials*** - Credentials provided by Google when setting up your project in the [Google Cloud Console](https://console.cloud.google.com/) usually downloaded as `gmail-credentials.json`. Set this key as the contents of that file. | ||
## Usage | ||
*Note: Make sure you have setup your configuration file* | ||
*Note: Make sure you have setup your configuration file* | ||
### Web Server | ||
const express = require("express") | ||
const app = express(); | ||
--- | ||
#### **simplert.discord(message,[send_to])** | ||
Send a message to a discord channel. | ||
`send_to` can be optional if it is specified in the configuration file under`discord:send_to` as the default, otherwise it needs to be passed in as the second argument | ||
const simplert = require("simplert"); | ||
simplert.configure("simplert.cfg.json") | ||
app.get("/test", async (req, res) => { | ||
simplert.discord("server test") | ||
res.send("alert sent") | ||
}) | ||
simplert.configure("simplert.json"); | ||
app.listen(8849, "127.0.0.1", (req, res) => { | ||
console.log("listening at localhost:8849") | ||
}) | ||
simplert.discord("some event"); //assuming discord:send_to is set in config file | ||
simplert.discord("some other event", "general"); // send to a different channel | ||
--- | ||
#### **simplert.email(body,[send_to, subject, send_from])** | ||
Send an email message | ||
`send_to, subject, send_from` can be optional if defaults are set under `email:gmail:*` in the configuration file. Otherwise, they need to be passed as arguments to the function. | ||
const simplert = require("simplert"); | ||
simplert.configure("simplert.json"); | ||
simplert.email("some event"); //assuming discord:send_to is set in config file | ||
simplert.email("some other event", "email@example.com"); // send to a specific email | ||
simplert.email("another event", "email@example.com", "Some Email Subject"); // set an email subject | ||
simplert.email("and another event", "email@example.com", "Some Email Subject", "alias@example.com"); // set a send from alias if you use Gsuite | ||
### Script | ||
const simplert = require("simplert"); | ||
simplert.configure("simplert.cfg.json") | ||
## Scripts | ||
#### **generate-config** | ||
`npx simplert generate-config` | ||
Generates a simplert configuration file. By default, all alert will be set to disabled, you will need to manually configure the necessary values in this file before using simplert. | ||
#### **generate-gmail** | ||
`npx simplert generate-gmail` | ||
This will generate an auth token and automatically populate the `email:gmail:token` value in your configuration file. This assumes that you have `email:gmail:credentials` set in your configuration file which should be set to the content in your `gmail-credentials.json` file that was download from Google Cloud Console when setting up a project. | ||
async function run() { | ||
await simplert.discord("some event") | ||
await simplert.discord("some other event") | ||
} | ||
run(); | ||
## Supported Alerts | ||
- #### [Discord](https://discordapp.com/) | ||
*Email, Slack, SMS coming eventually* | ||
#### [Discord](https://discordapp.com/) | ||
#### [Gmail](https://mail.google.com) | ||
*Slack and SMS coming eventually* | ||
@@ -60,1 +84,2 @@ ## Bugs and Improvements | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
12354
6
224
0
1
84
2
5
1
+ Addedgoogleapis@^45.0.0
+ Addedabort-controller@3.0.0(transitive)
+ Addedagent-base@6.0.2(transitive)
+ Addedarrify@2.0.1(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbignumber.js@9.1.2(transitive)
+ Addedbuffer-equal-constant-time@1.0.1(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addedecdsa-sig-formatter@1.0.11(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedevent-target-shim@5.0.1(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedfast-text-encoding@1.0.6(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedgaxios@2.3.4(transitive)
+ Addedgcp-metadata@3.5.0(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgoogle-auth-library@5.10.1(transitive)
+ Addedgoogle-p12-pem@2.0.5(transitive)
+ Addedgoogleapis@45.0.0(transitive)
+ Addedgoogleapis-common@3.2.2(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedgtoken@4.1.4(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhttps-proxy-agent@5.0.1(transitive)
+ Addedis-stream@2.0.1(transitive)
+ Addedjson-bigint@0.3.1(transitive)
+ Addedjwa@2.0.0(transitive)
+ Addedjws@4.0.0(transitive)
+ Addedlru-cache@5.1.1(transitive)
+ Addedmime@2.6.0(transitive)
+ Addedms@2.1.3(transitive)
+ Addednode-fetch@2.7.0(transitive)
+ Addednode-forge@0.10.0(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedqs@6.13.1(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedside-channel@1.0.6(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addedurl-template@2.0.8(transitive)
+ Addeduuid@7.0.3(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-url@5.0.0(transitive)
+ Addedyallist@3.1.1(transitive)