Skip to content

Instantly share code, notes, and snippets.

@gordinmitya
Created October 2, 2023 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gordinmitya/c332c213e312585641b19f9abc38eab1 to your computer and use it in GitHub Desktop.
Save gordinmitya/c332c213e312585641b19f9abc38eab1 to your computer and use it in GitHub Desktop.
Python bot to get user_id by forwarded message
"""
pip install python-telegram-bot
"""
from telegram import Update
from telegram.ext import ApplicationBuilder, MessageHandler, ContextTypes
import telegram.ext.filters as filters
async def any_message_handler(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
msg = f'Your user id is `{update.effective_user.id}`\n'
if update.message.forward_from:
user_id = update.message.forward_from.id
msg += f'You forwarded a message from `{user_id}`'
await update.message.reply_markdown(msg, reply_to_message_id=update.message.message_id)
app = ApplicationBuilder().token("TOKEN HERE").build()
app.add_handler(MessageHandler(filters.ALL, any_message_handler))
app.run_polling()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment