Next.js Discord

Discord Forum

Embed telegram Channel posts

Unanswered
Polar bear posted this in #help-forum
Open in Discord
Avatar
Polar bearOP
How can I show the latest telegram channel posts on my nextjs app?

8 Replies

Avatar
You basically need telegram api then? are you asking about general approach or specific issue here?
Avatar
Polar bearOP
General approach
I've read the telegram docs and didn't found what I looking for
Avatar
add you bot to your channel (as normal user) and then use the endpoint to read the chat history. The correct endpoint should be: https://core.telegram.org/method/messages.getHistory
Avatar
import { NextResponse } from 'next/server';

const TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN;
const TELEGRAM_CHANNEL_USERNAME = process.env.TELEGRAM_CHANNEL_USERNAME;

export async function GET() {
  try {
    const response = await fetch(
      `https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/getUpdates?chat_id=@${TELEGRAM_CHANNEL_USERNAME}&limit=10`
    );
    const data = await response.json();

    if (!data.ok) {
      throw new Error('Failed to fetch Telegram posts');
    }

    const posts = data.result
      .filter((update: any) => update.channel_post)
      .map((update: any) => update.channel_post);

    return NextResponse.json({ posts });
  } catch (error) {
    console.error('Error fetching Telegram posts:', error);
    return NextResponse.json({ error: 'Failed to fetch Telegram posts' }, { status: 500 });
  }
}

This can be used as API route that fetches latest updates from telegram api
then in your page, call this api in interval - not sure what did you mean by "real time" but this approach should work mostly
Avatar
Polar bearOP
It's a public channel.
Not a chat.
Will this work
Avatar
I haven't tried it yet. Try and see