This repository was archived by the owner on Jan 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
124 lines (106 loc) · 3.39 KB
/
index.js
File metadata and controls
124 lines (106 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env node
'use strict';
const program = require('commander');
const inquirer = require('inquirer');
const Configstore = require('configstore');
const chalk = require('chalk');
const questions = require('./lib/question');
const constants = require('./lib/constants');
const api = require('./lib/api');
const path = require('path');
const packageJson = require(path.join(__dirname, 'package.json'));
// Create a Configstore instance
const configStore = new Configstore(packageJson.name);
program
.name('nalo-sms')
.version(packageJson.version, '-v, --version')
.description(packageJson.description)
.usage('[command] [options]');
program
.command('set <value>')
.option('-u, --user', 'Set a global nalo-sms username')
.option('-p, --pass', 'Set a global nalo-sms password')
.option('-s, --source', 'Set a global nalo-sms message source')
.description('Set global config for nalo-sms-cli')
.action(function(value, cmd) {
if (cmd.user) {
configStore.set('user.username', value);
}
if (cmd.pass) {
configStore.set('user.password', value);
}
});
program
.command('send')
.description('Send message from the cli')
.action(function() {
const query = questions.send_message_question(configStore);
inquirer.prompt(query).then(res => {
res.dlr = res.dlr ? 1 : 0;
res.type = constants.getMessageType(res.type);
res.username =
res.use_global_user === true
? configStore.get('user.username')
: res.username;
res.password =
res.use_global_pass === true
? configStore.get('user.password')
: res.password;
if (res.store_username) {
configStore.set('user.username', res.username);
}
if (res.store_password) {
configStore.set('user.password', res.password);
}
const result = {};
result['dlr'] = res.dlr;
result['type'] = res.type;
result['username'] = res.username;
result['message'] = res.message;
result['destination'] = res.destination;
result['source'] = res.source;
result['password'] = res.password;
api
.sendSMS(result)
.then(res => {
if (res.body.includes(':')) {
console.log(
chalk.red(constants.getErrorType(res.body.split(':')[0]))
);
return;
}
const code = res.body.split('|')[0];
if (code === '1701') {
console.log(chalk.green('Message has been successfully sent'));
} else {
console.log(chalk.red('Unknown error. Contact Support'));
}
})
.catch(err => {
console.log(chalk.red(err.message));
});
});
});
program
.command('balance')
.description('Check your credit balance')
.action(function() {
let username = configStore.get('user.username');
let password = configStore.get('user.password');
if (username == null || password == null) {
return console.log(chalk.yellow('Run nalo-sms set [options] <value>'))
}
api.checkBalance(username, password)
.then(res => {
let result = JSON.parse(res.body);
console.log(chalk.yellow(`Your balance is Ghc${result.balance}`));
})
.catch(err => {
console.log(err.message);
})
});
program.parse(process.argv);
// Show help if no command is parsed
if (!process.argv.slice(2).length) {
program.outputHelp();
}