【问题标题】:Forwarding posts from a private channel of Telegram从 Telegram 的私人频道转发帖子
【发布时间】:2018-05-12 17:36:51
【问题描述】:

我有一个私人频道的邀请链接,我想从这个频道转发(或发送)帖子给我。我想要的伪代码如下所示。

def get_channel(bot, update):
   message=update.channel_post.text
   print(message)

updater = Updater(my_token)
channel_handler = MessageHandler(Filters.text, get_channel, 
channel_post_updates=True, invite_link='http://t.me/aa23faba22939bf')
updater.dispatcher.add_handler(channel_handler)

当我的机器人在我创建的频道中时,这很有效(为我的目的添加了邀请链接。我不知道应该在哪里输入邀请链接)。但我想要的是我的机器人从我的机器人“不”包含的频道转发帖子。我更喜欢 python,但任何 API 都可以。我搜索了所有的谷歌世界,但没有任何线索。任何提示表示赞赏。

【问题讨论】:

  • 您是机器人管理员吗?
  • Sean/ 是的,我当然是机器人的管理员。我在 Telethon API 中找到了一个解决方案:target_channel = client.get_entity('invite link after t.me'),剩下的问题是我应该从实体 'target_channel' 中提取消息。

标签: python python-3.x telegram telegram-bot python-telegram-bot


【解决方案1】:

我找到了 Telethon 库的解决方案。它对我有用(http://telethon.readthedocs.io/en/latest/extra/advanced-usage/update-modes.html)

def callback(update):
    print('I received', update)

client = TelegramClient('session', api_id, api_hash,
                        update_workers=1, spawn_read_thread=False)

client.connect()
client.add_event_handler(callback)
client.idle()  # ends with Ctrl+C

在回调函数中,您可以只过滤频道帖子或群组消息。

【讨论】:

    【解决方案2】:

    如果您想将消息(任何类型)从频道(无论是私人还是公共)转发到任何其他聊天:

    1. 将您的机器人添加到频道(请注意,机器人仅作为管理员添加到频道并具有管理员权限)

    2. 以下代码(在 python 中)将执行转发:

       from telegram import Bot
       def forward(update, context):
           chat_id = update.effective_chat.id
      
           bot = Bot(token=TOKEN)
           bot.forward_message(chat_id = chat_id,
                               from_chat_id = channel_id,
                               message_id = (number))
      

      上面的代码,将消息从'from_chat_id'转发到'chat_id'(是询问查询的用户的chat_id)

    3. channel_id 只是频道的电报邀请链接(以@开头)

    4. message_id 是频道中消息的唯一编号。如果您右键单击频道内的消息,然后选择“复制帖子链接”并将其粘贴到某处,则如下所示: https://t.me/c/123456789/2040

      (123456789是私人频道的链接。如果是公共频道,不是'123456789',而是以@开头的频道公共地址)

    5. 消息 id 是:2040,不是全部。

    参考:https://python-telegram-bot.readthedocs.io/en/stable/telegram.bot.html#telegram.Bot.forward_message

    【讨论】:

      猜你喜欢
      • 2022-07-17
      • 2021-08-08
      • 2016-10-19
      • 1970-01-01
      • 1970-01-01
      • 2018-01-07
      • 2021-07-01
      • 2017-06-06
      • 1970-01-01
      相关资源
      最近更新 更多