I have this backend API i am working on, i’m trying to implement alternative telegram authentication beside the official Telegram widget or passport auth. so i have a POST request which wait for 30min until it get the message from user on telegram through telegram bot. the issue is when i send the FIRST request and also send back “/start (code)” on the bot it works as i expected but it doesn’t work for more than one time. it console log error: [polling_error] {"code":"ERR_HTTP_HEADERS_SENT"}
, i’m sending back only one response on request. i don’t understand where this error is coming from. i’m using “node-telegram-bot-api” to interact with telegram bot API.
here’s what my code looks like:
controller.js
exports.signIn = (req, res, next) => {
const { botSignInHandler } = require("./handlers");
const { signInMethod } = req.params;
if (signInMethod === "bot") {
req.setTimeout(1800000); // 30 min
let { startId } = req.query;
if (startId) {
return botSignInHandler(startId, bot, (user) => {
bot.sendMessage(user.chat_id, "success");
res.json(user);
});
} else {
let err = new Error("start-id not found");
err.statusCode = 400;
return next(err);
}
}
};
handlers.js
exports.botSignInHandler = (startId, bot, cb) => {
return bot.on("message", (msg) => {
let user = {
chat_id: msg.chat.id,
first_name: msg.chat.first_name,
last_name: msg.chat.last_name ? msg.chat.last_name : "",
avatar: uiAvatar(msg.chat.first_name),
};
let text = msg.text;
let _id = text.split(" ")[1];
if (_id === startId) return cb(user);
});
};