telegraf: sendMessage failed, reason: socket hang up

Error logs

/ProjectPath/node_modules/node-fetch/lib/index.js:1483
			reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err));
          ^
FetchError: request to https://api.telegram.org/[BOT_TOKEN]:[REDACTED]/sendMessage failed, reason: socket hang up
    at ClientRequest.<anonymous> (//ProjectPath/node_modules/node-fetch/lib/index.js:1483:11)
    at ClientRequest.emit (node:events:390:28)
    at ClientRequest.emit (node:domain:475:12)
    at TLSSocket.socketOnEnd (node:_http_client:471:9)
    at TLSSocket.emit (node:events:402:35)
    at TLSSocket.emit (node:domain:475:12)
    at endReadableNT (node:internal/streams/readable:1343:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  type: 'system',
  errno: 'ECONNRESET',
  code: 'ECONNRESET'
}
  • Telegraf.js Version: 4.5.2
  • Node.js Version: 16.13.0
  • Operating System: macOS 12.1

Minimal Example Code Reproducing the Issue

const bot: Telegraf<Context<Update>> = new Telegraf(config.telegramBotToken);
bot.use(async (ctx, next) => {
  if (ctx.from && config.allowedUsers.includes(ctx.from.id.toString())) {
    await next();
  } else {
    ctx.reply("Sorry, you are not allowed to use this bot.");
  }
});

bot.use(async (_, next) => {
  const start = dayjs();
  await next();
  const ms = dayjs().diff(start);
  console.log("Response time: %sms", ms);
});

bot.launch();

cron.schedule("*/15 * * * *", async () => {
  const msg = `-= Heartbeat ${dayjs.utc().format("HH:mm")} UTC =-`;
  try {
    bot.telegram.sendMessage(config.telegramChatId, msg);
  } catch (error) {
    console.log("schedule error:", error);
  }
});

process.once("SIGINT", () => {
  bot.stop("SIGINT");
});
process.once("SIGTERM", () => {
  bot.stop("SIGTERM");
});

This error will be thrown after running for a period of time

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 15 (1 by maintainers)

Most upvoted comments

Who find solution for this error?