D
D
dxrkn3sssss2021-07-18 23:35:03
Python
dxrkn3sssss, 2021-07-18 23:35:03

How to get the id of the sender of the message?

The question is in the header.
Telethon.
The bottom line is to add a check for the sender's ID (if *sender id* == *my id**:), so that the script does not react to all messages with the tag !.

from telethon.sync import TelegramClient, events
import time
with TelegramClient('name', api_id="123", api_hash="abcd") as client:
   client.start()
   print("Logined")
   @client.on(events.NewMessage(pattern='!'))
   async def handler(event):
      try:
            print("Message got")
            text = event.text.split(" ")
            print(text)
            if text[0]=="!spam":
               i = 0
               while i < int(text[1]):
                  await event.respond(text[2])
                  print ('Message '+text[2]+ ' send!')
                  i += 1
                  time.sleep(int(text[3]))
      except Exception as e:
         await event.respond(e)
   client.run_until_disconnected()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Arkhipov, 2014-12-30
@arvitaly

Do you have errors enabled? Perhaps some content is given before the headings?
Is the domain from which the page is called the same?

A
Alexey Zakharov, 2014-12-30
@Zakhar0v

cookies you have are not "not saved", but not sent.
because in addition to the headers, something has already been sent to the client.
The most common (and for beginners, the most mysterious) reason is that
in PHP, everything that is not in <? ... ?> is output to the browser. For example:

<?PHP
echo 'Это PHP код'; 
?>
а это не PHP код, и в браузер будет выведено как простой текст, даже если это просто пробел или символ перевода строки.

thus, to control the output, you need to control either all (absolutely all) characters after the tag ?>or not use the closing tag at all, which in the comments above was correctly advised by @maxaon By the
way, NetBeans in the latest version (7.4) also recommends removing the closing tag.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question