jarvis-md
Version:
A simple WhatsApp Bot
87 lines (80 loc) • 2 kB
JavaScript
const axios = require('axios');
const fs = require('fs');
const path = require('path');
async function getBuffer(url, options) {
try {
options = options || {};
const res = await axios({
method: "get",
url,
headers: {
DNT: 1,
"Upgrade-Insecure-Request": 1,
},
...options,
responseType: "arraybuffer",
});
return res.data;
} catch (e) {
console.log(`Error : ${e}`);
throw e;
}
}
async function getJson(url, options) {
try {
options = options || {};
const res = await axios({
method: "GET",
url: url,
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
},
...options,
});
return res.data;
} catch (err) {
console.log(`Error : ${err}`);
throw err;
}
}
function loadEnv(filePath = '.env') {
const envPath = path.resolve(process.cwd(), filePath);
if (!fs.existsSync(envPath)) {
throw new Error(`Environment file not found: ${filePath}`);
}
const envContent = fs.readFileSync(envPath, 'utf-8');
const lines = envContent.split('\n');
lines.forEach((line) => {
if (!line || line.startsWith('#')) return;
const [rawKey, ...rawValue] = line.split('=');
if (rawKey && rawValue.length > 0) {
const key = rawKey.trim();
const value = rawValue.join('=').trim();
const trimmedValue = value.replace(/^["']|["']$/g, '');
process.env[key] = trimmedValue;
}
});
return process.env;
};
async function postJson(url, data, options) {
try {
options = options || {};
const res = await axios({
method: "POST",
url: url,
data: data,
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
"Content-Type": "application/json",
},
...options,
});
return res.data;
} catch (err) {
console.log(`Error : ${err}`);
throw err;
}
}
module.exports = { getBuffer, getJson, postJson, loadEnv };