Embed telegram Channel posts
Unanswered
Polar bear posted this in #help-forum
Polar bearOP
How can I show the latest telegram channel posts on my nextjs app?
15 Replies
@Polar bear How can I show the latest telegram channel posts on my nextjs app?
You basically need telegram api then? are you asking about general approach or specific issue here?
Polar bearOP
General approach
I've read the telegram docs and didn't found what I looking for
@Polar bear How can I show the latest telegram channel posts on my nextjs app?
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
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
@B33fb0n3 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
Polar bearOP
It's a public channel.
Not a chat.
Will this work
Not a chat.
Will this work
@Polar bear It's a public channel.
Not a chat.
Will this work
I haven't tried it yet. Try and see
@Polar bear solved?
Polar bearOP
I didn't try yet
@Polar bear I didn't try yet
when will you try it?
Polar bearOP
I'll try to do it now.
@Polar bear what's the result?
@B33fb0n3 <@881974465221783606> what's the result?
Polar bearOP
sorry about the delay.
I've rethink about this, and decided to make the bot send messages instead of using it to read.