Advertisement
romerlrl

counter_to_december

Nov 24th, 2023
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const https = require("https");
  2. //import https from https
  3.  
  4. function sendData(url, data) {
  5.   const postData = JSON.stringify(data);
  6.   console.log("SEND DATA");
  7.   const options = {
  8.     method: "GET",
  9.     headers: {
  10.       "Content-Type": "application/json",
  11.       "Content-Length": postData.length,
  12.     },
  13.   };
  14.  
  15.   const req = https
  16.     .request(url, options, (res) => {
  17.       const { statusCode } = res;
  18.       //console.log("STATUS CODE", statusCode == 200);
  19.       let error;
  20.       if (statusCode != 200) {
  21.         console.log("error");
  22.         error = new Error(`Request Failed. Status Code: ${statusCode}`);
  23.       } else {
  24.         console.log("NOT ERROR");
  25.       }
  26.  
  27.       if (error) {
  28.         console.log(error.message);
  29.         // Consume the response data to free up memory
  30.         res.resume();
  31.         return;
  32.       } else {
  33.         console.log("NOT ERROR ");
  34.       }
  35.  
  36.       res.setEncoding("utf8");
  37.       let rawData = postData;
  38.  
  39.       res.on("data", (chunk) => {
  40.         rawData += chunk;
  41.       });
  42.  
  43.       res.on("end", () => {
  44.         try {
  45.           // Parse the data, if needed
  46.           console.log("Data sent successfully");
  47.           console.log("Response:", rawData);
  48.  
  49.           // You can do something with the data here if required
  50.         } catch (e) {
  51.           console.error("Error parsing response:", e.message);
  52.         }
  53.       });
  54.     })
  55.     .on("error", (e) => {
  56.       console.error(`Error making request: ${e.message}`);
  57.     });
  58.  
  59.   // Write data to request body
  60.   req.write(postData);
  61.   req.end();
  62.   // console.log(req)
  63. }
  64.  
  65. exports.handler = async (event, context = null) => {
  66.   const EXAM_DATE = new Date(2023, 12, 10);
  67.   const start = new Date(2023, 11, 24);
  68.   const SECONDS_IN_DAY = 24 * 60 * 60 * 1000;
  69.   let n = parseInt(Math.random() * 200) + 100;
  70.   if (context != null) {
  71.     n = context.awsRequestId.substr(1, 4);
  72.   }
  73.   const diff = Math.abs((EXAM_DATE - start) / SECONDS_IN_DAY);
  74.   const new_title = `PES EMBRAER - ${diff} dias (${n})`;
  75.   const ENDPOINT_URL = `https://api.telegram.org/bot${process.env.BOT_TOKEN}/setChatTitle`;
  76.   let dataToSend = { chat_id: "@embraerpes", title: new_title };
  77.   console.log(ENDPOINT_URL, "\n", dataToSend);
  78.   sendData(ENDPOINT_URL, dataToSend);
  79.   const response = {
  80.     statusCode: 200,
  81.     body: JSON.stringify(dataToSend),
  82.   };
  83.   return response;
  84. };
  85.  
  86. exports.handler("");
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement