Telethon auto reply to new private messages to whom I haven't had a conversation earlier

Question:

I want to automatically reply to new private messages which I haven’t had a conversation earlier. But my code gives an error and I cant figure out what the solution is.

This is what I tried out:

from telethon import TelegramClient, events
from telethon import functions, types
from time import sleep

api_id = 123456
api_hash = 'enterownapihash'


msg = 'Sorry, I am unavailable right now.'

client = TelegramClient('automessage', api_id, api_hash, sequential_updates=True)
@client.on(events.NewMessage(incoming=True, blacklist_chats=True))

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

async def handler(event):
    if event.is_private and event.sender_id not in users:
        await event.respond(msg)

this is the error I get:

Unhandled exception on setup
Traceback (most recent call last):
  File "C:UsersuserAppDataLocalProgramsPythonPython310libsite-packagestelethonclientupdates.py", line 520, in _dispatch_update
    await callback(event)
TypeError: setup() takes 0 positional arguments but 1 was given

Tried this now:

from telethon import TelegramClient, events
from telethon import functions, types
from time import sleep

api_id = 123456
api_hash = 'enterownapihash'


msg = 'Sorry, I am unavailable right now.'

client = TelegramClient('automessage', api_id, api_hash, sequential_updates=True)

async def setup():
    global users
    users = set()
    async for dialog in client.iter_dialogs():
        if dialog.is_user and not dialog.entity.bot:
            users.add(dialog.id)

@client.on(events.NewMessage(incoming=True))
async def handler(event):
    if event.is_private and event.sender_id not in users:
        await event.respond(msg)

client.start()
client.run_until_disconnected()

I get this error whenever my account gets a message:

Unhandled exception on handler
Traceback (most recent call last):
  File "C:UsersuserAppDataLocalProgramsPythonPython310libsite-packagestelethonclientupdates.py", line 520, in _dispatch_update
    await callback(event)
  File "C:UsersuserDesktopScriptsCODINGtelethonAutoRespond.py", line 27, in handler
    if event.is_private and event.sender_id not in users:
NameError: name 'users' is not defined

What is the solution to this? Thanks in advance!

Asked By: Matt Klein

||

Answers:

EDIT:

you’re not calling the setup(), you have to run it accordingly at footer of your file before locking the client until disconnected to listen to events, something like:

client.start()
client.loop.run_until_complete(setup())
client.run_until_disconnected()

users doesn’t seem in the global scope, as for the error, the decorator is on top of the wrong function..

async def setup():
    global users
    users = set()
    async for dialog in client.iter_dialogs():
        if dialog.is_user and not dialog.entity.bot:
            users.add(dialog.id)

@client.on(events.NewMessage(incoming=True))
async def handler(event):
    if event.is_private and event.sender_id not in users:
        await event.respond(msg)

also "blacklist_chats" is pointless if no "chats" is supplied.

Answered By: MustA
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.