This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

1import { BskyAgent } from "@atproto/api"; 2import * as dotenv from "dotenv"; // Import dotenv for loading environment variables 3import { CronJob } from "cron"; // Import CronJob for scheduling tasks 4import * as process from "process"; // Import process for accessing environment variables 5import { generateWolfNoiseString } from "./wolf-noise-generator"; 6 7// Load environment variables from the config.env file 8dotenv.config({ path: "./src/config.env" }); 9 10// Create a Bluesky Agent 11const agent = new BskyAgent({ 12 service: "https://bsky.social", 13}); 14 15// Main function for generating and posting wolf noise strings 16async function main() { 17 console.log("Main function called."); 18 19 // Check for empty environment variables and abort if needed 20 if (!process.env.BLUESKY_USERNAME || !process.env.BLUESKY_PASSWORD) { 21 console.error( 22 "Missing required environment variables: BLUESKY_USERNAME and BLUESKY_PASSWORD. Aborting script." 23 ); 24 return; 25 } 26 27 try { 28 // Login to Bluesky 29 await agent.login({ 30 identifier: process.env.BLUESKY_USERNAME!, 31 password: process.env.BLUESKY_PASSWORD!, 32 }); 33 console.log("Logged in to Bluesky."); 34 35 // Generate a random wolf noise string 36 let randomNoise; 37 do { 38 randomNoise = generateWolfNoiseString(); 39 } while (randomNoise.trim().length === 0); // Loop until a non-empty string is generated 40 41 // Post the generated string to Bluesky 42 if (randomNoise) { 43 await agent.post({ 44 text: randomNoise.trim(), // Use the generated string (trimmed) 45 langs: ["en-US"], 46 createdAt: new Date().toISOString(), 47 }); 48 console.log("Just posted:", randomNoise.trim()); // Log the posted string 49 } else { 50 console.log("Failed to generate a valid wolf noise string after multiple attempts."); 51 } 52 } catch (error) { 53 console.error("Error during posting:", error); 54 // You can optionally implement retry logic or notify someone here 55 } 56} 57 58// Function to generate a random delay before the next post 59function getRandomDelay() { 60 const minHours = 1; // Minimum hours for delay 61 const maxHours = 3; // Maximum hours for delay 62 63 // Convert hours to seconds 64 const minDelaySeconds = minHours * 60 * 60; 65 const maxDelaySeconds = maxHours * 60 * 60; 66 67 // Generate a random number within the desired range (inclusive) 68 const randomDelay = 69 Math.floor(Math.random() * (maxDelaySeconds - minDelaySeconds + 1)) + 70 minDelaySeconds; 71 72 return randomDelay; 73} 74 75// Schedule a cron job to run the main function periodically 76const scheduleExpression = "0 * * * *"; // Every hour (used as a base for randomisation) 77 78const job = new CronJob(scheduleExpression, async () => { 79 console.log("Cron job triggered."); 80 // Calculate a random delay before running the main function 81 const delay = getRandomDelay(); 82 console.log(`Next post scheduled in approximately ${(delay / 3600).toFixed(2)} hours.`); 83 84 // Wait for the random delay 85 await new Promise((resolve) => setTimeout(resolve, delay * 1000)); 86 87 // Run the main function 88 await main(); // Ensure to await the main function 89}); 90 91// Start the cron job 92console.log("Starting the cron job."); 93job.start();