1

I am doing a Telegram Bot implemented in a Raspberry Pi 3. So my first idea is to use "telegraf" with Node.js and execute the code of the BOT there, but also with "shell.js" i would like to run some commands on Raspberry Terminal. The problem is that i would like the code from the BOT to wait until a file is downloaded and then the program continues...

The problem here is that the program does some things not in order of appearance.

    var shell = require('shelljs')

    const download = (ctx) => {
            ctx.reply('Downloading...')

            shell.exec('sudo bash script_Download.sh ' + ctx.command.args[0])
            while(shell.exec('ps -ef | grep wget | grep -v grep')!=''){}
            ctx.reply('Downloaded.')

            ctx.reply('Uploading the file to Google Drive...')
            shell.exec('mv ~/Super_Secret_File /mnt/gdrive/TRBDownloads')     
    }
module.exports = download

Here in this specific code, I would like to the program to write "Downloading...", after that start the script_Download begins and the program is paused until the command wget is finished (inside script_Downlaod) this part is done by this: while(shell.exec('ps -ef | grep wget | grep -v grep')!=''){}. But the return is something like "File not found" (this means that wget didnt finished) and after that, "Downloading" and "Uploading the file to Google Drive..." is written on Telegram chat.

So, i would like to know how to make this part of code executing in order: First write Downloading, execute the script, waits until it finishs and then writes Uploading... (Sorry, I don't know that much of Javascript so my knowledge right now is basic )

Rafa Roman
  • 31
  • 1
  • 5
  • Have your tried using the [`callback` parameter](https://github.com/shelljs/shelljs#execcommand--options--callback)? – Barthy Sep 26 '19 at 00:28
  • Nope, I would give it a try! – Rafa Roman Sep 26 '19 at 08:24
  • Did that work out? Do you need any more help? – Barthy Sep 26 '19 at 20:59
  • I didn't even try... Because searching for info I decided to implement the problem by trying "https" in nodejs and make my program entirely by javascript and I will not use scripts anymore. So the shelljs and his asynchronouns functions are not a problem now. Btw, thx for your answer – Rafa Roman Sep 28 '19 at 14:28

0 Answers0