grammyjs / grammY

The Telegram Bot Framework.
https://grammy.dev
MIT License
2.28k stars 112 forks source link

How to run grammy sendMessage on nextjs app router #534

Closed RicardoPinheiroo closed 8 months ago

RicardoPinheiroo commented 8 months ago
//app/actions.js
import { Bot } from "grammy";
async function bot(pedidoId) {

  const supabase = await getSupabase();

  let data, error;
  try {
    ({ data, error } = await supabase.rpc("get_pedido_by_id", { pedido_id: pedidoId }));
  } catch (err) {
    console.error("Error calling RPC function:", err);
  }

  if (error) {
    console.error("Error querying Pedido:", error);
  }

  const bot = new Bot(process.env.TELEGRAM_BOT);

  try {
    await bot.api.sendMessage(
      process.env.CHAT_ID,
      `Novo pedido! ${data.Pedido_nucleo} no valor de ${data.Pedido_valor}€`
    )
  } catch (err) {
    console.error("Error sending message:", err);
  }

  return null;
}

i have this code to send a message when a user creates a new request, its working fine on localhost. When i deploy the code to vercel it doesnt work.

ERROR: Error sending message: i [HttpError]: Network request for 'sendMessage' failed!

KnorpelSenf commented 8 months ago

Can you enable sensitive logs and share why the network request fails?

RicardoPinheiroo commented 8 months ago

it gives me this error in vercel:

Error sending message: n [HttpError]: Network request for 'sendMessage' failed! at /var/task/frontend/.next/server/chunks/860.js:24:3347 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async l.call (/var/task/frontend/.next/server/chunks/860.js:24:1164) at async l.callApi (/var/task/frontend/.next/server/chunks/860.js:24:2052) at async b (/var/task/frontend/.next/server/chunks/18.js:1:7647) at async s (/var/task/frontend/.next/server/chunks/18.js:1:7335) at async /var/task/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:16:406 at async rg (/var/task/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:15:6309) at async rz (/var/task/frontend/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:16:24709) at async Y (/var/task/frontend/node_modules/next/dist/compiled/next-server/server.runtime.prod.js:16:25417) { error: TypeError: Expected signal to be an instanceof AbortSignal at new W (/var/task/frontend/.next/server/chunks/860.js:40:61610) at /var/task/frontend/.next/server/chunks/860.js:40:63056 at new Promise () at Y (/var/task/frontend/.next/server/chunks/860.js:40:63008) at l.call (/var/task/frontend/.next/server/chunks/860.js:24:1059) at l.callApi (/var/task/frontend/.next/server/chunks/860.js:24:2063) at i.sendMessage (/var/task/frontend/.next/server/chunks/860.js:17:41052) at b (/var/task/frontend/.next/server/chunks/18.js:1:7659) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async s (/var/task/frontend/.next/server/chunks/18.js:1:7335) }

KnorpelSenf commented 8 months ago

Ah. Try this:

import { Bot } from "grammy/web";
RicardoPinheiroo commented 8 months ago

It worked, thanks.