I want to connect to the telegram's WebSocket or other protocol, to receive every message. I was inspecting the network tab in dev tools, but I could not find any WebSocket protocol, however, when I entered console, I can see [SW] on message {type: "ping", localNotifications: true, lang: {…}, settings: {…}} Which convince me, that it is sent to maintain connection with the WebSocket. I tried a telethon library for python, but it allows me only to show all messages in the chat.
from telethon import TelegramClient
import asyncio
client = TelegramClient(name, api_id, api_hash)
client.start()
for i in client.iter_dialogs():
if i.name == 'test':
chj = i
async def main():
channel = await client.get_entity(chj)
messages = await client.get_messages(channel, limit= None) #pass your own args
#then if you want to get all the messages text
for x in messages:
print(x) #return message.text
"""for i in client.iter_dialogs():
print(i)"""
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
I want to be getting messages like in WebSocket whenever on the desired chat someone's types something
I have found an answer. This can be achieved using events from telethon library
from telethon import TelegramClient, events
name = 'test'
api_id = 1234
api_hash = 'hash'
client = TelegramClient(name, api_id, api_hash)
#client.on(events.NewMessage())
async def newMessageListener(event):
newMessage = event.message.message
print(newMessage)
with client:
client.run_until_disconnected()
Related
Objective: Use A's API ID and API hash to sign in to B's phone
`
from telethon import TelegramClient, events, sync
import asyncio
api_id = [A's API ID]
api_hash = [A's API hash]
client = TelegramClient('anon', api_id, api_hash)
#client.on(events.NewMessage)
async def my_event_handler(event):
await event.reply('hi')
client.start(phone='[B's phone]')
client.run_until_disconnected()
`
With the above code, result is as follows:
UserWarning: the session already had an authorized user so it did not login to the user account using the provided phone (it may not be using the user you expect)
A's account is still being used in the client.
I tried the client.sign_in method but not sure how to do it:
`
from telethon import TelegramClient, events, sync
import asyncio
api_id = [A's API ID]
api_hash = [A's API hash]
client = TelegramClient('anon', api_id, api_hash)
#client.on(events.NewMessage)
async def my_event_handler(event):
await event.reply('hi')
async def signin():
print('sign in')
await client.sign_in(phone='[B's phone]')
client.start()
asyncio.run(signin())
client.run_until_disconnected()
`
Result is stuck at "print('sign in') and nothing else happened.
i am using Pyrogram to work with telegram API.
I have succeed to join channel.
I have a task to add message handler and receive messages in channel.
But the message handler is not invoked when message arrives (i am the owner of channel)
The code:
import asyncio
from pyrogram import Client
import time
from pyrogram.handlers import MessageHandler, RawUpdateHandler
api_id = "xx"
api_hash = "xx"
def my_handler(client, message):
message.forward("me")
print('sent msg')
async def main():
async with Client("my_account", api_id, api_hash) as app:
a = await app.get_chat('test2k3')
msg_handler = MessageHandler(my_handler)
app.add_handler(msg_handler)
await app.join_chat(str(a.id))
print(f'joined chat ' + str(a.id))
while True:
time.sleep(2.4)
asyncio.get_event_loop().run_until_complete(main())
Sleeping while the client runs halts it until the sleep is over. Pyrogram itself will already keep itself alive until Ctrl and C is pressed. Remove your while True sleep loop.
as recommended by sudden_appearance
use asyncio.sleep() inside async functions instead of time.sleep()
I have a simple python script to read a message from Telegram-Channle.
when the Channel with option Restrict Saving Content is enabled, then the message is empty.
I use Telethon==1.23.0
from telethon import TelegramClient, events
api_id = ******
api_hash = '2f06ea4*****'
phone = '+#######'
client: TelegramClient = TelegramClient('Client', api_id, api_hash)
#client.on(events.NewMessage(chats:'chats='https://t.me/*****'))
async def my_event_handler(event):
print(event.raw_text)
client.start()
client.run_until_disconnected()
It seems to be an open issue..
https://github.com/LonamiWebs/Telethon/issues/3264
I've been trying to send my client's incoming messages via my bot back to the client. The event handler get stuck in a loop doing so. I'm still new to asynchronous programming and telethon, any feedback or specific reference is highly appreciated. :-)
from telethon import TelegramClient, client, events
api_id = xxx
api_hash = 'xxx'
telegram_bot_token = 'xxx'
telegram_chat_id = 'xxx'
bot = TelegramClient('xxx', api_id, api_hash).start(bot_token='xxx')
client = TelegramClient('xxx', api_id, api_hash)
#client.on(events.NewMessage)
async def messaged_by_bot(event):
await bot.send_message('xxx', event.message.text)
client.start()
client.run_until_disconnected()
I'm making a bot to forward all receiving messages on telegram.
from telethon import TelegramClient
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('anon', api_id, api_hash)
log_channel=-1002030404403
async def main():
async for message in client.iter_messages('me'):
print(message.id, message.text)
if event.is_private:
await client.forward_messages(log_channel, event.message)
with client:
client.loop.run_until_complete(main())
but this is not identifying different of bot and a user
how to filter a bots
event.sender.bot will be True if the sender is a bot.
You can find this in the quick reference for Message, where it says "It bases ChatGetter and SenderGetter.", meaning .sender exists.