Telethon get media size before download - telegram

I have the following code to download media from chat:
getmessage = client.get_messages(dialog, limit=1000)
for message in getmessage:
try:
if message.media == None:
print("message")
continue
else:
print("Media**********")
client.download_media(message)
I want to limit the download media size to X MB,
How can I get the media size in bytes before I download it?

You can refer to the Objects Reference for Message to find out the message.file property. It will be a File object with a size property. Thus:
if message.media:
print(message.file.size, 'in bytes')
Note that file will be None for media like polls.

You can get the file size with this telegram bot api: https://core.telegram.org/bots/api#file
You should send file_id as parametr, you can find file_id inside your message object.

Related

pyTelegramBotAPI message handlers can't see photo

I need my telegram bot to forward messages from the private chat to the customer care staff group.
I run this code:
#bot.message_handler(func=lambda message: message.chat.type=='private')
def forwarder(message):
bot.forward_message(group, message.chat.id, message.id)
bot.send_message(group, '#id'+str(message.chat.id))
It works smoothly with text messages, but does nothing with photos.
Even if I remove all previous message handlers, so there is no conflict with them to handle photos, it still keeps doing nothing.
If I use the get.updates() method I can check "manually" if the photo has arrived and I find it.
Edit: Even if i just run this code only
import telebot
bot = telebot.TeleBot("MY TOKEN")
#bot.message_handler(func=lambda message: True)
def trivial(message):
print('yes')
bot.polling()
I get 'yes' for text messages and absolutely nothing, not even raised exceptions for photos.
If you add content_types as a parameter you will receive whatever is specified in the argument (which takes an array of strings). Probably the default value for that parameter is set to ['text']. Which will only listen for 'text' messages.
To get the results you're looking for
your test code will look like:
import telebot
bot = telebot.TeleBot("MY TOKEN")
#bot.message_handler(func=lambda, message: True, content_types=['photo','text'])
def trivial(message):
print('yes')
bot.polling()
And your working code:
#bot.message_handler(func=lambda, message: message.chat.type=='private', content_types=['photo','text'])
def forwarder(message):
bot.forward_message(group, message.chat.id, message.id)
bot.send_message(group, '#id'+str(message.chat.id))
In Telegram Bot API there is a resource /sendphoto
See:
Sending message in telegram bot with images
Try to find the related method in the PyTelegramBotApi.
Or implement the function to send a photo your own (e.g. using requests library in Python). Then you can use it in the message-handlers of your bot, to forward images.

How to remove a sticker from Telegram sticker set (for every user)

I created a sticker set by my bot. I added there some junk images. Now I ask myself a question: How to remove a single sticker from Telegram sticker set for every user?
You should use #Stickers Bot, all you need to do is type /delsticker and follow the directions from there. As long as you are the owner of the sticker pack, it shouldn't be a problem.
Default API connection with URL:
https://api.telegram.org/bot <token>/METHOD_NAME
In order for your bot to interact with your sticker pack, you must create it using the createNewStickerSet method (https://core.telegram.org/bots/api#createnewstickerset )
Send the desired sticker to your bot
Use the get Updates method (https://core.telegram.org/bots/api#getupdates).
In the response, you will get a json structure where you can take the file_id
Use deleteStickerFromSet method (https://core.telegram.org/bots/api#deletestickerfromset)
python example
def deleteStickerFromSet(file_id: str):
data = {
'sticker': file_id,
}
r = requests.post('https://api.telegram.org/bot <token>/' + "deleteStickerFromSet", data=data)
Returns True on success.

Telegram Bot API - Using "getFile" with a high-quality photo's "file_id" yields a link to low-quality file

I'm well aware this question seems like a possible duplicate (e.g., 1, 2, 3), but I couldn't find a straight answer to my scenario anywhere.
The exact flow is as follows:
The Bot receives a message with a photo from a user. It then extracts and stores the file_id of the highest-quality photo out of the message.photo array (the last array item).
The bot fires a getFile(file_id) request using the stored file_id, which returns a single link (NOT an array) that points to a low-quality file (slightly bigger than a thumbnail).
To summarize:
Using the exact file_id with getFile() returns a link to a low-quality file.
Using the exact file_id with sendPhoto() will send a full-size photo.
On the chance I'm missing something here, can anyone confirm that that's expected behavior? Thanks.
file_id's are different in the photo array. use file_id which has the biggest file_size and then you could get the high quality file by getFile method
Base on image resolution, "photo" array contains different number file_id. Use ctx.message.photo.lenght - 1 to access the last file_id for best quality.
the best quality in -1 index
raw = message.photo[-1].file_id
path =path+"/"+raw+".jpg"
file_info = bot.get_file(raw)
downloaded_file = bot.download_file(file_info.file_path)
with open(path,'wb') as new_file:
new_file.write(downloaded_file)

Firebase Storage : Get the token of the URL

I currently have an application that works with Firebase.
I repeatedly load profile pictures. However the link is quite long, it consumes a certain amount of data. To reduce this load, I would like to put the link in raw and only load the token that is added to the link.
To explain, a link looks like this: “https://firebasestorage.googleapis.com/v0/b/fir-development.appspot.com/o/9pGveKDGphYVNTzRE5U3KTpSdpl2?alt=media&token=f408c3be-07d2-4ec2-bad7-acafedf59708”
So I would like to put in gross: https://firebasestorage.googleapis.com/v0/b/fir-developpement.appspot.com/o/
In continuation: “9pGveKDGphYVNTzRE5U3KTpSdpl2” which is the UID of the user that I recover already and the or my problem this poses: “alt = media & token = f408c3be-07d2-4ec2-bad7-acafedf59708” which adds randomly for each photo .
I would like to get back only this last random piece …
Is it possible ?
Thank you
UP : 01/11 Still no solution
It's not supported to break apart and reassemble download URLs. You should be treating these strings as if their implementation details might change without warning.

Weird thing with telegram bot api, can't change title of audio

Some weird thing with telegram api. I am trying to send audio from telegram bot and by the way to change performer and title, but I can't. First of all I thoght I made a mistake, but not! I tried to do the same thing from the browser search line becouse there is no chance to do something wrong, and no results! May be you can try to do the same thing? It would be great, becouse I don't know what is wrong. I am trying to do it on Python with pyTelegramBotAPI. For example code:
import telebot
import const
#Подключаюсь к боту
bot = telebot.TeleBot(const.token)
#bot.message_handler(content_types=["text"])
def handle_command(message):
a = bot.send_audio(message.from_user.id, musicurl, caption=None, duration=None, performer="Pharik", title="hfdhdfh",
reply_to_message_id=None)
print(a.audio.performer)
print(a.audio.title)
bot.polling(none_stop=True, interval=0)
I discover that you can't set performer and title parameters if you upload audio file by the link. If you are doing it with local file, it works. There is one way I see, take the file from link, download it, use EasyId3 to rewrite meta of mp3 file and after that send it to Telegram. But it's weird I think. Maybe it's a mistake, becouse Telegram Bot API has this parameters and it doesn't work. However Telegram uploads files on own servers, so they can change meta on their side using parametrs. Where is the logic? How knows any solutions?
I used Mp3tag to edit Title (title) and Artist (performer) of the mp3 file and Telegram use it to display after uploaded.

Resources