telme/index.js

74 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-03-19 18:51:24 +00:00
'use strict';
process.env.NTBA_FIX_319 = 'junk';
const TelegramBot = require('node-telegram-bot-api');
const HOME = require('os').homedir();
const CONFIG_FILE_NAME = '.telmeconfig';
const FILEPATH = `${HOME}/${CONFIG_FILE_NAME}`;
const APP_NAME = 'telme';
const fs = require('fs');
const Init = require('./flows/init');
const SendMessage = require('./flows/simple_message');
const TaskMessage = require('./flows/task_message');
function printUsage(withInit = false) {
if (withInit) console.log(`[Usage] $ ${APP_NAME} --init`);
console.log(`[Usage] $ ${APP_NAME} --m "message to send"`);
console.log(`[Usage] $ ${APP_NAME} <command> <args>`);
}
async function main() {
if (process.argv.indexOf('--init') != -1) {
try {
await Init.init();
printUsage();
process.exit(0);
} catch (e) {
console.error(`An error has occurred. Error: ${e.message}`);
process.exit(1);
}
}
try {
var file = fs.readFileSync(FILEPATH);
} catch (e) {
console.log(
`Please run '${APP_NAME} --init' first. then follow instructions`);
process.exit(1);
}
const {TOKEN, USER_ID, DONE_MESSAGE} = JSON.parse(file);
if (process.argv.indexOf('--m') === 2) {
if (!process.argv[3]) {
console.log('[ERROR] Missing message to send');
console.log(`[Usage] $ ${APP_NAME} --m "message to send"`);
process.exit(1);
}
const msg = process.argv[3];
try {
await SendMessage.send(TOKEN, USER_ID, msg);
console.log(`[${APP_NAME}]: Told ya!`);
process.exit(0);
} catch (e) {
console.error(`[${APP_NAME}]: An error occurred. Error: ${e.message}`);
process.exit(1);
}
} else {
if (process.argv.length < 3) {
printUsage(true);
process.exit(1);
}
try {
await TaskMessage.run(TOKEN, USER_ID, DONE_MESSAGE);
} catch (e) {
console.error(e);
}
}
}
main().then(() => {});