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()
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 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 want to use telethon to send messages to my own private channel, and receive mobile push notifications when the python script posts a message. With the below code I'm able to send the messages, but I do not receive any mobile push notifications. I've turned on all notification settings in the mobile app. I have been googling around for 'telethon push notifications', without any luck.
from telethon import TelegramClient
api_id = 'api_id'
api_hash = 'api_hash'
username = 'username'
channel_invite_link = 'channel invite link'
async def func():
entity = await client.get_entity(channel_invite_link)
await client.send_message(entity=entity, message="Hi")
with TelegramClient(username, api_id, api_hash) as client:
client.loop.run_until_complete(func())
Just use the silent argument, i.e.:
await client.send_message(entity=entity, message="Hi", silent=False)
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()
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.