首页 > 解决方案 > Telegram Bot API:使用 getUserProfilePhotos() 时服务器关闭

问题描述

语境

我将 telegraf.js 用于 3 个以上的机器人,它们在很多组中运行良好。目前其中一个机器人需要请求没有显示图片的用户的显示图片。

它按预期工作得很好,但不幸的是,机器人抛出了一个错误,服务器宕机了

当前代码

// Request for display picture
const requestDP = async  (ctx, next) => {
  const { from, chat } = ctx.message;
  const { can_delete_messages:canDelete } = await deletePermision(ctx);

  let totalDPs = 1;
  totalDPs = await ctx.telegram.getUserProfilePhotos(ctx.message.from.id)
  .then((result) => result.total_count)
  .catch((err) => {
    console.log(err);
  });

  if (totalDPs != 0) return next();

  if ((chat.id === -1001341734527 || chat.id === -1001083711103 ) && canDelete) {
    await ctx.deleteMessage().then((response) => response, ({ response }) => response.ok);

    await ctx.replyWithMarkdown(`[${from.first_name||''} ${from.last_name||''}](tg://user?id=${from.id}), പ്രൊഫൈൽ ഫോട്ടോ ഇല്ലാത്തവർക്ക് ഗ്രൂപിൽ മെസേജ് അയക്കാൻ സാധ്യമല്ല`)
    .then(({ message_id }) => setTimeout(() => ctx.deleteMessage(message_id), 5 * 60 * 1000))
    .catch((err) => console.log("Error in prevencontacts: " + err));
  }
}

module.exports = requestDP;

失败信息(针对错误)

let totalDPs = 1;
totalDPs = await ctx.telegram.getUserProfilePhotos(ctx.message.from.id)
.then((result) => result.total_count)
.catch((err) => {
  console.log(err);
});

这些功能似乎运行良好。但不幸的是,有时他们会收到一个错误,然后服务器就宕机了。错误信息

Error: 400: Bad Request: request for new profile photos has already been sent

偶尔刷新服务器真的很难:-(
有什么解决方案来管理这个错误吗?

标签: javascriptnode.jsecmascript-6botstelegram-bot

解决方案


推荐阅读