How Can I Send a Downloaded PDF to a Telegram Channel Using an RSS Trigger Instead of Sending a URL?

This topic was automatically generated from Slack. You can find the original thread here.

i have an rss trigger that send a url to a pdf file into its entries, how can i send in telegram channel the already downloaded pdf instead the url to view it ?

To send a PDF file to a Telegram channel instead of the URL, you’ll need to modify your Pipedream workflow to download the PDF from the URL provided by the RSS trigger and then use the Telegram Bot API to send the downloaded PDF to your channel. Here’s a step-by-step guide on how you can achieve this: 1. RSS Trigger: Your workflow will start with an RSS trigger that watches for new entries, which include URLs to PDF files. 2. Download the PDF: Next, you’ll need to add a Node.js code step to download the PDF file from the URL provided by the RSS trigger. You can use the axios library to make the HTTP request to download the file and save it temporarily in the /tmp directory. 3. Send the PDF to Telegram Channel: Finally, you’ll use the Telegram Bot API to send the downloaded PDF to your Telegram channel. You’ll need to use the sendDocument method of the Telegram Bot API, specifying the chat ID of your channel and the file you’ve downloaded. Here’s an example code snippet that demonstrates steps 2 and 3: ``` import { axios } from “@pipedream/platform” export default defineComponent({ props: { rss: { type: “app”, app: “rss”, }, telegram_bot: { type: “app”, app: “telegram_bot”, } }, async run({ steps, $ }) { // Step 2: Download the PDF from the URL provided by the RSS trigger const pdfUrl = steps.trigger.event.body.pdfUrl; // Replace with the actual path to the URL in the RSS trigger data const response = await axios($, { url: pdfUrl, responseType: “arraybuffer”, }); const pdfData = response.data; const filePath = /tmp/downloaded.pdf; await $fs.writeFile(filePath, pdfData); // Step 3: Send the PDF to the Telegram channel const formData = new $multipart.FormData(); formData.append(“chat_id”, “@your_channel_id”); // Replace with your channel ID formData.append(“document”, $fs.createReadStream(filePath)); await axios($, { method: “POST”, url: https://api.telegram.org/bot${this.telegram_bot.$auth.token}/sendDocument, // Replace with your bot token headers: { …

How can i save the file to use it in the next step ?

To save a file and use it in the next step of your Pipedream workflow, you can write the file to the /tmp directory, which is accessible from your workflow steps for saving and retrieving files. Here’s how you can do it in a Node.js code step: import { writeFile } from "fs/promises"; export default defineComponent({ async run({ steps, $ }) { // Example content to save const content = "Hello, world!"; // Specify the file path in the /tmp directory const filePath = "/tmp/myfile.txt"; // Write the content to the file await writeFile(filePath, content); // Log the path for debugging purposes console.log(`File saved to ${filePath}`); // Return the file path to use it in the next step return filePath; }, }); In this example, we’re using the fs/promises module to write a file asynchronously. We save a simple string "Hello, world!" to a file named myfile.txt in the /tmp directory. The file path is then logged and returned from the step, so you can use it in subsequent steps of your workflow. To use the file in the next step, you can access it using the path returned from the previous step. For example, if you want to read the file content in the next step, you can do something like this: import { readFile } from "fs/promises"; export default defineComponent({ async run({ steps, $ }) { // Get the file path from the previous step const filePath = steps.previousStepName.result; // Read the content of the file const content = await readFile(filePath, "utf8"); // Log or use the content as needed console.log(content); }, }); Replace steps.previousStepName.result with the actual reference to the result of your previous step, where previousStepName is the name of the step that saved the file. This way, you can seamlessly pass data between steps in your Pipedream workflow.

Axios sent me “unable to verify the first certificate”

When you encounter the “unable to verify the first certificate” error with Axios, it typically means that Axios is unable to trust the SSL certificate presented by the server. This can happen if the server is using a self-signed certificate or if there is an issue with the certificate chain. To bypass this error in a Pipedream workflow, you can disable SSL certificate verification for the specific request. However, please be aware that disabling SSL verification can make your application vulnerable to man-in-the-middle attacks. Use this approach only if you understand the risks and it’s for a trusted internal network or a testing environment. Here’s how you can modify your Axios request to ignore SSL certificate errors: import { axios } from "@pipedream/platform"; export default defineComponent({ async run({ steps, $ }) { return await axios($, { url: "https://your-secure-server.com/api", method: "GET", // Add this to ignore SSL certificate errors httpsAgent: new $.node.https.Agent({ rejectUnauthorized: false, }), }); }, }); Please replace "https://your-secure-server.com/api" with the actual URL you’re trying to access. Remember, this is not recommended for production use due to security concerns. If you’re encountering this issue in a production environment, it’s better to resolve the certificate issues on the server or ensure that the certificate chain is correctly installed and trusted.

with python how can i save a pdf from a url and use it in the next step ?

To save a PDF from a URL and use it in the next step within a Pipedream workflow using Python, you can follow this approach. This example demonstrates how to download a PDF file to the /tmp directory, which you can then reference in subsequent steps of your workflow. import requests def handler(pd: "pipedream"): # Specify the URL of the PDF file pdf_url = "https://example.com/path/to/your/file.pdf" # Define the path where the PDF will be saved pdf_path = "/tmp/downloaded_file.pdf" # Download the PDF file to /tmp with requests.get(pdf_url, stream=True) as response: # Check if the request was successful response.raise_for_status() # Open the new file in binary write mode with open(pdf_path, "wb") as file: for chunk in response.iter_content(chunk_size=8192): # Write the chunk to file file.write(chunk) # Now the PDF is saved to /tmp/downloaded_file.pdf and can be used in the next steps # For example, you can return the path to use it in the next step return {"pdf_path": pdf_path} This code snippet uses the requests library to download a PDF from a specified URL and saves it to the /tmp directory. The /tmp directory is a writable directory available in Pipedream workflows, making it suitable for temporarily storing files like this. After downloading the file, the path to the saved PDF (pdf_path) is returned, which you can then use in subsequent steps of your workflow. Remember, to use external libraries like requests in your Python code within Pipedream, ensure they are available or import them according to Pipedream’s Python environment constraints.

it gets the error: HTTPSConnectionPool(host=‘foobar’, port=443): Max retries exceeded with url:…(Caused by SSLError(SSLCertVerificationError(1, ‘[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)’)))

It looks like you’re encountering an SSL certificate verification error when trying to establish an HTTPS connection. This error occurs when Python’s requests library (or a similar library) cannot verify the SSL certificate presented by the server you’re trying to connect to. This can happen for a variety of reasons, such as the certificate being self-signed, the root CA not being recognized, or the certificate having expired. While it’s crucial to understand the security implications, if you’re working in a controlled environment (e.g., connecting to a server with a self-signed certificate for testing purposes), you might consider bypassing the SSL verification as a temporary workaround. However, this should be avoided in production environments due to the security risks of not verifying SSL certificates. Here’s how you can bypass SSL certificate verification in a Python requests call. Please note, this is for educational purposes or testing in a safe environment: import requests # WARNING: This disables SSL certificate verification. Use with caution. response = requests.get('https://foobar', verify=False) print(response.text) And if you’re using the http.client library: ```
import http.client # WARNING: This example does not perform SSL certificate verification. Use with caution. conn = http.client.HTTPSConnection(‘foobar’, context=ssl._create_unverified_context()) conn.request(“GET”, “/”) response = conn.getresponse() print(response.read().decode())