How to broadcast message with inline keyboard to all users in Bots.Business? - bots.business

How to broadcast message with inline keyboard to all users?
I have such code:
Bot.sendMessage("Hello")
I want:
add inline Keyboard to that message
broadcast it to all users

Command: "/message"
var message = "your text message"
var buttons = [
{title: "Go to Google", url: "https://google.com"},
{title: "Call command for Button1", command: "/touch Button1" },
{title: "Call command for Button2", command: "/touch Button2" }
];
Bot.sendInlineKeyboard(buttons, message);
Command: /broadcast
// for admin only
if(user.telegramid!="your telegram id"){ return }
Bot.runAll({
command: "/message"
})
Bot.sendMessage("message will be broadcasted to all chats")

Related

Is it possible to push text to prompt input?

I am using the prompt module, node version 12.4.0
I am wondering if its possible to push a string to the 'input field' of a prompt.get()
The following script displays a (double) "prompt: "
After 5 seconds, it inserts the text "This is a test"
When an enter command is given, the string is not recognized as input.
It isn't possible to edit the string before pressing enter either.
(type anything before 5 sec and press enter, and the input WILL display)
End-goal: to have commands send to prompt from external source, but give the end user the ability to modify the commands before entering.
I tried process.stdout.write, process.stdin.write
I also tried to replace process.std*.write with prompt.std*.write
The answer might be os-specific, but I have tried this code both under Win10 x64 as well as Linux
const prompt = require("prompt");
function myFunc() {
process.stdin.write("This is a test");
}
setTimeout(myFunc, 5000);
prompt.get({
properties: {
test: {
description: "prompt"
}
}
}, (err, result)=> {
console.log("input: "+ result.test);
});
actual result:
~/Nodejs/temp$ node index.js
prompt: prompt: This is a test
input:
~/Nodejs/temp$
desired result:
~/Nodejs/temp$ node index.js
prompt: prompt: This is a test
input: This is a test
~/Nodejs/temp$
After digging into how the prompt module works, I 'solved' this myself.
prompt uses readline behind the curtains, and readline has a .write function that does what I need, send editable text to the prompt.
prompt itself does not extend this function, and since it hasnt been maintained for 3 years, I switched to readline instead.
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'prompt> '
});
rl.prompt();
rl.on('line', (line) => {
console.log(line);
rl.prompt();
}).on('close', () => {
console.log('Have a great day!');
process.exit(0);
});
// simulate external input, and write to prompt>
function myFunc() {
rl.write("This is a test");
}
setTimeout(myFunc, 5000);

Commander.js - Implementing sub commands that executes when the previous one is finished

I'm using Commander.js to write my own CLI. I managed to write commands that work individually but now I need to implement sub commands but the docs are a bit vague and confusing so I haven't been able to figure out.
What I want is the connect command to connect to a MongoDB instance and when it has done that proceed to execute the get command. How can I achieve this?
These are the commands and package.json:
./package.json:
{
...
"main": "./commands/my-cli.js",
"bin": "./commands/my-cli.js",
...
}
./commands/my-cli.js:
const commander = require('commander');
const program = new commander.Command();
const connect = require('./my-cli-connect');
const get = require('./my-cli-get');
// Initialize each command
connect(program);
get(program);
./commands/my-cli-connect.js:
function connect(program) {
program
.command('connect <db> <username> <password>', 'Connects to a database')
.action((db, username, password) => {
MongoClient.connect(<some-mongo-url>, {useNewUrlParser: true}, (err, connection) => {
assert.equal(null, err, 'Failed to connect to MongoDB instance');
// Continue here with the get command
});
});
program.parse(process.argv);
}
module.exports = connect;
./commands/my-cli-get.js:
function get(program) {
program
.command('get <collection>')
.option('-q,--query <query>', 'Search terms', jsonParser, {})
.description('Returns documents from a MongoDB collection')
.action(action);
program.parse(process.argv);
function action(collection, options) {
// This never runs
console.log('hello world');
}
}
module.exports = get;
Running my-cli --help shows these available commands:
...
Commands:
connect <db> <username> <password> Connects to a database
help [cmd] display help for [cmd]
Example command execution that should call both connect and then get when connect has finished connecting:
$ my-cli connect myDb myUser myPass get users -q '{"email": "foo#gmail.com"}'
Right now the get command's action function never runs.

Adding samba user programically with node.js

I've wrote function in node,js as follows:
const addUserToSamba = (userLogin, password) => {
const echoPassword = spawnSync('echo', ['ne',`${password}\n${password}\n`]);
const addUserCredentialsToSamba = spawnSync('smbpasswd', ['-a', '-s', userLogin], {
input: echoPassword.stdout,
});
console.log('Added user credentials to Samba configuration');
console.log(addUserCredentialsToSamba.stderr.toString());
console.log(addUserCredentialsToSamba.stdout.toString());
}
I want to achieve something like this in bash:
echo -ne "$PASS\n$PASS\n" | smbpasswd -a -s $LOGIN
When I'm running addUserToSamba(userLogin, password); function with given userlLogin and user password then I even get message Added user ${userLogin} as a stdout of the addUserCredentialsToSamba outcome.
But the credentials don't work.
Have I correctly rewritten this bash function to the node.js one?
If no, how can I fix it?
You can simply skip the echo part and output the credentials directly to the smbpasswd process:
const addUserCredentialsToSamba = spawnSync('smbpasswd', ['-a', '-s', userLogin], {
input: `${password}\n${password}\n`,
});

NodeJS: Make it possible for user input when running child_process

I'm trying to create a Node script that will ask the user a few questions, save the input and using the answers run a mvn archetype:generate command to install their environment.
I got as far to where I get the Maven command running. But when Maven asks for user input for values such as groupId and `` I can enter the values, give an [enter] and that's where it stops.
It doesn't take input and process them. All it does is display it, as the CLI does, but doesn't accept them.
Here's a snippet of code with the values for user input pre-filled:
var spawn = require('child_process').spawn;
var answerCollection = {
"name": "nameOfMyArchetype", //answer of inquiry
"version": "1.2.3.4" //answer of inquiry
};
var cmd = "mvn";
var args = [
"archetype:generate",
"-DarchetypeArtifactId=" + answerCollection.name,
"-DarchetypeGroupId=com.backbase.expert.tools",
"-DarchetypeVersion=" + answerCollection.version
];
var runCmd = function(cmd, args, callback) {
var child = spawn(cmd, args);
child.stdin.pipe(process.stdin);
child.stdout.pipe(process.stdout);
child.stdout.on('end', function(res) {
console.log("stdout:end");
callback(res);
});
child.stderr.on('data', function(text) {
console.log("stderr:data");
console.log(data);
});
child.stderr.on('exit', function(data) {
console.log("stderr:exit");
console.log(data);
});
};
So far I've tried the above code with child_process and spawn = require('child_process').spawn('bash').
Question: Is there any other way to make sure I can trigger a script and if that returns with a prompt and asks for input I can type and enter and the script will continue?
From Facebook I got this tip to use cross-spawn, instead of child_process:
From Robert Haritonov:
Use cross-spawn: spawn('bower', bowerCommand, {stdio:'inherit'}).on('close', function () {});
This works perfectly well and provides exactly the behaviour I need.

Detach a child process from Casperjs

I'm trying to develop an test script with casperjs that, in case of failure of a single test, sends an email to the developer.
The mailing script is made with nodejs using nodemailer.
If the function that starts the mailer is somewhere in the casperjs script it works perfectly, the problem comes when the test fails: it seems that casperjs exists without leaving the time to the child process to complete.
The only way I found to make it work is to add a --auto-exit=no and to explicitly exit when I receive the mailer response. But I'd like to avoid passing the param outside the script.
Is there a way to:
set inside the script the auto-exit option?
OR to detach the child process so that it doesn't die when the parent exits?
Listener:
casper.test.on("fail", function(failure){
casper.capture(image_dir+'/ERROR.png');
tools.sendMail(
tools.config.recipients,
'Errore '+tools.config.website,
'Il Test sul controllo login è fallito. In allegato screenshot del problema.',
'Il Test sul controllo login è fallito. In allegato screenshot del problema.',
'screenshots/login'+'/ERROR.png'
);
});
Function
var sendMail = function($to,$subject,$text,$html,$attachments) {
var childProcess;
try {
childProcess = require("child_process");
} catch (e) {
casper.echo("error"+e);
}
if (childProcess) {
childProcess.execFile("/usr/bin/nodejs", ["common/mailer.js", $to, $subject, $text, $html , $attachments], null, function(err, stdout, stderr) {
casper.echo("----------------------execFileSTDOUT:"+ JSON.stringify(stdout));
casper.echo("----------------------execFileSTDERR:"+ JSON.stringify(stderr));
casper.exit(1);
});
casper.echo("Done");
} else {
casper.echo("Unable to require child process");
}
};
I've also tried to use "nohup /usr/bin/nodejs" but with no luck.
Thanks

Resources