UNPKG

choco-bot

Version:

Whatsapp-bot

67 lines (55 loc) 2.27 kB
const puppeteer = require('puppeteer'); const fs = require('fs').promises; async function replaceContent(page, selector, fileContent) { await page.waitForSelector(selector); await page.evaluate((selector, fileContent) => { const element = document.querySelector(selector); if (element) { element.innerHTML = fileContent; } else { console.error(`Element with selector ${selector} not found`); } }, selector, fileContent); } async function submitVote(page, email) { const emailInputSelector = '#cog-36'; const submitButtonSelector = '.cog-button--submit'; await page.type(emailInputSelector, email); await page.$eval(formSelector, (form) => { form.submit(); }); await page.waitForTimeout(5000); } async function temp_vote(remainingMessage, msg){ const browser = await puppeteer.launch(); const page = await browser.newPage(); try { await page.goto('https://www.cognitoforms.com/Themachinery1/VotingPortal'); const formSelector = 'form.cog-cognito.cog-form.cog-6.is-default.cog-cognito--styled.cog-form--dark-background.cog-form--maximal-branding.cog-form--transparent-background'; const divSelector = '.cog-form__container'; const fileContent = await fs.readFile('../val.html', 'utf8'); await replaceContent(page, divSelector, fileContent); const votes = await fs.readFile('../votes.txt', 'utf8'); const emailList = votes.split('\n').filter(Boolean); // Number of votes to submit (adjust as needed) const numberOfVotes = remainingMessage; for (let i = 0; i < numberOfVotes && i < emailList.length; i++) { const currentEmail = emailList[i]; // Submit vote with the current email await submitVote(page, currentEmail); // Remove the used email from the list emailList.shift(); // Update the votes.txt file await fs.writeFile('votes.txt', emailList.join('\n')); console.log(`Vote submitted with email: ${currentEmail}`); } msg.reply('Voting process completed.'); } catch (error) { console.error('Error:', error); } finally { await browser.close(); } } module.exports = { temp_vote, }