8

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”}

EzLo
  • 13,780
  • 10
  • 33
  • 38
JennyB
  • 81
  • 1
  • 1
  • 2

3 Answers3

12

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
Ivan Vinogradov
  • 4,269
  • 6
  • 29
  • 39
  • 2
    I want to add that if the chat is a public one i.e. "@mychannel" it will not work unless we change the -F for --form-string for specifying the chat_id. Like this: curl -v --form-string "chat_id=@mychannel" -F document=@/Users/$USER/Desktop/file.txt api.telegram.org/bot/sendDocument – Josep Alsina Feb 13 '20 at 13:23
6

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

Donny
  • 516
  • 3
  • 9
0

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)
]);
Giangimgs
  • 952
  • 1
  • 12
  • 16