Skip to content

Commit

Permalink
Add tweet post function
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillweston committed Jun 4, 2024
1 parent e6f41cc commit 12b2d14
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 59 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@ To successfully deploy and run the project locally, you need to create a `contra
"emailServiceID": "Your_Email_Service_ID",
"emailTemplate": "Your_Email_Template",
"tweetId": "Your_Tweet_Id",
"tweetId2": "Your_Second_Tweet_Id",
"tweetMessage": "Your_Tweet_Message",
"userName": "Your_User_Name",
"checkRetweetEnabled": "false",
"checkRetweet2Enabled": "false",
"checkRetweetEnabled": "true",
"checkTweetEnabled": "true",
"checkLikeEnabled": "true",
"checkFollowEnabled": "true",
"retweetEnabled": "false",
"retweet2Enabled": "false",
"retweetEnabled": "true",
"tweetEnabled": "true",
"likeEnabled": "true",
"followEnabled": "true"
}
Expand Down Expand Up @@ -264,14 +264,14 @@ Here is an example of what your `contract-config.json` might look like for the B
"emailServiceID": "lotso_email",
"emailTemplate": "lotso_email_template",
"tweetId": "0x123abc456def789ghi",
"tweetId2": "0x123abc456def789ghi",
"tweetMessage": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"userName": "0x123abc456def789ghi",
"checkRetweetEnabled": "false",
"checkRetweet2Enabled": "false",
"checkTweetEnabled": "false",
"checkLikeEnabled": "true",
"checkFollowEnabled": "true",
"retweetEnabled": "false",
"retweet2Enabled": "false",
"retweetEnabled": "true",
"tweetEnabled": "true",
"likeEnabled": "true",
"followEnabled": "true"
}
Expand Down Expand Up @@ -299,7 +299,7 @@ This section provides guidance on deploying your project to GitHub Pages and Clo
|**`EMAIL_SERVICE_ID`**|Essential|The service ID for sending emails|
|**`EMAIL_TEMPLATE`**|Essential|The email template name|
|**`TWEET_ID`**|Essential|The tweet ID for the target tweet|
|**`TWEET_ID2`**|Essential|The second tweet ID for the target tweet|
|**`TWEET_MESSAGE`**|Essential|The message for the target tweet|
|**`USER_NAME`**|Essential|The user name for the target user|
|**`CHECK_RETWEET_ENABLED`**|Optional|Indicates if the retweet check is enabled, "false" by default|
|**`CHECK_RETWEET_2_ENABLED`**|Optional|Indicates if the retweet check is enabled, "false" by default|
Expand Down
6 changes: 3 additions & 3 deletions create-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ const config = {
"emailServiceID": process.env.EMAIL_SERVICE_ID,
"emailTemplate": process.env.EMAIL_TEMPLATE,
"tweetId": process.env.TWEET_ID,
"tweetId2": process.env.TWEET_ID_2,
"tweetMessage": process.env.TWEET_MESSAGE,
"userName": process.env.USER_NAME,
"checkRetweetEnabled": process.env.CHECK_RETWEET_ENABLED,
"checkRetweet2Enabled": process.env.CHECK_RETWEET_2_ENABLED,
"checkTweetEnabled": process.env.CHECK_TWEET_ENABLED,
"checkLikeEnabled": process.env.CHECK_LIKE_ENABLED,
"retweetEnabled": process.env.RETWEET_ENABLED,
"retweet2Enabled": process.env.RETWEET_2_ENABLED,
"tweetEnabled": process.env.TWEET_ENABLED,
"likeEnabled": process.env.LIKE_ENABLED,
"followEnabled": process.env.FOLLOW_ENABLED,
};
Expand Down
20 changes: 10 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,11 @@ <h3>Authorize Your Twitter to Unlock</h3>
<div id="retweet-section" class="col-lg-3 col-sm-6 mob-mar-bottom disabled">
<div class="price-table gold-bg">
<h3><sub>Step 1</sub></h3>
<h3 class="gold">Repost Our Tweet #1</h3>
<h3 class="gold">Repost Our Tweet</h3>
<div id="retweet-progress" class="counter-1 progress-cycle" style="display: none;">
<div class="chart" data-percent="0"></div>
<h5>0%</h5>
<span>Reposting Tweet #1</span>
<span>Reposting Tweet</span>
</div>
<div id="retweet-info">
<p><i class="fa fa-check-circle-o" aria-hidden="true"></i> Verified Twitter Account</p>
Expand All @@ -642,26 +642,26 @@ <h5>0%</h5>
<div id="repostMessage"></div>
</div>
</div>
<div id="retweet-section-2" class="col-lg-3 col-sm-6 mob-mar-bottom disabled">
<div id="tweet-section" class="col-lg-3 col-sm-6 mob-mar-bottom disabled">
<div class="price-table">
<h3><sub>Step 2</sub></h3>
<h3>Repost Our Tweet #2</h3>
<div id="retweet-2-progress" class="counter-1 progress-cycle" style="display: none;">
<h3>Post Your Tweet</h3>
<div id="tweet-progress" class="counter-1 progress-cycle" style="display: none;">
<div class="chart" data-percent="0"></div>
<h5>0%</h5>
<span>Reposting Tweet #2</span>
<span>Post Tweet</span>
</div>
<div id="retweet-2-info">
<div id="tweet-info">
<p><i class="fa fa-check-circle-o" aria-hidden="true"></i> Verified Twitter Account</p>
<p><i class="fa fa-check-circle-o" aria-hidden="true"></i> Repost Our Tweet</p>
<p><i class="fa fa-check-circle-o" aria-hidden="true"></i> Post Your Tweet</p>
<p><i class="fa fa-check-circle-o" aria-hidden="true"></i> Task Completed</p>
</div>
<div class="price-btn text-center">
<a href="javascript:void(0);" id="retweet-2"><i class="fa fa-twitter" aria-hidden="true"></i> Repost</a>
<a href="javascript:void(0);" id="tweet"><i class="fa fa-twitter" aria-hidden="true"></i> Post</a>
</div>
</div>
<div class="info-txt middle-align">
<div id="repostMessage2"></div>
<div id="postMessage"></div>
</div>
</div>
<div id="like-section" class="col-lg-3 col-sm-6 mob-mar-bottom disabled">
Expand Down
50 changes: 27 additions & 23 deletions js/claim-airdrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Fireworks } from 'fireworks-js';

// 1. Get a project ID at https://cloud.walletconnect.com
let projectId, activeNetwork, contractAddress, authWebAddress, turnstileSiteKey;
let tweetId, tweetId2, userName;
let tweetId, tweetMessage, userName;
let checkActionsEnabled = {};

// Updated contract ABI to include getAirdropAmount function
Expand Down Expand Up @@ -60,13 +60,13 @@ try {
turnstileSiteKey = jsonConfig.turnstileSiteKey;
projectId = jsonConfig.projectId;
tweetId = jsonConfig.tweetId;
tweetId2 = jsonConfig.tweetId2;
tweetMessage = jsonConfig.tweetMessage;
userName = jsonConfig.userName;

// Access additional properties for Twitter checks and actions
checkActionsEnabled = {
checkRetweet: jsonConfig.checkRetweetEnabled,
checkRetweet2: jsonConfig.checkRetweet2Enabled,
checkTweet: jsonConfig.checkTweetEnabled,
checkLike: jsonConfig.checkLikeEnabled
};

Expand All @@ -75,12 +75,12 @@ try {
throw new Error("Required configuration values (activeNetwork or contractAddress or authWebAddress or turnstileSiteKey or projectId) are missing.");
}

if (!tweetId || !userName || !tweetId2) {
throw new Error("Required configuration values (tweetId or tweetId2 or userName) are missing.");
if (!tweetId || !userName || !tweetMessage) {
throw new Error("Required configuration values (tweetId or tweetMessage or userName) are missing.");
}

if (Object.values(checkActionsEnabled).includes(false)) {
throw new Error("Required configuration values (checkRetweetEnabled, checkRetweet2Enabled, or checkLikeEnabled) are missing.");
throw new Error("Required configuration values (checkRetweetEnabled, checkTweetEnabled, or checkLikeEnabled) are missing.");
}
} catch (error) {
// Check if the error is due to missing file
Expand Down Expand Up @@ -448,7 +448,7 @@ async function checkUserEligibility() {
}

// Check if the user has interacted with the required steps
const twitterCheck = await checkTwitterInteractions(tweetId, tweetId2);
const twitterCheck = await checkTwitterInteractions(tweetId);
if (!twitterCheck.success) {
console.log('Twitter interaction checks failed:', twitterCheck.message);
updateProgressBar(100, 'red');
Expand Down Expand Up @@ -668,28 +668,38 @@ async function checkIfPurchased(address) {
}
}

async function checkTwitterInteractions(tweetId, tweetId2) {
async function checkTwitterInteractions(tweetId) {
try {
let step_cnt = 0;
const actionsMap = {
checkRetweet2: { action: 'retweet', responseKey: 'isRetweeted', tweetId: tweetId2, message: 'Tweet2 has not been retweeted by the user.' },
checkLike: { action: 'like', responseKey: 'isLiked', tweetId: tweetId, message: 'Tweet is not liked by the user.' },
checkRetweet: { action: 'retweet', responseKey: 'isRetweeted', tweetId: tweetId, message: 'Tweet has not been retweeted by the user.' },
checkTweet: { action: 'tweet', responseKey: 'isTweeted', tweetId: null, message: 'Tweet is not posted by the user.' },
checkLike: { action: 'like', responseKey: 'isLiked', tweetId: tweetId, message: 'Tweet is not liked by the user.' },
//checkBookmark: { action: 'bookmark', responseKey: 'isBookmarked', tweetId: tweetId, message: 'Tweet has not been bookmarked by the user.' }
};

let promises = [];
for (let action in actionsMap) {
if (checkActionsEnabled[action] === "true") {
const isActionDone = await checkAction(actionsMap[action].action, actionsMap[action].tweetId, actionsMap[action].responseKey);
console.log(`${action} check:`, isActionDone);
if (!isActionDone) {
console.log(actionsMap[action].message);
} else {
step_cnt++;
let url = `${authWebAddress}/check-${actionsMap[action].action}`;
if (actionsMap[action].tweetId !== null) {
url += `?tweetId=${actionsMap[action].tweetId}`;
}
let promise = fetch(url, { credentials: 'include' })
.then(handleResponse)
.then(response => {
console.log(`${action} check:`, response.data[actionsMap[action].responseKey]);
if (!response.data[actionsMap[action].responseKey]) {
console.log(actionsMap[action].message);
} else {
step_cnt++;
}
});
promises.push(promise);
}
}

await Promise.all(promises);

console.log('Checks passed steps: ', step_cnt);
if (step_cnt === 0) {
return { success: false, step: step_cnt, message: 'No checks passed' };
Expand All @@ -702,12 +712,6 @@ async function checkTwitterInteractions(tweetId, tweetId2) {
}
}

function checkAction(action, tweetId, responseKey) {
return fetch(`${authWebAddress}/check-${action}?tweetId=${tweetId}`, { credentials: 'include' })
.then(handleResponse)
.then(response => response.data[responseKey]);
}

function handleResponse(response) {
if (response.status === 401) {
// Throw an error that specifically handles 401 Unauthorized
Expand Down
35 changes: 22 additions & 13 deletions js/twitter-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ document.addEventListener('DOMContentLoaded', function () {
// Access additional properties if needed
actionsEnabled = {
retweet: jsonConfig.retweetEnabled,
retweet2: jsonConfig.retweet2Enabled,
tweet: jsonConfig.tweetEnabled,
like: jsonConfig.likeEnabled,
follow: jsonConfig.followEnabled
};
Expand All @@ -60,7 +60,7 @@ document.addEventListener('DOMContentLoaded', function () {
const isAnyActionDisabled = Object.entries(actionsEnabled).some(isEnabled => !isEnabled);

if (isAnyActionDisabled) {
throw new Error("Required configuration values (retweetEnabled or retweet2Enabled or likeEnabled or followEnabled) are missing.");
throw new Error("Required configuration values (retweetEnabled or tweetEnabled or likeEnabled or followEnabled) are missing.");
}

// Check if the user connect the wallet
Expand All @@ -83,7 +83,7 @@ document.addEventListener('DOMContentLoaded', function () {
window.open(authUrl, 'TwitterLogin', `width=${windowWidth},height=${windowHeight},left=${left},top=${top},scrollbars=yes`);
});

['retweet', 'like', 'retweet-2', 'follow-us'].forEach(action => {
['retweet', 'like', 'tweet', 'follow-us'].forEach(action => {
document.getElementById(action).addEventListener('click', () => {
handleAction(action);
});
Expand Down Expand Up @@ -115,7 +115,7 @@ async function checkAuthStatus() {
// Map of enabled flags to their corresponding element IDs
const actionElementMap = {
retweet: 'retweet-section',
retweet2: 'retweet-section-2',
tweet: 'tweet-section',
like: 'like-section',
follow: 'follow-section'
};
Expand Down Expand Up @@ -145,7 +145,7 @@ function displayInfo(action, message, type) {
'authentication': 'twitterAuthMessage',
'retweet': 'repostMessage',
'like': 'likeMessage',
'retweet-2': 'repostMessage2',
'tweet': 'postMessage',
'follow-us': 'followMessage'
};

Expand Down Expand Up @@ -178,7 +178,7 @@ async function handleAction(action) {
try {
const actionConfig = {
'follow-us': { configKey: 'userName', actionType: 'follow-us', info: 'follow-info', progress: 'follow-progress', section: 'follow-section' },
'retweet-2': { configKey: 'tweetId2', actionType: 'retweet', info: 'retweet-2-info', progress: 'retweet-2-progress', section: 'retweet-section-2' },
'tweet': { configKey: 'tweetMessage', actionType: 'tweet', info: 'tweet-info', progress: 'tweet-progress', section: 'tweet-section' },
'retweet': { configKey: 'tweetId', actionType: 'retweet', info: 'retweet-info', progress: 'retweet-progress', section: 'retweet-section' },
'like': { configKey: 'tweetId', actionType: 'like', info: 'like-info', progress: 'like-progress', section: 'like-section' },
};
Expand All @@ -195,16 +195,25 @@ async function handleAction(action) {
if (!configValue) {
throw new Error(`Required configuration value '${configKey}' is missing.`);
}
const queryParams = `${configKey}=${encodeURIComponent(configValue)}`;
const actionType = actionConfig[action].actionType;

const actionResponse = await fetch(`${authWebAddress}/${actionType}?${queryParams}`, {
method: 'GET',
let fetchOptions = {
headers: {
'Content-Type': 'application/json'
},
credentials: 'include', // Ensure cookies are included with the request
});
credentials: 'include', // Ensure cookies are included the request
};

let url = `${authWebAddress}/${actionType}`;

if (actionType === 'tweet') {
fetchOptions.method = 'POST';
fetchOptions.body = JSON.stringify({ [configKey]: configValue });
} else {
fetchOptions.method = 'GET';
url += `?${configKey}=${encodeURIComponent(configValue)}`; // Include queryParams in the URL for GET requests
}

const actionResponse = await fetch(url, fetchOptions);

if (!actionResponse.ok) {
const responseBody = await actionResponse.json();
Expand Down Expand Up @@ -250,7 +259,7 @@ function checkAllActionsDisabled() {
const actionElementMap = {
retweet: 'retweet-section',
like: 'like-section',
retweet2: 'retweet-section-2',
tweet: 'tweet-section',
follow: 'follow-section'
};

Expand Down

0 comments on commit 12b2d14

Please sign in to comment.