全球主机交流论坛

标题: mjj们 有没有办法实现tg监听群组实时转发? [打印本页]

作者: iwiiwi    时间: 2024-7-7 13:48
标题: mjj们 有没有办法实现tg监听群组实时转发?
有个群组发的媒体文件 60秒自动撤回
有没有办法就是监听这个群组 有媒体文件就自动转发到另一个群组?
作者: auto8624    时间: 2024-7-7 15:15
楼下来回答吧
作者: xhcj666    时间: 2024-7-7 15:24
很简单
作者: 大爷    时间: 2024-7-7 16:05
群组地址多少,私我我看看
作者: aba    时间: 2024-7-7 16:17
from telegram import Update, Bot
from telegram.ext import Updater, MessageHandler, Filters, CallbackContext

# 替换为您的 Bot Token
BOT_TOKEN = 'YOUR_BOT_TOKEN'
# 源群组的 Chat ID
SOURCE_CHAT_ID = -1001234567890
# 目标群组的 Chat ID
TARGET_CHAT_ID = -1009876543210

def media_forwarder(update: Update, context: CallbackContext):
    message = update.message
   
    # 检查消息是否来自指定的源群组
    if message.chat_id == SOURCE_CHAT_ID:
        # 检查消息中是否有媒体文件
        if message.photo:
            context.bot.send_photo(chat_id=TARGET_CHAT_ID, photo=message.photo[-1].file_id, caption=message.caption)
        elif message.video:
            context.bot.send_video(chat_id=TARGET_CHAT_ID, video=message.video.file_id, caption=message.caption)
        elif message.document:
            context.bot.send_document(chat_id=TARGET_CHAT_ID, document=message.document.file_id, caption=message.caption)
        elif message.audio:
            context.bot.send_audio(chat_id=TARGET_CHAT_ID, audio=message.audio.file_id, caption=message.caption)
        elif message.voice:
            context.bot.send_voice(chat_id=TARGET_CHAT_ID, voice=message.voice.file_id, caption=message.caption)
        elif message.animation:
            context.bot.send_animation(chat_id=TARGET_CHAT_ID, animation=message.animation.file_id, caption=message.caption)

def main():
    updater = Updater(token=BOT_TOKEN, use_context=True)
    dispatcher = updater.dispatcher

    # 添加处理媒体文件消息的处理器
    media_handler = MessageHandler(Filters.chat(chat_id=SOURCE_CHAT_ID) & Filters.media, media_forwarder)
    dispatcher.add_handler(media_handler)

    # 启动机器人
    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

作者: iwiiwi    时间: 2024-7-7 16:26
aba 发表于 2024-7-7 16:17
from telegram import Update, Bot
from telegram.ext import Updater, MessageHandler, Filters, Callback ...

这个代码是需要把机器人加进源群组么
作者: hous135    时间: 2024-7-7 17:42
用协议号监听
作者: iwiiwi    时间: 2024-7-7 17:48
hous135 发表于 2024-7-7 17:42
用协议号监听

协议号有 监听怎么实现呢
作者: Pyinfo    时间: 2024-7-7 17:52
建议先去了解一下telegram api hash
作者: hous135    时间: 2024-7-8 23:20
iwiiwi 发表于 2024-7-7 17:48
协议号有 监听怎么实现呢

去tg开发者官方哪里申请api_id 以及api_hash 用协议号进行登录 监听特定群组的消息 进行转发 或者就是下载之后再进行发送




欢迎光临 全球主机交流论坛 (https://443502.xyz/) Powered by Discuz! X3.4