skip to Main Content

I was wondering if there is a possible way to get messages from the telegram channel knowing that I logged in to this account and I am the admin of this channel so I just want the get messages.

import feedparser
    from telegram import Update, ForceReply, InlineKeyboardButton, InlineKeyboardMarkup
    from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext, CallbackQueryHandler
    from bs4 import BeautifulSoup
    from datetime import datetime
    import json
    import telegram
    from time import sleep
    from telegram.ext import MessageHandler, Filters
    
    
    class Config:
        def __init__(self):
            with open("config.json", "r") as config:
                self.config = json.load(config)
    
    
    class TelegramBotChannel:
    
        def __init__(self, token, start_channel_id):
            self.updater = Updater(token=token, use_context=True)
            self.dispatcher = self.updater.dispatcher
            self.start_channel_id = start_channel_id
    
    
    if __name__ == '__main__':
        telegram_bot = TelegramBotChannel(Config().config["token"], Config().config["start"])
        pass

3

Answers


  1. Bots can only get updates about channel posts if they are a member in that channel (and bots can only be added to channels as admin). If they are admins in the channel, they will receive updates just like from every other chat.

    Login or Signup to reply.
  2. Requirements :

    • Your bot should be in the channel. obviously as an admin

    so first just make a function :

    def forwader(update , context):
    context.bot.copy_message("@temporary2for"  ,"@tempmain" , update.channel_post.message_id)
    

    After that make handler :

    forwadHandler= MessageHandler(Filters.text & (~Filters.command) , forwader)
    

    Than register your handler :

    dispatcher.add_handler(forwadHandler)
    

    Than don’t forget to start Bot polling :

    updater.start_polling()
    updater.idle()
    

    Full code :

    from telegram import bot
    from telegram.ext import Updater , CommandHandler , Filters , MessageHandler
    from config import useless
    import logging
    
    
    updater = Updater(token=useless, use_context=True)
    dispatcher = updater.dispatcher
    import logging
    logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                         level=logging.INFO)
    
    
    
    
    
    def forwader(update , context):
        
        context.bot.copy_message("@temporary2for"  ,"@tempmain" , update.channel_post.message_id)
    
    
    forwadHandler= MessageHandler(Filters.text & (~Filters.command) , forwader)
    
    
    
    dispatcher.add_handler(forwadHandler)
    
    
    updater.start_polling()
    updater.idle()
    

    Some Import are useless .

    Login or Signup to reply.
  3. This is the minimal code to fetch the messages from a channel using a telegram bot which is the subscriber (only admin subscription possible) of the channel. Provide the correct bot api as KEY.:

    from api_keys import bot_api_key as KEY
    from telegram.ext import Updater, Filters, MessageHandler
    
    updater = Updater(token=KEY, use_context=True)
    dispatcher = updater.dispatcher
    
    
    def forwarder(update, context):
        msg = update.channel_post
        if msg:
            print(msg)
    
    
    forwardHandler = MessageHandler(Filters.text & (~Filters.command), forwarder)
    dispatcher.add_handler(forwardHandler)
    updater.start_polling()
    updater.idle()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search