我想发送一个自动回复到我之前没有在 Telegram 上与之交谈过的新聊天

i want to send an auto reply to a new chat with whom i havent had a conversation earlier on Telegram

我是 Python 和 Telethon 的新手。

我已经阅读了 Telethon 模块的文档。 如果新人通过 Telegram 私信给我发消息,我想发送自动回复。

我找到了一个(在 this gist 中) 它向每个传入的私人聊天发送自动回复。但我只想给新人回复。

请指导

我想检查用户是否出现在下面的 df 中并相应地发送回复。

获取对话框 ID 列表的代码如下:

import time
from telethon import TelegramClient
from telethon import events
import pandas as pd

api_id = 123456
api_hash = 'enterownapihash'

# fill in your own details here
phone = '+111111111'
session_file = 'username'  # use your username if unsure
password = 'passwords'  # if you have two-step verification enabled

client = TelegramClient(session_file, api_id, api_hash, sequential_updates=True)
xyz = pd.DataFrame()
z = pd.DataFrame()
client.connect()
client.start()

for dialog in client.iter_dialogs():
    x = dialog.id
    y = dialog.title
    #print('{:>14}: {}'.format(dialog.id, dialog.title))
    xyz['dialog id'] = [x]
    xyz['username'] = [y]
    z = pd.concat([z,xyz],ignore_index= True)
print(xyz)
print(z)

编辑: 下面是我试过但没有用的代码。

import time
from telethon import TelegramClient
from telethon import events
import pandas as pd

api_id = 123456
api_hash = 'enterownapihash'

# fill in your own details here
phone = '+111111111'
session_file = 'username'  # use your username if unsure
password = 'passwords'  # if you have two-step verification enabled

# content of the automatic reply
message = "Hello!"

client = TelegramClient(session_file, api_id, api_hash, sequential_updates=True)
xyz = pd.DataFrame()
z = pd.DataFrame()
#client.connect()
#client.start()

for dialog in client.iter_dialogs():
    x = dialog.id
    y = dialog.title
    print('{:>14}: {}'.format(dialog.id, dialog.title))
    xyz['x'] = [x]
    xyz['y'] = [y]
    z = pd.concat([z,xyz],ignore_index= True)

if __name__ == '__main__':

    @client.on(events.NewMessage(incoming=True))
    async def handle_new_message(event):
        if event.is_private:  # only auto-reply to private chats
            from_ = await event.client.get_entity(event.from_id)  # this lookup will be cached by telethon
            if event.client.get_entity != z['x'].values:
                if not from_.bot:  # don't auto-reply to bots
                    print(time.asctime(), '-', event.message)  # optionally log time and message
                    time.sleep(1)  # pause for 1 second to rate-limit automatic replies
                    await event.respond(message)


    client.start(phone, password)
    client.run_until_disconnected()

一种方法是在启动时获取对话框中存在的所有 ID。当一条新消息到达时,如果他们的 ID 不存在,你就知道这是一个新人(代码假设你 运行 它在 async def 中):

async def setup():
    users = set()
    async for dialog in client.iter_dialogs():
        if dialog.is_user:
            users.add(dialog.id)

# later, on a handler

async def handler(event):
    if event.is_private and event.sender_id not in users:
        # new user

另一种选择是使用 messages.GetPeerSettingsRequest,当它是一个新的聊天时,它将 result.report_spam 作为 True。理想情况下,您还可以缓存此内容:

user_is_new = {}

async def handler(event):
    if event.is_private:
        if event.sender_id not in user_is_new:
            res = client(functions.messages.GetPeerSettingsRequest(event.sender_id))
            # if you can report them for spam it's a new user
            user_is_new[event.sender_id] = res.report_spam

        if user_is_new[event.sender_id]:
            # new user