如何从我的私人电报频道中读取消息?

发布于 2025-01-29 17:24:16 字数 999 浏览 4 评论 0原文

我正在尝试从我的私人电报频道中阅读消息。我使用了以下代码。我在私有情况下阅读该消息时面临问题,但是如果我将渠道更改为公众,则该代码正常工作。但是我希望该频道私密,有什么解决方案吗?

import configparser
import json
import re
from telethon.errors import SessionPasswordNeededError
from telethon import TelegramClient, events, sync
from telethon.tl.functions.messages import (GetHistoryRequest)
from telethon.tl.types import (PeerChannel)

api_id = *******
api_hash = ******

# Here you define the target channel that you want to listen to:
user_input_channel = 'https://t.me/<channel_id>'

client = TelegramClient('name', api_id, api_hash)


# Listen to messages from target Channel
@client.on(events.NewMessage(chats=user_input_channel))
async def newMessageListner(event):
    # Get message text
    newMessage = event.message.message
    print(newMessage)


with client:
    client.run_until_disconnected()

在私人中使用频道时,我会收到此错误消息

Cannot find any entity corresponding to "https://t.me/<channel_id>"

注意: 通道ID被删除

I am trying to read messages from my private telegram channel. I used the following code. I am facing a problem reading the message while it is in private but if I change the channel to the public this code is working fine. But I want the channel to be private is there any solution for this?

import configparser
import json
import re
from telethon.errors import SessionPasswordNeededError
from telethon import TelegramClient, events, sync
from telethon.tl.functions.messages import (GetHistoryRequest)
from telethon.tl.types import (PeerChannel)

api_id = *******
api_hash = ******

# Here you define the target channel that you want to listen to:
user_input_channel = 'https://t.me/<channel_id>'

client = TelegramClient('name', api_id, api_hash)


# Listen to messages from target Channel
@client.on(events.NewMessage(chats=user_input_channel))
async def newMessageListner(event):
    # Get message text
    newMessage = event.message.message
    print(newMessage)


with client:
    client.run_until_disconnected()

while trying with the channel in private I am getting this error message

Cannot find any entity corresponding to "https://t.me/<channel_id>"

Note:
Channel ID is delibrately masked

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈场末日恋爱 2025-02-05 17:24:16

在频道ID之前添加-100,例如-100xxxxxxx

Add -100 before the channel Id like -100XXXXXXX

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文