Telethon changes the name to the current time - python

I want my application to change my name in the telegram to the current time every minute. I have already tried to do something, but to no avail
from telethon import TelegramClient
from telethon.tl.functions.account import UpdateProfileRequest
import asyncio
import datetime
today = datetime.datetime.today()
time= today.strftime("%H.%M")
api_id = 123456
api_hash = 'ххх'
client = TelegramClient('session_name', api_id, api_hash)
client.start()
async def main():
while True:
await client(UpdateProfileRequest(first_name=time))
await asyncio.sleep(1)
client.loop.run_forever()

from telethon import TelegramClient
from telethon.tl.functions.account import UpdateProfileRequest
import asyncio
import datetime
api_id = 123456
api_hash = 'ххх'
client = TelegramClient('session_name', api_id, api_hash)
client.start()
async def main():
while True:
time = datetime.datetime.today().strftime("%H.%M")
async with client:
await client(UpdateProfileRequest(first_name=time))
await asyncio.sleep(60)
asyncio.get_event_loop().run_until_complete(main())

first stuff don't use while loop , it may use too memory and disable handle updates from telethon
second stuff 1 second its too fast and telegram may ban you account for spam
I prefer to use aiocron
Install aiocron using the following command
pip3 install aiocron
Code:
import asyncio, aiocron, datetime
from telethon import TelegramClient, events, sync, functions, types
from telethon.tl.functions.account import UpdateProfileRequest
api_id = 123456
api_hash = "ххх"
client = TelegramClient("session_name", api_id, api_hash)
client.start()
#aiocron.crontab("*/1 * * * *")
async def set_clock():
time = datetime.datetime.today().strftime("%H.%M")
async with client:
await client(UpdateProfileRequest(first_name=time))
#client.on(events.NewMessage)
async def e(event):
if event.raw_text == "ping":
await event.reply("pong")
client.run_until_disconnected()

Related

Telethon New Message Event Handler waits minute

Telethon event handler waits 1 minute before sending out a burst of messages at the same time.
I tried removing functions from other souces as I thought that could be it and it did not work.
code:
`
from telethon import TelegramClient, events
import logging
import time
#from main import add
logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s', level=logging.WARNING)
api_id =
api_hash =
client = TelegramClient('anon', api_id, api_hash)
#client.on(events.NewMessage)
async def my_event_handler(event):
print(event.raw_text)
#add(event.raw_text)
client.start()
client.run_until_disconnected()
`
form me work ok and i have testet
python need to run all time
#exit()
import sys
from telethon import TelegramClient, events
import logging
import time
import telethon.tl.functions as _fn
logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s', level=logging.WARNING)
api_id = row[2]
api_hash = row[3]
client = TelegramClient(path+str(api_id), api_id, api_hash)
#client.on(events.NewMessage)
async def my_event_handler(event):
print(event.raw_text)
print(event)
#add(event.raw_text)
client.start()
client.run_until_disconnected()
print('Finish...')

How to edit client massage in Telegram using python?

I need to edit last message sent with python. How can I do it?
from telethon import TelegramClient, events, sync
from telethon.tl.functions.messages import EditMessageRequest
# Remember to use your own values from my.telegram.org!
api_id = api_id
api_hash = api_hash
client = TelegramClient('anon', api_id, api_hash)
#client.on(events.NewMessage(chats='j'))
async def my_event_handler(event):
print(event.raw_text)
await client.send_message(event.chat_id,'dd')
event.edit_massge("")
client.start()
client.run_until_disconnected()
When I send a message I want the script to edit it to the text that I will enter.
from telethon import TelegramClient, events, sync
from telethon.tl.functions.messages import EditMessageRequest
# Remember to use your own values from my.telegram.org!
api_id = api_id
api_hash = api_hash
client = TelegramClient('anon', api_id, api_hash)
#client.on(events.NewMessage(chats='j'))
async def my_event_handler(event):
print(event.raw_text)
message = await client.send_message(event.chat_id, "hello") # changed line
await client.edit_message(event.chat_id, message, "hello!") # changed line
client.start()
client.run_until_disconnected()
In addition, you can only edit your own messages (just in case you want to edit a message from someone else)

Can't get NewMessage event on SuperGroup (SelfBot)

I'm trying to listen to supergroup new messages, but I can't seem to be able to:
from telethon.sync import TelegramClient
from telethon import events, utils
from telethon.tl.types import InputMessagesFilterPhotos
api_id = 98.....
api_hash = 'acbc55199.....'
num = '+921.......'
client = TelegramClient(num, api_id, api_hash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(num)
client.sign_in(num, input('Code: '))
#client.on(events.NewMessage(chats=[-123456789]))
async def handler(event):
print(event.from_id.user_id)
It works on group I created and channels/groups I'm a member in, but it doesn't for supergroups.
Any idea if I'm doing anything wrong?

Telethon verions 1.11.3 with python 3.7.3 not seems to be working

Upon trying following code on Telethon 1.11.3 , python 3.7.3
from telethon import TelegramClient
api_id = xxx #i give my id
api_hash = xxx##i give my
client = TelegramClient(name, api_id, api_hash)
async def main():
# Now you can use all client methods listed below, like for example...
await client.send_message('me', 'Hello to myself!')
with client:
client.loop.run_until_complete(main())
I get error as RuntimeError: You must use "async with" if the event loop is running (i.e. you are inside an "async def")
The problem is not the code itself but the shell you used. IPython and similar run the asyncio event loop which break Telethon's sync magic.
To work around this, you can use a normal python shell or write async and await in the right places:
from telethon import TelegramClient
api_id = ...
api_hash = ...
client = TelegramClient(name, api_id, api_hash)
async def main():
# Now you can use all client methods listed below, like for example...
await client.send_message('me', 'Hello to myself!')
# Note the async and await keywords
async with client:
await main()
Of course, in this scenario, main() is not really necessary either:
async with client:
await client.send_message('me', 'Hello to myself!')
# Create an object for TelegramClient()
client = TelegramClient(phone, api_id, api_hash)
async with client :
await client.send_message('me', 'Hello!!!!!')
client.connect()
if not client.is_user_authorized() :
client.send_code_request(phone)
client.sign_in(phone, input('Enter verification code : '))

RuntimeWarning: coroutine 'main' was never awaited error

I run this test code:
import telethon.sync
from telethon import TelegramClient
from telethon.tl.functions.messages import AddChatUserRequest
from telethon.tl.functions.contacts import ImportContactsRequest
api_id = XXXXXXX
api_hash = 'XXXXXXXXXXXXXXC'
with TelegramClient('anon', api_id, api_hash) as client:
async def main():
client(AddChatUserRequest(-XXXXXXXXXXXXXX, ['username'], fwd_limit=10))
main()
And it gives me this:
/data/data/ru.iiec.pydroid3/files/temp_iiec_codefile.py:19: RuntimeWarning: coroutine 'main' was never awaited
main()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
What should I do to make the program work?
Take a look at telethon documentation, see how it starts event loop:
from telethon import TelegramClient
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('anon', api_id, api_hash)
async def main():
me = await client.get_me()
# etc.
with client:
client.loop.run_until_complete(main())

Categories

Resources