yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.13k stars 1.49k forks source link

sendPhoto not working, doesn't throw any error #1128

Closed emibonezzi closed 9 months ago

emibonezzi commented 9 months ago

Hello, I'm trying to develop a test telegram bot that creates a picture with a random number on it and send it to a chat. Since deploying a function that includes node-canvas from a zip file is painful I've used docker to build an image (aws node base image) and deployed to a lambda function through a image within a ECR container. When I test it in the lambda console management, the code runs with no error as you can see:

image image

But the bot doesn't send anything at all in the groupchat. It doesn't even throw any err related to the token or chat_id. To spot where something is going wrong I've put a "console.log" to see if something doesn't execute but as you can see in the previous log, the "ended" log runs so the stream actually works. It seems like the problem is in the sendPhoto function maybe?

Here's my index.js code

const { createCanvas } = require("canvas");
const telegramBot = require("node-telegram-bot-api");
const bot = new telegramBot("5233454187:XXXXX");

exports.handler = async function (event, context) {
  console.log("Inside the func");
  // create canva
  const canvas = createCanvas(200, 200);
  // create context
  const ctx = canvas.getContext("2d");
  // set font
  ctx.font = "20px Arial";
  ctx.fillStyle = "black";
  // fill text
  ctx.fillText(Math.random().toString(), 20, 50, 150);
  // once you're done crafting the canva
  // create buffer
  try {
    const buffer = canvas.toBuffer();
    // send picture
    await bot.sendPhoto("-1001177958034", buffer);
  } catch {
    await bot.sendMessage("-1001177958034", "Ops, qualcosa è andato storto.");
  }
};

// -1001177958034

Here's my Dockerfile for references:


ARG ARCH=arm64
FROM public.ecr.aws/lambda/nodejs:18-${ARCH}

#architecture -x86_64 or arm64
# copy function code
COPY . ${LAMBDA_TASK_ROOT}

RUN yum update -y
RUN yum groupinstall "Development Tools" -y
RUN yum install gcc-c++ cairo-devel pango-devel libjpeg-turbo-devel giflib-devel librsvg2-devel pango-devel bzip2-devel jq python3 -y
RUN mkdir ${LAMBDA_TASK_ROOT}/lib

RUN npm install canvas --build-from-source
RUN npm install

# set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "index.handler" ]

It could be something related to the "tmp" folder of the lambda function and creating a write stream inside that. Somehow the stream gets created and the picture generated but when it's time for the bot to send it it disappear? I'm really confused.

emibonezzi commented 9 months ago

Solved, I wasn't "awaiting" the sendPhoto function.

// send picture
    await bot.sendPhoto("-1001177958034", buffer);