首页 > 解决方案 > 制作一个简单的电报转发机器人

问题描述

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

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

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

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

谢谢 :)

标签: twitterfilteringchatbottelegram-bot

解决方案


您应该使用 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


推荐阅读