So, i want to send Telegram API Bot in Google Cloud VM Instance using Ubuntu latest version or the highest version. I’m using NodeJS program and fetch to run the API.
But i got this error:
TypeError: fetch failed
at node:internal/deps/undici/undici:13185:13
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async exports.sendMessage (/root/anonymous/controller/telegram/sendMessageController.js:19:21) {
[cause]: AggregateError [ETIMEDOUT]:
at internalConnectMultiple (node:net:1117:18)
at internalConnectMultiple (node:net:1185:5)
at Timeout.internalConnectMultipleTimeout (node:net:1711:5)
at listOnTimeout (node:internal/timers:596:11)
at process.processTimers (node:internal/timers:529:7) {
code: 'ETIMEDOUT',
[errors]: [ [Error], [Error] ]
}
}
This my code (sendMessageController.js) :
const dotenv = require("dotenv");
dotenv.config();
exports.sendMessage = async (req, res) => {
const { message } = req.body;
if (!req.body || Object.keys(req.body).length === 0) {
return res.status(400).json({
status: 400,
message: "Bad request",
});
}
try {
const token = process.env.TELEGRAM_TOKEN;
const url = process.env.TELEGRAM_API_URL;
const channel = process.env.TELEGRAM_CHANNEL_ID;
const request = await fetch(`${url}/bot${token}/sendMessage`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
chat_id: channel,
text: message.replace(/[_*[]()~`>#+-=|{}.!-]/g, "\$&"),
parse_mode: "MarkdownV2",
}),
});
const response = await request.json();
console.log(response);
res.status(200).json({ status: 200, message: "Success", data: response });
} catch (error) {
console.error(error);
res.status(500).json({ status: 500, message: "Internal server error" });
}
};
I’m triggering using REST API, the issue is from this code.
I don’t now what to do, maybe you all experts can figure my problem out. If there’s a question, don’t be shy to ask for it.
Thank you
What i expect is the fetch is working, nothing failed like in Localhost.