functional code does not run on my telegram bot

1

The problem is that this code:

const Jimp = require('Jimp');

var map = [["⬜","⬜","u","⬜","⬜","⬜"],["⬜","⬜","⬜","⬜","⬜","⬜"], 
           ["⬜","⬜","⬜","⬜","⬜","⬜"],["⬜","⬜","⬜","⬜","⬜","⬜"], 
           ["⬜","⬜","⬜","⬜","⬜","⬜"],["⬜","⬜","⬜","⬜","⬜","⬜"]];

var imageData = [
[ 0xFF0000FF, 0xFF0000FF, 0xFF0000FF, 0xFF0000FF, 0x00FF00FF, 0xFF0000FF ],
[ 0xFF0000FF, 0xFF0000FF, 0x0000FFFF, 0xFF0000FF, 0xFF0000FF, 0xFF0000FF ],
[ 0xFF0000FF, 0x00FF00FF, 0xFF0000FF, 0xFF0000FF, 0xFF0000FF, 0x0000FFFF ],
[ 0xFF0000FF, 0xFF0000FF, 0xFF0000FF, 0xFF0000FF, 0x00FF00FF, 0xFF0000FF ],
[ 0xFF0000FF, 0xFF0000FF, 0x0000FFFF, 0xFF0000FF, 0xFF0000FF, 0xFF0000FF ],
[ 0xFF0000FF, 0x00FF00FF, 0xFF0000FF, 0xFF0000FF, 0xFF0000FF, 0x0000FFFF ]
];

for (var i = 6 - 1; i >= 0; i--) {
    for (var o = 6 - 1; o >= 0; o--) {
        if(map[i][o]=="⬜"){
            imageData[i][o]=0xFFFFFFFF;
        }
        else{
            imageData[i][o]=0x000000FF;
        }
    }
}

var id=scaleArray(imageData,30);

let image = new Jimp(180, 180, function (err, image) {
if (err) throw err;

id.forEach((row, y) => {
   row.forEach((color, x) => {
     image.setPixelColor(color, x, y);
   });
});

image.write('test.png', (err) => {
   if (err) throw err;
   });
});

function scaleArray(src, factor) {

var srcWidth = src[0].length,
  srcHeight = src.length,
  dstWidth = srcWidth  * factor,
  dstHeight = srcHeight * factor;

 var dst = [];
 for (var col = 0; col < srcHeight; col++) {
    for (var i = 0; i < factor; i++) {
       var current_col = [];
       for (var row  = 0; row < srcWidth; row++) {
           for (var j = 0; j < factor; j++) {
               current_col.push(src[col][row]);
           }
       }
    dst.push(current_col);
  }
}
return dst;
}

This code collects what is in the map array, and converts it into a 500x500 image, in this case it returns:

but once I try to use it within a command of my bot:

 bot.onText(/\/photo/, (msg)=> {
    makePNG();
    bot.sendPhoto(msg.chat.id, "test.png");
 });

using the function or putting it directly in the message:

    function makePNG(){
    for (var i = 6 - 1; i >= 0; i--) {
        for (var o = 6 - 1; o >= 0; o--) {
            if(map[i][o]=="⬜"){
                imageData[i][o]=0xFFFFFFFF;
            }
            else{
                imageData[i][o]=0x000000FF;
            }
        }
    }
    var imgr=scaleArray(imageData,30);
    var image = new Jimp(180, 180, function (err, image) {
      if (err) throw err;

      imgr.forEach((row, y) => {
        row.forEach((color, x) => {
          image.setPixelColor(color, x, y);
        });
      });
      image.write('test.png', (err) => {
        if (err) throw err;
      });
    });
}

This does not send the photo of the bot, however, if I have already created the image, and put the bot.sendPhoto to the function, send me that photo, I mean, that the execution stops at the time of I convert my array into a photo.

I would really appreciate the help. PS: this is the error that returns, and does not clarify anything:

error: [polling_error] {}
    
asked by RGVylar 11.04.2018 в 10:10
source

1 answer

2

As he said @ lois6b the problem was that he did not have time to finish the image before the delivery was executed:

    bot.onText(/\/photo/, (msg)=> {
    promise=new Promise(resolve => {
        for (var i = 6 - 1; i >= 0; i--) {
            for (var o = 6 - 1; o >= 0; o--) {
                if(map[i][o]=="⬜"){
                    imageData[i][o]=0xFFFFFFFF;
                }
                else{
                    imageData[i][o]=0x000000FF;
                }
            }
        }
        var imgr=scaleArray(imageData,30);
        var image = new Jimp(180, 180, function (err, image) {
          if (err) throw err;

          imgr.forEach((row, y) => {
            row.forEach((color, x) => {
              image.setPixelColor(color, x, y);
            });
          });
          image.write('test.png', (err) => {
            if (err) throw err;
          });
        });
        resolve();
    }).then(_ => {
      bot.sendPhoto(msg.chat.id, "test.png");
    }).catch(err => {
      console.log(err);
    });JJ
});

I do not know if I'm doing it right, but for now, the problem has been solved.

    
answered by 11.04.2018 в 11:31