How do I download a file or photo that was sent to my Telegram bot? - api

I am using the telegram bot API but I cant see anyway to download a filé that was sent to my bot. I get a hash of the file but dont know what to do with it. Is there any way? Thanks.

This is now available!
https://core.telegram.org/bots/api#getfile
Hooray! It was added on Sep 18th (2015):
https://core.telegram.org/bots/api
Usage:
In the JSON of the message you will receive a file_id as before. An example of a message object with a voice file:
{
message_id: 2675,
from: {
id: 10000001,
first_name: 'john',
username: 'john'
},
chat: {
id: 10000001,
first_name: 'john',
username: 'john'
},
date: 1442848171,
voice: {
duration: 2,
mime_type: 'audio/ogg',
file_id: 'AwADBAADYwADO1wlBuF1ogMa7HnMAg', // <------- file_id
file_size: 17746
}
}
Via the API's getFile you can now get the required path information for the file:
https://api.telegram.org/bot<bot_token>/getFile?file_id=the_file_id
This will return an object with file_id, file_size and file_path. You can then use the file_path to download the file:
https://api.telegram.org/file/bot<token>/<file_path>
Note that this link will only be available for an hour. After an hour you can request another link. This means that if you want to host the file somehow and you rather avoid checking and re-checking for fresh links every time you serve it you might be better off downloading the file to your own hosting service.
The maximum size of a file obtained through this method is 20MB.
Error: Obtained when a file large than 20mb is used.(Shown below)
{"ok":false,"error_code":400,"description":"Bad Request: file is too big[size:1556925644]"}
From telegram's docs:
On success, a File object is returned. The file can then be downloaded via the link https://api.telegram.org/file/bot/<file_path>, where <file_path> is taken from the response. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by calling getFile again.For the moment, bots can download files of up to 20MB in size.

Yay! It's just added at September 18, 2015
You can use getFile(file_id). This function returns a File object containing file_path. You can download the file through this address:
https://api.telegram.org/file/bot<token>/<file_path>
As mentioned in Telegram Bot API Documentation, the File object will be valid for about one hour. You should call getFile again to get a new File object if the old one expires.

If you are using pyTelegramBotAPI you can download your photo using this code:
raw = message.photo[2].file_id
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)

The method to work with files is not available yet.
Source: telegram on twitter
https://twitter.com/telegram/status/614468951926509568

If you have the file_id then you need to use the sendDocument or sendPhoto methods, if you want to send to yourself, you need to tell your bot your user id or your chat id (the same in one-to-one chat).

Also, pay attention, that Telegram api (by webhook) provides thumbs prop, for images and gifs it will provide thumbnail of file. To get source file, you need to check root object file_id.

Related

Getstream.io throws exception when using the "to" field

I have two flat Feed Groups, main, the primary news feed, and main_topics.
I can make a post to either one successfully.
But when I try to 'cc' the other using the to field, like, to: ["main_topics:donuts"] I get:
code: 17
detail: "You do not have permission to do this, you got this error because there are no policies allowing this request on this application. Please consult the documentation https://getstream.io/docs/"
duration: "0.16ms"
exception: "NotAllowedException"
status_code: 403
Log:
The request didn't have the right permissions or autorization. Please check our docs about how to sign requests.
We're generating user tokens server-side, and the token works to read and write to both groups without to.
// on server
stream_client.user(user.user_id).create({
name: user.name,
username: user.username,
});
Post body:
actor: "SU:5f40650ad9b60a00370686d7"
attachments: {images: [], files: []}
foreign_id: "post:1598391531232"
object: "Newsfeed"
text: "Yum #donuts"
time: "2020-08-25T14:38:51.232"
to: ["main_topics:donuts", "main_topics:all"]
verb: "post"
The docs show an example with to: ['team:barcelona', 'match:1'], and say you need to create the feed groups in the panel, but mention nothing about setting up specific permissions to use this feature.
Any idea why this would happen? Note that I'm trying to create the new topics (donuts, all) which don't exist when this post is made. However, the docs don't specify that feeds need to be explicitly created first - maybe that's the missing piece?
If you haven’t already tried creating the feed first, then try that. Besides that, the default permissions restrict a user from posting on another’s feed. I think it’s acceptable to do this if it’s a notification feed but not user or timeline.
You can email the getstream support to change the default permissions because these are not manageable from the dashboard.
Or you can do this call server side as an admin permissions.

Sending files using pure HTTP request using telegram bot

hello everyone I was tring to send files using my bot like http://api.telegram.org/botTOKEN/sendDocument?document=http://my_path&chat_id but it ain't support .txt .docx..... and other formats..... any help please
According to https://core.telegram.org/bots/api#sending-files
Sending by URL In sendDocument, sending by URL will currently only
work for gif, pdf and zip files.
You may try to use this approach
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.
This answer might give you some ideas on how to do that.
UPDATE
It is also good idea to use someone's library to understand how it works there.
For example, I use longman/telegram-bot from this repo. There is encodeFile method in Request class.
Method is follows:
public static function encodeFile($file)
{
$fp = fopen($file, 'rb');
if ($fp === false) {
throw new TelegramException('Cannot open "' . $file . '" for reading');
}
return $fp;
}
Which means simple fopen method with 'rb' parameter is enough to convert file.

Are there examples of itineraries that are compatible with the Amadeus self-service trip parse API?

I've tried booking references from a dozen providers (which I don't want to post for privacy reasons) and every time the API returns 'Unable to parse' but with no additional diagnostic information.
As a self-service API they don't offer support through any channel other than Stack Overflow, but I'm hoping someone has successfully used the endpoint.
I'm mostly using GMail to access sample flight booking emails, then selecting "View Original" to download the original MIME format email
This is what I use to read the .eml file into code:
function base64_encode(file) {
// read binary data
var bitmap = fs.readFileSync(file);
// convert binary data to base64 encoded string
return new Buffer(bitmap).toString('base64');
}
However every single email I submit to the endpoint eventually returns:
data:
{ data:
{ type: 'trip-parser-job',
id: 'REDACTED',
self: [Object],
status: 'ERROR',
detail: 'Unable to parse' } } }
and at this point, I'm starting to think that either the API is broken, or they haven't correctly documented what data should be submitted as content. I've decoded the sample document they provide and can't see any major difference between that and my inputs.
Does someone have either some working samples that the API was able to process, or some NodeJS code which seems to reliably get a result from the API?

Downloading a publicly-shared file from OneDrive

When I create a share link in the UI with the "Anyone with this link can view this item" option, I get a URL that looks like https://onedrive.live.com/redir?resid=XXX!YYYY&authkey=!ZZZZZ&ithint=<contentType>. What I can't figure out is how to use this URL from code to download the content of the file. Hitting the link gives HTML for a page to show the file.
How can I construct a call to download the file? Also, is there a way to construct a call to get some (XML/JSON) metadata about the file, and maybe even a preview or something? I want to be able to do this all without prompting a user for credentials, and all the API docs are about how to make authenticated calls. I want to make anonymous calls to get publicly-shared files.
Have a read over https://dev.onedrive.com - it documents how you can make a query to our service to get the metadata for an item, along with URLs that can be used to directly download the content.
Update with more details
Sorry, the documentation you need for your specific scenario is still in process (along with the associated SDK changes) so I'll give you an overview of how to do it.
There's a sibling to the /drives path called /shares which accepts a sharing URL (such as the one you have above) in an encoded format and allows you to get metadata for the item it represents. This does not require authentication provided the sharing URL has a valid authkey.
The encoding scheme for the id is u!<UrlSafeBase64EncodedUrl>, where <UrlSafeBase64EncodedUrl> follows the guidelines outlined here (trim the = characters from the end).
Here's a snippet that should give you an idea of the whole process:
string originalUrl = "https://onedrive.live.com/redir?resid=XXX!YYYY&authkey=!foo";
byte[] urlAsUtf8Bytes = Encoding.UTF8.GetBytes(originalUrl);
string utf8BytesAsBase64String = Convert.ToBase64String(urlAsUtf8Bytes);
string encodedUrl = "u!" + utf8BytesAsBase64String.TrimEnd('=').Replace('/', '_').Replace('+', '-');
string metadataUrl = "https://api.onedrive.com/v1.0/shares/" + encodedUrl + "/root";
From there you can append /content if you want to get the contents of the file, or you can start navigating through if the URL represents a folder (e.g. /children/childfile.txt)

Vb.Net Gmail API Send Message with attachment > 5Mb

On Gmail Api documentation i read that i've to make an HTTP request at "/upload/gmail/v1/users/userId/messages/send" when sending message larger then 5mb but i don't find any example how to implement this using the Client Library in .net
All the examples on the site refer to the "messages.Send" function that takes as parameter a raw message and the user id but i saw that there's another overload that also take the stream of the content to upload and the content type of it.
The problem is that i've no clue how to correctly call it.
Does anyone have successfully achived it ?
Thx for your answer
Simone
Simone, it's means that you use simple upload:
uploadType=media. For quick transfer of smaller files, for example, 5 MB or less.
You must use Multipart upload or Resumable upload (https://developers.google.com/gmail/api/guides/uploads)
You can send a post query with payload (see CURLOPT_POSTFIELDS, if use a CURL) on the https://www.googleapis.com/gmail/v1/users/me/messages/send?access_token=your_access_token&uploadType=multipart. Payload must contain json encoded message. Structure of this message for example:
$message = [
'message' => [
'raw' => str_replace(['+', '/'], ['-', '_'], base64_encode($mimeString)),
'threadId' => $yourThreadId
]
];
Variable $mimeString must contain correct mime string