我使用python和telethon作为电报客户端获取新消息,但需要确定答复并回复哪个消息

发布于 2025-01-24 14:20:40 字数 1722 浏览 0 评论 0原文

我有一个带有Telethon的Python脚本,它可以使用我的帐户登录,连接到正确的频道,并且可以很好地获取新消息。它还可以识别它是否是新消息与对先前消息的答复。我只需要弄清楚答复正在答复哪个消息。这是我到目前为止的代码。

import requests
import configparser
import threading
from telethon import TelegramClient, events, sync
from telethon.errors import SessionPasswordNeededError
from telethon.tl.types import (
    PeerChannel
)

from os import system
system("title " + "My Telegram Client")

# Reading Configs
config = configparser.ConfigParser()
config.read("config.ini")

# Setting configuration values
api_id = config['Telegram']['api_id']
api_hash = config['Telegram']['api_hash']

api_hash = str(api_hash)

phone = config['Telegram']['phone']
username = config['Telegram']['username']

subjectFilter = ['']
levelFilter = ['']

client = TelegramClient(username, api_id, api_hash)

async def main(phone):
    await client.start()
    print("TELEGRAM CLIENT CREATED")
    # Ensure you're authorized
    if await client.is_user_authorized() == False:
        await client.send_code_request(phone)
        try:
            await client.sign_in(phone, input('Enter the code: '))
        except SessionPasswordNeededError:
            await client.sign_in(password=input('Password: '))

me = await client.get_me()
user_input_channel=PeerChannel(int(-1172633073)) #BL TECH PRO
print("Connected to BL Tech Pro Telegram Channel")

@client.on(events.NewMessage(chats=user_input_channel))
    async def newMessageListener(event):
    newMessage = event.message.message
    
    #CHECK IF MESSAGE IS A REPLY
    if event.message.is_reply:
        print("reply")
    else:
        print("not reply")    
    
    #PRINT MESSAGE TO SCREEN
    print(newMessage)

client.start()
client.run_until_disconnected()

I have a working Python script with Telethon and it logs in with my account, connects to the correct channel and gets new messages just fine. It also can identify if it is a new message vs a reply to a previous message. I just need to figure out which message the reply is replying to. Here is the code I have so far.

import requests
import configparser
import threading
from telethon import TelegramClient, events, sync
from telethon.errors import SessionPasswordNeededError
from telethon.tl.types import (
    PeerChannel
)

from os import system
system("title " + "My Telegram Client")

# Reading Configs
config = configparser.ConfigParser()
config.read("config.ini")

# Setting configuration values
api_id = config['Telegram']['api_id']
api_hash = config['Telegram']['api_hash']

api_hash = str(api_hash)

phone = config['Telegram']['phone']
username = config['Telegram']['username']

subjectFilter = ['']
levelFilter = ['']

client = TelegramClient(username, api_id, api_hash)

async def main(phone):
    await client.start()
    print("TELEGRAM CLIENT CREATED")
    # Ensure you're authorized
    if await client.is_user_authorized() == False:
        await client.send_code_request(phone)
        try:
            await client.sign_in(phone, input('Enter the code: '))
        except SessionPasswordNeededError:
            await client.sign_in(password=input('Password: '))

me = await client.get_me()
user_input_channel=PeerChannel(int(-1172633073)) #BL TECH PRO
print("Connected to BL Tech Pro Telegram Channel")

@client.on(events.NewMessage(chats=user_input_channel))
    async def newMessageListener(event):
    newMessage = event.message.message
    
    #CHECK IF MESSAGE IS A REPLY
    if event.message.is_reply:
        print("reply")
    else:
        print("not reply")    
    
    #PRINT MESSAGE TO SCREEN
    print(newMessage)

client.start()
client.run_until_disconnected()

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

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

发布评论

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

评论(1

浸婚纱 2025-01-31 14:20:40

经过大量测试,我弄清楚了。
这将打印出所有消息属性的原始消息:

if event.message.is_reply:
    orgMessage = await event.get_reply_message()
    print("reply")
    print(orgMessage)
else:
    print("not reply")

After a lot of testing, I figured it out.
This prints out the original message with all the message properties:

if event.message.is_reply:
    orgMessage = await event.get_reply_message()
    print("reply")
    print(orgMessage)
else:
    print("not reply")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文