【问题标题】:Make a simple bot for telegram forwarding制作一个简单的电报转发机器人
【发布时间】:2023-10-27 15:06:01
【问题描述】:

我是一名试图为电报制作机器人的学生。 我已经用谷歌试过了,但我真的很难做到。 我想做的是 2 个机器人,帮助我收集信息。

  1. 从电报频道或群组(我不是主持人)自动转发到我的频道或群组

  2. 从 twitter 自动转发到我的电报,但在过滤我设置的关键字后转发

我已经尝试过 IFTTT,但没有解决方案将频道转发到频道,并在从 twitter 转发时过滤关键字。

谢谢:)

【问题讨论】:

    标签: twitter filtering chatbot telegram-bot


    【解决方案1】:

    您应该使用 MTPROTO 来充分发挥 Telegram API 的潜力,因为机器人是真正 MTPROTO API 的有限版本,它们无法为您的工作(即爬行)提供全部潜力。

    其中一个优秀的 MTPROTO 库是用 python 编写的,名为Telethon

    你可以从这个用 Telethon 编写的源代码中得到一个想法:

    api_id = "YOUR API ID HERE"
    api_hash = "YOUR API HASH HERE"
    
    client = TelegramClient(session_name, api_id, api_hash)
    client.start()
    
    to_copy_groups = {
        from_group_id_here: to_group_id_here,
    }
    
    @client.on(event=events.NewMessage)
    async def message_handler(event):
        from_id = None
        chat_type = None
        if isinstance(event.peer_id, PeerChat):
            from_id = event.peer_id.chat_id
            chat_type = "Chat"
        elif isinstance(event.peer_id, PeerChannel):
            from_id = event.peer_id.channel_id
            chat_type = "Channel"
        if from_id is not None and from_id in to_copy_groups:
            if to_copy_groups[from_id] in groups:
                if chat_type == "Chat":
                    await event.forward_to(PeerChat(to_copy_groups[from_id]))
                if chat_type == "Channel":
                    await event.forward_to(PeerChannel(to_copy_groups[from_id]))
        return
    
    

    【讨论】: