Now I am sending a message to the user, and if the user sends a message with four buttons, the first one is pressed and the code does not stop working (it waits for a message from the user further). How can I make the code stop after clicking on the first button?
import random
from telethon import TelegramClient, events
api_id = 109258102341
api_hash = 'k12iy5k2u43v51ui34'
client = TelegramClient('waererfer', api_id, api_hash)
#client.on(events.NewMessage(chats=('#username')))
async def normal_handler(event):
if event.message.button_count == 4:
await event.message.click(0)
client.start()
client.send_message('#username', 'Hello')
client.run_until_disconnected()
Instead of:
client.run_until_disconnected()
You should use:
await client.disconnect()
And the script will exit after sending the message.
Related
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.
I'm making a bot that displays new messages from different chats to the user. The first client, acting as a user, listens to the channels, and the second client, acting as a bot, displays a new message to the user.
from telethon import TelegramClient, events
api_id = '...'
api_hash = '...'
bot_token = '...'
client = TelegramClient('client', api_id, api_hash)
bot = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)
client.start()
#client.on(events.NewMessage(chats=['...']))
async def main(event):
await bot.send_message('...', event.message)
client.run_until_disconnected()
But there is a problem: if media appears in the message, then the bot does not send the message and displays the following error:
telethon.errors.rpcerrorlist.MediaEmptyError: The provided media object is invalid or the current account may not be able to send it (such as games as users) (caused by SendMediaRequest)
If the message is sent by the first client, then the message is sent and no error occurs.
Help me please!
I have a scenario where my bot starts a conversation with another bot to receive one reply with inline keyboard. How can my bot reply using the inline keyboard?
from pyrogram import Client
api_id = 12345
api_hash = "hash123"
with Client("my_account", api_id, api_hash) as app:
app.send_message("otherbot", "Hello") # This message is received by otherbot, which then triggers a reply that contains an inline keyboard
# Here I need to reply to otherbot's message
What you're looking for is a Conversation Handler.
A conversation-like feature is not available yet in Pyrogram. One way to do that is saving states into a dictionary using user IDs as keys. Check the dictionary before taking actions so that you know in which step your users are and update it once they successfully go past one action.
https://t.me/pyrogramchat/213488
(copy-pasted from another answer of mine.)
As stated by #ColinShark, pyrogram does not support Conversation Handler. So, this simple workaround worked for my use case:
from pyrogram import Client
api_id = 12345
api_hash = "hash123"
bot_name = "otherbot"
with Client("my_account", api_id, api_hash) as app:
response = app.send_message(bot_name, "Hello")
bot_reply_msg_id = int(response["message_id"]) + 1
response = app.get_messages(bot_name, bot_reply_msg_id)
try:
response.click(2, timeout=1) # to click on the third button/option. Index start at 0.
except TimeoutError:
pass
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()
I have this code
from telethon.sync import TelegramClient, events
with TelegramClient('name', api_id, api_hash) as client:
#client.on(events.NewMessage(pattern=pattern))
async def handler(event):
await event.reply("Here should be the Chat or Group name")
How to implement this?
if we are talking only about groups/channels
chat_from = event.chat if event.chat else (await event.get_chat()) # telegram MAY not send the chat enity
chat_title = chat_from.title
Else (If we want to get the full name of chat entities, including Users):
from telethon import utils
chat_from = event.chat if event.chat else (await event.get_chat()) # telegram MAY not send the chat enity
chat_title = utils.get_display_name(chat_from)
get_display_name() actually gets a name that you would see. Works for types User, Channel, Chat
That method shall not have await