Telegram Bot delete sent photos? - python

I am looking to delete a photo sent by my bot (reply_photo()), I can't find any specific reference to doing so in the documentation, and have tried delete_message(), but don't know how to delete a photo. Is it currently possible?

It's currently possible in Telegram API, not the Bot API unfortunately. It's a shame :(

I found a workaround (that I am currently still playing with). I named the photo I sent, then when I want to delete it, I delete it using .delete(). For example:
photo = x.message.reply_photo(...)
...
...
photo.delete()

You need to have the chat_id and the message_id of that message sent by bot, then you can delete using context.bot.delete_message(chat_id, message_id).
Note: Bot cannot delete a message if it was sent more than 48 hours ago.

Related

Can i get chat id with pyrogram?

I want to know, is there a way, to get a of a chat which i'm joining my bot to?(using pyrogram)
like, i created a chat manager bot, and when i'm joining him to chat, i want him to work in this specific chat.
But every function like get_chat_members() needs target id.
how do it get this?
i already found a solution
You can get in through a message form that chat.

How to update a message text on telegram api ? (Python)

I'm developing a telegram bot using telepot, I'm trying to ask the user a question for example: What's your name?
Then wait for the user to type the name, and save it in a variable, but this is not a library method, I think.
So I decided to create my own method.
When asking the question "What is your name?" I will go to the telegram api and I will change the text field to empty so in the next function I will use a loop that does not break until the user's last message is different from empty, understand?
I access: https://api.telegram.org/bot/(enter image description heretoken)getUpdates
and I want to change this session:
(Change a message send by user)enter image description here
"message":{"message_id":1133,"from":{"id":5296812825,"is_bot":false,"first_name":"Vinicius","language_code":"pt-br"},"chat" :{"id":5296812825,"first_name":"Vinicius","type":"private"},"date":1652800679,"text":"Mensagem from User"}}] }
for
"message":{"message_id":1133,"from":{"id":5296812825,"is_bot":false,"first_name":"Vinicius","language_code":"pt-br"},"chat" :{"id":5296812825,"first_name":"Vinicius","type":"private"},"date":1652800679,"text":"EMPTY"}}] }
No; Bot's can't alter messages send by an user
A Telegram bot can only alter/delete his own messages.
Luckily, otherwise every message you send could be altered by some random bot, this would lead to insecure behaviour for sure.

discord.py - Whenever someone deletes a message, send a message

I want to make it so that whenever someone deletes a message, I want to send a message straight after. I tried making some code that I thought would work, but it doesn't.
if message.delete():
await message.channel.send("stop deleting")
You can do that with the help of client events that is sent by discord when a message is deleted. Doc link - on_message_delete

Open another Telegram chat/group/channel using Bot

I suspect it may be rather kid question – but anyway.
How to open another Telegram chat or group or channel using pyTelegramBotAPI? I want to forward the user (not message, the user himself) to another channel if he clicks certain button.
I saw content type migrate_to_chat_id in Message class declaration. Should I use it? If so, how to get an id of channel I need? It won't send any message to my bot.
I would better use "t.me/..." url.
Partly solved.
Speaking about the buttons, it is indeed easy. You just use named parameter url= in InlineKeyboardButton() method.
For other cases. You need to open another channel(s) from function depending on several conditions for instance. Still don't know. Import requests and make GET request? I suspect that something for it should already be in pyTelegramBotAPI, but searching in lib files wasn't successful.

How to use parse mode in telegram send message bot in Python

I'm pretty new to making bot thing so I have several questions I want to ask regarding this:
I'm using a bot to send message by
def send_message(chat_id, msg):
url = f"https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={msg}"
requests.get(url)
send_message(chat_id,msg)
By this, I can send message and I want to beautify the result by using parse_mode but I don't know where to put in in url to make it work.
I'm alternate to using Telethon. I can use it to send message to individual and group by user name and group invite link, but when I try to send it to channel by:
client.send_message(entity='my channel name', message=message)
When I try to run it, it return Cannot find any entity corresponding to "Test channel".
I also try
destination_channel_username='test_ali3'
entity=client.get_entity(destination_channel_username)
client.send_message(entity=entity,message="Hi")
but it require my channel access_hash, how can get it, or are there any other way to send message to channel.
I know Telegram bot API have function like sendMessage or bot.sendMessage that also can do the job, but somehow I can't call it, which packages should I install and/or import.
Thanks a lot!
This should do the trick:
def send_message(chat_id, msg, parse_mode):
url = f"https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text={msg}&parse_mode={parse_mode}"
requests.get(url)
TBH I'm not too familiar with telethon, but that library is mainly intended for so called userbots. that means that it uses the API that also the Telegram app uses and because of that, it's often times more involved than just the plain bot api.
To use the Bot API in Python, I can recommend the package python-telegram-bot, see https://python-telegram-bot.org. Disclaimer: I'm currently the maintainer of that package. There are also other python packages for the bot api, see e.g. https://core.telegram.org/bots/samples#python

Categories

Resources