3

Necesito llamar a un método de una clase la cual no debe volver a instanciarse, ya que provoca un error.

En main.js:

var telegram = require('./telegram.js');
telegram.hello();

En telegram.js

const bot = new TelegramBot(token, {polling: true});

function hello(req) {
        console.log("hello!!");
}

module.exports.hello = hello;

El error es por instanciar dos veces el bot de telegram:

Conflict: terminated by other long poll or webhook
2

2 respuestas 2

0

La forma mas facil que veo es crear una variable global para cachear tu TelegramBot en tu telegram.js

let bot = null;
if (global.cachedModule) {
    bot = global.cachedModule;
} else {
    bot = new TelegramBot(token, {polling: true});
    global.cachedModule = bot;
}

function hello(req) {
        console.log("hello!!");
}

module.exports.hello = hello;
0

A ver, lo que pones de ejemplo

const bot = new TelegramBot(token, {polling: true});

function hello(req) {
        console.log("hello!!");
}

module.exports.hello = hello;

Te exporta la función hello y sin embargo, bot no se está usando.

Si quieres crear una instancia de TelegramBot, agregarle métodos y exportar esa instancia, entonces:

const bot = new TelegramBot(token, {polling: true});

bot.algo = function(){}

module.exports = bot;

Tienes que exportar al objeto, no al método del objeto.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.