A
A
Asriel2022-04-21 14:27:46
Python
Asriel, 2022-04-21 14:27:46

How to send a message knowing the user id in Telegram?

I'm writing code that will catch new messages on a channel. And if there is a certain text, then it will write to a certain user. The layer in the form of a bot is not used, messages should be sent on behalf of my account with which the user has already had correspondence.

It gives an error when sending a message

Request caused struct.error: argument out of range: GetUsersRequest(id=[InputUse
r(user_id=5091977781, access_hash=0)])


Here is the code
from telethon import TelegramClient, events
import asyncio

api_id = [api_id]
api_hash = "[api_hash]"
channel = -1001379139877
client = TelegramClient('bot', api_id, api_hash)

@client.on(events.NewMessage(chats=channel))
async def my_event_handler(event):
if "Первый, кто напишет" in str(event.message.message):
       await client.send_message(5091977781, "+")

client.start()
client.run_until_disconnected()


Why is this error happening and how do I send messages?

PS: there is a correspondence with the user

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2022-04-21
@phaggi

A simple googling quickly found the following on stackowerflow :

from telethon import utils
real_id, peer_type = utils.resolve_id(-1001234567891)

print(real_id)  # 1234567891
print(peer_type)  # <class 'telethon.tl.types.PeerChannel'>

peer = peer_type(real_id)
print(peer)  # PeerChannel(channel_id=1234567891)

But you better study this issue in more detail, there is a continuation, because. there are nuances.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question