Bot wrongly handle messages - python

I have little problem with writing my bot, I'm trying to send message only inside my bot, but my client handle any messages in any chats.
from telethon.sync import TelegramClient, events
import socks
api_id = 'my_id'
api_hash = 'my_hash'
client = TelegramClient('name', api_id, api_hash, proxy=###).start(bot_token='bot_token')
#client.on(events.NewMessage(pattern='/start'))
async def send_welcome(event):
await event.reply('How re you doing')
#client.on(events.NewMessage)
async def echo_all(event):
await event.reply(event.text)
client.run_until_disconnected()

You need to had func=lambda e: e.is_private into events.NewMessage() so the handler will only catch messages from private conversations (that's what you defined as "messages only inside your bot").
It will look like this:
#events.register(events.NewMessage(func=lambda e: e.is_private))
async def handler(event):
...

Related

Incoming Telegram message listener with Telethon

I'd like to listen for a new incoming message in a Telegram channel.
I'm using the Telethon package and tried to run the example found in the docs.
from telethon import TelegramClient, events
api_id = "..."
api_hash = "..."
CHANNEL_ID = "..."
client = TelegramClient('anon', api_id, api_hash)
client.start()
#client.on(events.NewMessage(chats=CHANNEL_ID, outgoing=False))
async def my_event_handler(event):
print(event)
client.run_until_disconnected()
This code runs, asks me for my phone number and the login code. But after that, nothing happens.
When a message is sent to the channel, I don't see anything printed out.

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)

TelegramClient is getting an error (method was never awaited)

I'm trying to send a message to a telegram channel for each new message in the API, But I'm sure that I'm not respecting the telethon documentation that I don't understand quitly.
Here is my code:
import requests, json
from telethon import TelegramClient, events, sync
class notify:
async def tele_message(discord_message):
api_id = 1111111
api_hash = 'hidden-hidden-hidden-hidden'
destination_user_username='gikou2'
client = await TelegramClient('anon', api_id, api_hash)
entity= await client.get_entity(destination_user_username)
await client.send_message(entity=entity,message=discord_message)
await client.start()
await client.run_until_disconnected()
headers = {
'authorization': 'authorization-authorization-authorization-authorization',
}
params = (('limit', '50'),)
read_mess = list()
while True:
response = requests.get('https://discord.com/api/v9/channels/292516924557099008/messages', headers=headers, params=params)
for message in reversed(response.json()):
if message['id'] not in read_mess:
read_mess.append(message['id'])
if message['author']['username'] == 'Noah Williams':
print(message['content'])
# client.send_file(destination_user_username, media, caption=event.raw_text)
notify.tele_message(message['content'])
First thing you don't need to make ansync function for TelegramClient and if its import for you then you have to also use wait while using the tele_message(ur func)
Maybe i am wrong but you can try

python telethon wait for reply

I currently have code to send message to my friend
but how can I know if he reply
this is my current code
please see the commented line
from telethon import TelegramClient
api_id = '1234567'
api_hash = 'MYHASH'
client = TelegramClient('session', api_id, api_hash)
client.start(phone_number)
destination_user_username='friend'
entity=client.get_entity(destination_user_username)
client.send_message(entity=entity,message="hello")
#if he reply hi
client.send_message(entity=entity,message="have a nice day")
how can I do this?
I found in the doc
solution 1
for message in client.iter_messages(chat):
print(message)
solution 2
api_id = '1234567'
api_hash = 'MYHASH'
with TelegramClient('session') as client:
destination_user_username='friend'
entity=client.get_entity(destination_user_username)
client.send_message(entity=entity,message="hello")
from telethon import events
#client.on(events.NewMessage(pattern='hi'))
async def handler(event):
# here received messgae, do something with event
print(dir(event)) # check all possible methods/operations/attributes
# reply once and then disconnect
await event.reply("have a nice day")
await client.disconnect()
client.run_until_disconnected()

Connection error while using telethon through python?

here is the code which I am using to operate telegram through python. But on running this, I am getting an error.
import asyncio
from telethon import TelegramClient,connection
api_id = xxxxxx
api_hash = 'xxxxxxxxxxx'
client = TelegramClient('anon', api_id, api_hash,
connection=connection.ConnectionTcpMTProxyRandomizedIntermediate,
proxy=('mtproxy.example.com', 2002, 'secret'))
async def main():
# Getting information about yourself
me = await client.get_me()
print('hello')
await main()
when I run this code, I get ConnectionError: Cannot send requests while disconnected

Categories

Resources