I create an api in nextjs "/api" folder.
I have tried several solution but each one returns same error.
I am using node-telegram-bot-api
and same issue is still occurring. Any information will be very helpful. Thank you
import { NextApiRequest, NextApiResponse } from "next";
import TelegramBot from "node-telegram-bot-api";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "POST") {
return res.status(405).json({ error: "Method not allowed" });
}
const { message } = req.body;
const chatId = "34323:efwefwewef";
const botToken = "-23233223";
if (!botToken || !chatId || !message) {
return res.status(400).json({ error: "Missing required parameters" });
}
try {
const bot = new TelegramBot(botToken, { polling: false });
await bot.sendMessage(chatId, message);
} catch (error: any) {
console.log(error);
res
.status(400)
.json({ error: "Failed to send message", details: error.message });
}
}
When I call this enpoint I get an error saying:
Error: ETELEGRAM: 404 Not Found
2
Answers
I figured it out after almost 4 hours
I swapped chatId for botToken and vise versa
LOL
your code probably looks ok i dont think there is problem in it… maybe the problem is in the request itself, are you certain that the botToken provided is valid ?
try by curling a request there :
$ curl https://api.telegram.org/bot<token>/getMe
it should return a response with no error if the token we valid.