How to send File via Telegram Bot - telegram-bot

I need to send txt File using TelegramBot API .
I already tried https://api.telegram.org/botMYT0KEN/sendDocument?chat_id=569502265&document=/Users/users/Desktop/file.txt
and have issue :
{"ok":false,"error_code":400,"description":"Bad Request: wrong remote
file id specified: Wrong string length”}

To complete Donny's answer, I would like to add an example of sending .txt file as a document.
Looks like you're using OS X as an operating system, which has curl utility preinstalled.
Open terminal and make a request like this:
curl -v -F "chat_id=569502265" -F document=#/Users/users/Desktop/file.txt https://api.telegram.org/bot<TOKEN>/sendDocument

From Telegram official documentation
Sending files There are three ways to send files (photos, stickers, audio, media, etc.):
If the file is already stored somewhere on the Telegram servers, you
don't need to reupload it: each file object has a file_id field,
simply pass this file_id as a parameter instead of uploading. There
are no limits for files sent this way. Provide Telegram with an HTTP
URL for the file to be sent. Telegram will download and send the file.
5 MB max size for photos and 20 MB max for other types of content.
Post the file using multipart/form-data in the usual way that files
are uploaded via the browser. 10 MB max size for photos, 50 MB for
other files. Sending by file_id
It is not possible to change the file type when resending by file_id.
I.e. a video can't be sent as a photo, a photo can't be sent as a
document, etc. It is not possible to resend thumbnails. Resending a
photo by file_id will send all of its sizes. file_id is unique for
each individual bot and can't be transferred from one bot to another.
Sending by URL
When sending by URL the target file must have the correct MIME type
(e.g., audio/mpeg for sendAudio, etc.). In sendDocument, sending by
URL will currently only work for gif, pdf and zip files. To use
sendVoice, the file must have the type audio/ogg and be no more than
1MB in size. 1–20MB voice notes will be sent as files. Other
configurations may work but we can't guarantee that they will.
https://core.telegram.org/bots/api
This is what you need from your question:
Post the file using multipart/form-data in the usual way that files
are uploaded via the browser. 10 MB max size for photos, 50 MB for
other files

You have to use the POST method instead GET. For example, I use https://github.com/php-curl-class/php-curl-class
$url = 'https://api.telegram.org/bot'.$botId.'/sendDocument';
$curl = new Curl();
$curl->post($url, [
"chat_id" => $chatId,
"document" => new \CURLFile($file_path)
]);

Related

Can't send a binary attachment to Slack using LogicApp

I'm trying to use a logicapp to get the content of an email and post it to slack. By content I mean:
the body of the email and other elements like From:, Subject:
any attachment in the email (which usually are binary like PDF, Excel, image)
the email itself saved in a blob as .eml file
Slack chat.postMessage API works without any problem to send any text element. This API has some attachment argument but doesn't seemto be designed or binary files (or not for files at all, only strings)
I've tried slack files.upload one but couldn't figure out the syntax, especially the syntax using a regular HTTP POST. Could find examples online using curl, Python, JS and C# SDK but I don't know how to translate them to HTTP POST just like I do with chat.PostMessage
I've tried the API on SOAP UI, using file as argument as per the documentation, and I've used it in different sections: in the header, in the body, and using the Attachment Tab, none of the work and always the same error message : no_file_data
Unfortuatelly slack documentation lacks of details. Here's what it says about files.upload:
You must provide either a file or content parameter.
The content of the file can either be posted using an enctype of multipart/form-data (with the file parameter named file), in the usual way that files are uploaded via the browser, or the content of the file can be sent as a POST var called content. The latter should be used for creating a "file" from a long message/paste and forces "editable" mode.
In both cases, the type of data in the file will be intuited from the
filename and the magic bytes in the file, for supported formats.
I could use alternatives like just saving the attachments in blobs and use Azure functions to send the file, but I want to understand what's the limitations before changing the method.
Any clue?

Sharefile folder shows different files as one

There are 3 files uploaded to Sharefile using Sharefile API. 2 of which are PDF's and named slightly different. Since 11/2/2020 both the pdf's are showing up uploaded as one (the names are concatenated with a comma) they used to be uploaded as 2 different files as their filenames are appended with _App or _Qte at the end. Example: Jane_Doe_100-1_HO_10-20-2020_App-8625.pdf,Jane_Doe_100-1_HO_10-20-2020_Qte-8625_4112020082002.pdf
Is anybody else have this problem. Sharefile support has been contacted and they said they do not support API calls.
We haven't made any changes to the code. Thanks in advance for the help.
What I had to do was split the upload proc into 2 and upload each file separately. Although Sharefile API allows\uses Multipart Form DataStream to enable sending files of the same type/extension as part of one stream, it seems they no longer upload the files separately. So I had to upload both the pdf files seperately.

How to get audio object from file_id in Telegram?

For a file uploaded to Telegram, I have a file_id and I can download it. But when the file is sent originally, there is an audio object available that has more metadata than the file (for example, title, performer, etc.). Is there a way to get this information again by having just the file_id?
You can use sendAudio method to send the audio to a chat and the response will contain all the attached details.
Unfortunately, it is not possible to get such information from the file_id only. No one knew what was contained in the file_id until recently when a couple of individual managed to 'crack' it. You can check what is in a file_id using this. So a file_id is just a representation of the location of a file on the Telegram servers which contains a little bit of information such as data center, location of file, a hashed checksum which further contains original uploader etc.

How can I get the number of pages in a PDF file inside a Google Apps Script?

The PDF files are in my Google Drive. I just got that I can get the number of pages of a PDF sending it as a job to Cloud Print, because Cloud Print will return a JSON object with "numberOfPages" attribute.
Is there any faster and easier way? Thanks in advance.
Well, I actually did like I said.
I take my file, then send it to CloudPrint via its JSON Api. I need to send it to a dummy printer, a printer I registered in CloudPrint but actually is never connected to Internet. Then, I get the number of pages of the PDF file in the value of response's "numberOfPages" attribute. Save this number in some var. Finally, I send a delete petition to JSON Api for my file in the dummy printer, indeed isn't necessary at all.
Thanks!

Requesting header information of a file

is there anyway I can request only the header information of any media. For example I just want to request header information of any video file so as to find its video length. I tried using ffmpeg -i {video_url} and did the work but I noticed that it actually downloads the given media in local storage and returns back the header information which obviously increases roundtrip time.
So I would really appreciate if there is any idea for finding the length of media in a fly. BTW I have a ruby on rails application where I need to implement this.
You could try with ffprobe -show_format. ffprobe comes with ffmpeg, and should have been compiled and installed along with it.
You can also try mediainfo. you can download it from: http://mediainfo.sourceforge.net/en.
There is also a wrapper gem for mediainfo but it didn't work well for me. I just used:
response = '#{mediainfo_path} #{source.path} --output=json 2>&1'
and you can then search the response for the properties you want such as "duration" etc.