Skip to content

Instantly share code, notes, and snippets.

@icy-r
Last active October 3, 2023 14:38
Show Gist options
  • Select an option

  • Save icy-r/3eaea8009b842ddf983e06bb2e5e62f1 to your computer and use it in GitHub Desktop.

Select an option

Save icy-r/3eaea8009b842ddf983e06bb2e5e62f1 to your computer and use it in GitHub Desktop.
const { getJson, bot } = require('../lib/');
const https = require('https'); // Import the built-in 'https' module
const unsplashAccessKey = 'HEsvJje0sWH7iUlXgAy_sRXoOJ9yiNmm9Y04bczGBFs'; // Replace with your Unsplash API access key
let autoChangeProfile = false; // Flag to control automatic profile changes
let changeInterval; // Interval for automatic profile changes
let currentKeywords = ''; // Current keywords for automatic changes
// Function to fetch an Unsplash image URL based on keywords
const fetchUnsplashImage = async (keywords) => {
try {
const response = await new Promise((resolve, reject) => {
https.get(`https://api.unsplash.com/photos/random?query=${keywords}&client_id=${unsplashAccessKey}`, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve(data);
});
}).on('error', (error) => {
reject(error);
});
});
const data = JSON.parse(response);
if (!data.urls || !data.urls.regular) {
throw new Error('Invalid response from Unsplash API');
}
return data.urls.regular;
} catch (error) {
console.error('Error fetching image from Unsplash:', error);
return null;
}
};
// Function to update the WhatsApp profile picture
const updateProfilePicture = async (imageUrl, message) => {
try {
const response = await getJson(imageUrl);
if (!response) {
return await message.send('Failed to fetch the image.');
}
// Save the image locally
const imageBuffer = Buffer.from(response, 'binary');
await message.sendImage(imageBuffer, 'image.jpg'); // Send the image as a message
// Update the WhatsApp profile picture with the downloaded image
await message.updateProfilePicture('image.jpg');
await message.send('Profile picture updated.');
} catch (error) {
console.error('Error updating profile picture:', error);
await message.send('Failed to update the profile picture.');
}
};
bot(
{
pattern: 'randompp ?(.*)',
fromMe: true,
desc: 'Change profile picture based on Unsplash image',
type: 'user',
},
async (message, match) => {
const command = match.toLowerCase();
if (command === 'off') {
// Turn off automatic profile changes
clearInterval(changeInterval);
autoChangeProfile = false;
await message.send('Automatic profile picture changes have been stopped.');
return;
} else if (command) {
// Set new keywords and start automatic changes
autoChangeProfile = true;
currentKeywords = command;
await message.send('Automatic profile picture changes are enabled with keywords: ' + command);
// Fetch and set the initial profile picture
const imageUrl = await fetchUnsplashImage(currentKeywords);
if (imageUrl) {
await updateProfilePicture(imageUrl, message);
}
// Set up an interval to change the profile picture every 2 hours
changeInterval = setInterval(async () => {
const imageUrl = await fetchUnsplashImage(currentKeywords);
if (imageUrl) {
await updateProfilePicture(imageUrl, message);
}
}, 2 * 60 * 60 * 1000); // 2 hours in milliseconds
return;
}
if (!autoChangeProfile) {
await message.send('Please provide keywords to start automatic profile picture changes or use "off" to stop them.');
return;
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment