const LifeforcePlugin = require("../utils/LifeforcePlugin.js");

let messageQueue = [];
let readySend = false;
let readyTimer = null;

class TelegramBot extends LifeforcePlugin {
  constructor(restifyserver, logger, name) {
    super(restifyserver, logger, name);
    this.apiMap = [
      {
        path: "/api/telegram/log",
        type: "post",
        handler: handleTelegramLog
      }
    ];

    const TGBot = require("node-telegram-bot-api");
    this.bot = new TGBot(this.config.telegram.token, { polling: true });

    this.bot.on("message", msg => {
      const chatId = msg.chat.id;

      // send a message to the chat acknowledging receipt of their message
      this.bot.sendMessage(
        chatId,
        "Hello! Here is the information about your chat message: \n" +
          JSON.stringify(msg.chat)
      );
    });

    if (this.config.telegram.logger) {
      var messageSend = function(message) {
        messageQueue.push(message);
      };

      logger.registerCallback(messageSend.bind(this));

      readyTimer = setInterval(() => {
        if (messageQueue.length > 0) {
          this.bot.sendMessage(
            this.config.telegram.chatid,
            messageQueue.join("\n")
          );
          messageQueue = [];
        }
      }, 10000);
    }
  }
}

function handleTelegramLog(req, res, next) {
  if (req.body) {
    var payload = req.body + "";

    // send a message to the chat acknowledging receipt of their message
    this.bot.sendMessage(this.config.telegram.chatid, payload);
    res.send(200, { error: false });
  } else {
    res.send(400);
  }
}

module.exports = TelegramBot;

@@ -56,16 +56,42 @@
 in.js%22);
+%0Aconst fs = require(%22fs%22);
 %0A%0Alet me
@@ -626,77 +626,720 @@
     
-// send a message to the chat acknowledging receipt of their message%0A
+let response =%0A        %22Hello! Here is the information about your chat message: %5Cn%22 +%0A        JSON.stringify(msg.chat);%0A%0A      if (msg.document) %7B%0A        response = %22You sent a file: %22 + JSON.stringify(msg.document);%0A        const filepath = this.config.telegram.filepath;%0A%0A        this.bot%0A          .downloadFile(msg.document.file_id, filepath)%0A          .then(newpath =%3E %7B%0A            let savepath = filepath + %22/%22 + msg.document.file_name;%0A%0A            // Take the file from newpath and move it into a better location%0A            fs.rename(newpath, savepath, done =%3E %7B%0A              this.bot.sendMessage(chatId, %22Saved %22 + msg.document.file_id);%0A            %7D);%0A          %7D)%0A          .catch(err =%3E %7B%0A      
     
@@ -1370,16 +1370,22 @@
         
+      
 chatId,%0A
@@ -1396,112 +1396,237 @@
     
-%22Hello! Here is the information about your chat message: %5Cn%22 +%0A          JSON.stringify(msg.chat)%0A      
+      %22Unable to save %22 + msg.document.file_id + %22, err: %22 + err.message%0A            );%0A          %7D);%0A      %7D%0A%0A      // send a message to the chat acknowledging receipt of their message%0A      this.bot.sendMessage(chatId, response
 );%0A 

Allow file upload via telegram