Support
Quality
Security
License
Reuse
kandi has reviewed Telethon and discovered the below as its top functions. This is intended to give you an instant insight into Telethon implemented functionality, and help decide if they suit your requirements.
Pure Python 3 MTProto API Telegram client library, for bots too!
Telethon error when import InputPeerChannel
>>> from telethon.tl.types import InputPeerChannel
Python script stops responding when receiving saved messages
async for message in client.get_messages('me'):
me = await client.get_me()
messages = await client.get_messages('me', limit=100)
for message in messages:
....
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
from telethon import TelegramClient
import asyncio
saved = open('saved_messages.txt', 'a')
client = TelegramClient('anon', '1', '2')
client.start()
async def main():
me = await client.get_me()
messages = await client.get_messages('me', limit=100)
for message in messages:
saved.write(f'{message.text}\n')
print(message.id, message.text)
saved.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
-----------------------
async for message in client.get_messages('me'):
me = await client.get_me()
messages = await client.get_messages('me', limit=100)
for message in messages:
....
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
from telethon import TelegramClient
import asyncio
saved = open('saved_messages.txt', 'a')
client = TelegramClient('anon', '1', '2')
client.start()
async def main():
me = await client.get_me()
messages = await client.get_messages('me', limit=100)
for message in messages:
saved.write(f'{message.text}\n')
print(message.id, message.text)
saved.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
-----------------------
async for message in client.get_messages('me'):
me = await client.get_me()
messages = await client.get_messages('me', limit=100)
for message in messages:
....
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
from telethon import TelegramClient
import asyncio
saved = open('saved_messages.txt', 'a')
client = TelegramClient('anon', '1', '2')
client.start()
async def main():
me = await client.get_me()
messages = await client.get_messages('me', limit=100)
for message in messages:
saved.write(f'{message.text}\n')
print(message.id, message.text)
saved.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
-----------------------
async for message in client.get_messages('me'):
me = await client.get_me()
messages = await client.get_messages('me', limit=100)
for message in messages:
....
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
from telethon import TelegramClient
import asyncio
saved = open('saved_messages.txt', 'a')
client = TelegramClient('anon', '1', '2')
client.start()
async def main():
me = await client.get_me()
messages = await client.get_messages('me', limit=100)
for message in messages:
saved.write(f'{message.text}\n')
print(message.id, message.text)
saved.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Telethon get channel participants without admin privilages
async for dialog in client.iter_dialogs():
if dialog.is_channel:
print( dialog.entity.participants_count)
-----------------------
client.get_participants(channel, aggressive=True)
Q:how to increment file creation from an event?
import os
import re
path = r'C:\\Users\\USER\\PycharmProjects\\rakna2\\'
client.start()
@client.on(events.NewMessage(chats='tradingnava'))
async def my_event_handler(event):
texte = (event.text)
texte = texte.split(" ")
Marche = texte[1]
Direction = texte[2]
allfile = [int(re.findall(f'\d+', fname)[0]) for fname in os.listdir(path)
if fname.startswith("OIF") and re.findall(f'\d+', fname)]
try:
s = str(max(allfile) + 1)
except ValueError:
allfile = [0]
with open(r'OIF%s.txt' %str(max(allfile) + 1), 'w') as f:
Direction = 'buy'
if Direction == 'buy':
f.write("buy b a")
f.close()
elif Direction == 'sell':
f.write("sell b a")
f.close()
else:
f.write(f"Direction Error {Direction}")
f.close()
Function does not work when return is specified
async def sendUserMessage(self, response):
isSent = False
client = TelegramClient('session', api_id, api_hash)
await client.connect()
result = await client(functions.contacts.GetContactsRequest(
hash=0
))
for user in result.users:
try:
s = SequenceMatcher(None, response, user.first_name)
if s.ratio() > 0.75 or distance.levenshtein(response, user.first_name) < 3:
speak("What do you wanna send?")
message = takeCommand()
isSent = True
receiver = InputPeerUser(user.id, 0)
await client.send_message(receiver, message, parse_mode='html')
else:
pass
except Exception:
pass
await client.disconnect()
if isSent:
speak("Message sent successfully")
else:
speak("Could not find that user in your contacts")
How to parse this result
for user in result.users:
print (user.id, user.first_name)
Telegram Bot presses Inline button too long
import asyncio
...
asyncio.create_task(messages.click(0))
Python Telethon, how to resume a download media
import os
file = 'file.rar'
try:
offset = os.path.getsize(file)
except OSError:
offset = 0
with open(file, 'ab') as fd:
# ^ append
async for chunk in client.iter_download(dialog.media, offset=offset):
# ^~~~~~~~~~~~~ resume from offset
fd.write(chunk)
Accept a request to subscribe to a private telegram channel via aiogram
@dp.chat_join_request_handler()
async def echo(message: types.Message):
await bot.approve_chat_join_request(
message.chat.id,
message.from_user.id)
-----------------------
@dp.chat_join_request_handler()
async def join(update: types.ChatJoinRequest):
await update.approve()
How can I send messages to my private telegram channel with Telethon?
import asyncio
async def sendMSG(channel, msg):
entity = client.get_entity(channel)
await client.send_message(entity = entity,message=msg)
asyncio.run(sendMSG("Channel Name", "Hello"))
QUESTION
Telethon error when import InputPeerChannel
Asked 2022-Apr-17 at 18:26I just started learning python. Now I'm doing a bot for Telegram. To work with the Core API, I use the Telethon library for Python 3. The error occurs in the line:
from telethon.utils import InputPeerChannel
Error text:
Traceback (most recent call last):
File "F:\Work\Projects\Python\Bots\NewsBot\main.py", line 5, in <module>
from telethon.utils import InputPeerChannel
ImportError: cannot import name 'InputPeerChannel' from 'telethon.utils' (F:\Work\Projects\Python\Bots\NewsBot\venv\lib\site-packages\telethon\utils.py)
ANSWER
Answered 2022-Apr-17 at 18:26An ImportError
in python indicates that the name (InputPeerChannel
in this case) isn't in the module (telethon.utils
in this case) or that the module can't be found.
In your case happens the first, the module telethon.utils
exists but it not contains the name InputPeerChannel
. This name can be found in the module tlethon.tl.types
so you can import it from there.
>>> from telethon.tl.types import InputPeerChannel
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Explore Related Topics
Save this library and start creating your kit