How to Send a document via Telegram Bot API - bots

I wanted to send a Document via Telegram bot API and my document is stored locally. I tried:
https://api.telegram.org/botToken/sendDocument?chat_id=-5278798&document=C:\Users\Administrator\Desktop\file.txt
But I got Error...
Bad Request: failed to get HTTP URL content
This method only works in remote URLs, how to send documents via bot API?

Using python-telegram-bot you can send a local file using send_document method
c_id = '000011'
filename = '/tmp/googledoc.docx'
context.bot.send_document(chat_id='c_id, document=open('googledoc.docx', 'rb'), filename="googledoc.docx")

You can't use files local from your pc as I assume you have done via the document=C:\Users\Administrator\Desktop\file.txt in your URL
On the docs it states "Pass a file_id to send a file that exists on the Telegram servers (recommended), pass an HTTP URL for Telegram to get a file from the Internet." Telegram media Documents
Meaning you either have to use a file on the telegram servers or upload it to a website and send it via the URL you have uploaded it too.

Related

How to download a file from a node server, using a react client, but hiding the filename and the path

I have a client/server architecture:
the client is running a reactjs & javascript application;
the server is running a nodejs application with a redis database.
The application start with the client asking the user for his/her email
then pass the email to the server which activates a two factor authentication sending a code to the user's phone.
the server send the user's phone a random code
the user insert the received code into the client and send back to the server
the server check the code and, if correct, send to the client a list of the user's owned files.
The client display a list of files (any kind, any extension), each with a connected button.
when the user push the button, I would like to send to the server just the user's email and the selected file name.
The server must then build the real path and is then able to retrieve the file and return it to the server which should store it as in any normal download (maybe asking the user in which location it want to store the file)
I'm using Axios on the client and express on the server.
I'm not able to realize this architecture:
If I use an 'a' tag, as suggested in several sites, it seems that the entire download is managed by the client, so I have to put the entire path of the file to download, and I don't want to show it.
Have someone an idea on how to approach and solve the problem?
Thank you
Paolo

How to force download a file using node js?

My concept is like that,I will send a request from frontend along with few params to backend (nodejs, express). Backend will check params info then will generate a downloadable link that user will be able to download without knowing the downloadable link.
I am expecting,
User will send: www.something.com/param?key=23232323
Backend will verify the key and will generate www.something.com/download/image.zip (that should be unknown by user).
User will get image.zip file. That download link should not be exposed.
Any other idea is appreciable.

How to download multiple files using ExpressJS one API call

I'm quite new to expressjs and I'm developing a web application which acts as an API application. There is a react frontend application also. When a button is clicked in the client app it will send an API call to the backend app and will download a file. That scenario is working fine.with the following code.
const file = `${__dirname}/upload-folder/dramaticpenguin.MOV`;
res.download(file); // Set disposition and send it.
});
But now I have a requirement to download multiple files from a button click. How can I do that .
Can someone help me here.
An HTTP response can only have one file. Really, "downloading a file" in HTTP means serving a response with a Content-Disposition: attachment header, to hint to the client that this response should be saved to the filesystem instead of rendered in the browser.
To download multiple files, you want the client code to initiate multiple HTTP requests (probably to different URLs), and the server can respond to each request with a different file. Note that many browsers will refuse to download multiple files in response to a single user action (for fear of flooding the user with unwanted files) or will at least prompt for confirmation before doing so.
If you cannot change your client-side code to make multiple requests, you will need to package your files inside a single file archive.

Script in server saving locally

I wrote a script that is using slack API to parse AWS S3 files looking for strings or samples. As this is in testing, I'm using my local machine and ngrok to forward localhost traffic.
The thing is that the generated files are getting stores in my machine and will be stored in server once the script is ready for production.
Ideally, I'd like to avoid users needing to grab files from server. Do you think it's possible to store directly in user local machine?
No. Slack does not allow you to access the local machine of their users through a Slack app / API.
Solution 1: Download via browser
The easiest solution would be to offer a direct download link in a Slack message, e.g. via a Link button. Once the user clicks it he is prompted to download the file to his local machine.
Here is an example from one of my apps:
And once you click it you get this window:
To enable downloading via browser you need to set appropriate headers and send the file contents to the browser.
One approach is to have a helper script for the actual download and include a link to the helper script in the link button (you may also want to include some parameters in the link that defines what which file is downloaded).
The helper script then does the following:
Fetch the file to be downloaded (e.g. an PNG image)
Set headers to enable downloading via browser
Send the file to the browser
Here is an example in PHP:
<?php
$filename = "demo.png";
$file = file_get_contents($filename);
header('Content-Disposition: attachment;filename=' . $filename);
header('Content-Type: image/png');
echo $file;
die();
For more infos on download headers see also this answer on SO.
Solution 2: Upload to Slack
Alternatively you could upload the file to the user's Slack workspace via file.upload API method. That way the user does not need to download anything and and you can remove the file from your server after your app has finished processing.

Rest API - Uploading large files to S3 using Pre-signed URLs and updating backend on success

Context
I am building Stateless REST APIs for a browser-based platform that needs to store some user-generated files. These files could potentially be in the GBs.
I am using AWS S3 for storage. I have used AWS SDK in the past for this to route the file uploads through the NodeJS server (Basically - Upload to Server, Server uploads to S3).
I am trying to figure out how to improve it using the Pre-signed urls. I understand the dynamics and the flow on how to get the presigned urls and how to upload the file to S3 directly.
I cannot use SQS or Lambda to trigger object created event.
The architecture needs to be AWS independent.
Question
The simplest of flows I need to achieve is pretty common -
User --> Opens Profile
Clicks Upload Photo
Client Sends Request to /getSignedUrl
Server Returns the signedURL for the file name/type
The client executes the PUT/POST request to upload the file to the signedUrl
Upload Successful
After this - my understanding is -
Client tells the server - File Uploaded Successfully
Server associates the S3 Url for the Photo to the User.
...and that's my problem. How do I associate the successfully uploaded file back to the user on the server in a secure way?
Not sure what I've been missing. It seems like a trivial use case but I haven't been able to find anything regarding it.
1/ I think for the avatar, you should set it as public-read.
When create signed upload url in the
GET: /signed-upload-url
You need to set the image as public-read. After that you are free to interact with image through the direct url. Since this is an avatar, so you can compress it, reduce the size of image by the AWS Lambda function.
2/ If you don't want to have the public-read, you need to associate with server to get signed-download-url to interact with image
GET: /signed-download-url

Resources