Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const https = require("https");
- //import https from https
- function sendData(url, data) {
- const postData = JSON.stringify(data);
- console.log("SEND DATA");
- const options = {
- method: "GET",
- headers: {
- "Content-Type": "application/json",
- "Content-Length": postData.length,
- },
- };
- const req = https
- .request(url, options, (res) => {
- const { statusCode } = res;
- //console.log("STATUS CODE", statusCode == 200);
- let error;
- if (statusCode != 200) {
- console.log("error");
- error = new Error(`Request Failed. Status Code: ${statusCode}`);
- } else {
- console.log("NOT ERROR");
- }
- if (error) {
- console.log(error.message);
- // Consume the response data to free up memory
- res.resume();
- return;
- } else {
- console.log("NOT ERROR ");
- }
- res.setEncoding("utf8");
- let rawData = postData;
- res.on("data", (chunk) => {
- rawData += chunk;
- });
- res.on("end", () => {
- try {
- // Parse the data, if needed
- console.log("Data sent successfully");
- console.log("Response:", rawData);
- // You can do something with the data here if required
- } catch (e) {
- console.error("Error parsing response:", e.message);
- }
- });
- })
- .on("error", (e) => {
- console.error(`Error making request: ${e.message}`);
- });
- // Write data to request body
- req.write(postData);
- req.end();
- // console.log(req)
- }
- exports.handler = async (event, context = null) => {
- const EXAM_DATE = new Date(2023, 12, 10);
- const start = new Date(2023, 11, 24);
- const SECONDS_IN_DAY = 24 * 60 * 60 * 1000;
- let n = parseInt(Math.random() * 200) + 100;
- if (context != null) {
- n = context.awsRequestId.substr(1, 4);
- }
- const diff = Math.abs((EXAM_DATE - start) / SECONDS_IN_DAY);
- const new_title = `PES EMBRAER - ${diff} dias (${n})`;
- const ENDPOINT_URL = `https://api.telegram.org/bot${process.env.BOT_TOKEN}/setChatTitle`;
- let dataToSend = { chat_id: "@embraerpes", title: new_title };
- console.log(ENDPOINT_URL, "\n", dataToSend);
- sendData(ENDPOINT_URL, dataToSend);
- const response = {
- statusCode: 200,
- body: JSON.stringify(dataToSend),
- };
- return response;
- };
- exports.handler("");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement