我使用python和telethon作为电报客户端获取新消息,但需要确定答复并回复哪个消息
我有一个带有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过大量测试,我弄清楚了。
这将打印出所有消息属性的原始消息:
After a lot of testing, I figured it out.
This prints out the original message with all the message properties: