Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Polymarket Commission Email Filter - Google Apps Script
- Archives matching commission emails strictly below LIMIT_DOLLARS.
- The default threshold is $10; change it to your preferred amount.
- Messages remain available in Gmail's All Mail.
- SETUP
- 1. Create a project at https://script.google.com/ and paste this code into Code.gs.
- 2. Set LIMIT_DOLLARS to your preferred threshold.
- 3. In the editor, go to Services > + > Gmail API > Add.
- 4. Run archiveSmallCommissions once and authorize Gmail access.
- 5. Go to Triggers > Add Trigger, select archiveSmallCommissions,
- choose Time-driven, and set your preferred schedule.
- 6. If you use a standard Google Cloud project, also enable the Gmail API
- in that project's Google Cloud console.
- */
- const LIMIT_DOLLARS = 10;
- function archiveSmallCommissions() {
- const lock = LockService.getScriptLock();
- if (!lock.tryLock(1000)) return;
- try {
- // Collect messages first, then archive them:
- // this prevents inbox changes from affecting pagination.
- const messageIds = [];
- let pageToken;
- do {
- const options = {
- q: `in:inbox from:${DUB_SENDER} subject:commission subject:Polymarket`,
- maxResults: 500
- };
- if (pageToken) options.pageToken = pageToken;
- const page = Gmail.Users.Messages.list('me', options);
- for (const message of page.messages || []) {
- messageIds.push(message.id);
- }
- pageToken = page.nextPageToken;
- } while (pageToken);
- let archived = 0;
- for (const id of messageIds) {
- const message = Gmail.Users.Messages.get('me', id, {
- format: 'metadata',
- metadataHeaders: ['Subject', 'From']
- });
- if (!(message.labelIds || []).includes('INBOX')) continue;
- const headers = message.payload.headers || [];
- const getHeader = name =>
- (headers.find(h => h.name.toLowerCase() === name) || {}).value || '';
- // Double-check the sender's exact email address.
- const from = getHeader('from').trim();
- const address = (from.match(/<([^<>]+)>/) || [null, from])[1];
- if (address.trim().toLowerCase() !== DUB_SENDER) continue;
- // Read only the commission amount from the subject,
- // not the purchase amount from the message body.
- const subject = getHeader('subject').trim();
- const match = subject.match(
- /^You['’]ve made a \$([0-9]+(?:,[0-9]{3})*)(?:\.([0-9]{2}))? commission from Polymarket$/i
- );
- // Leave messages with unrecognized subjects unchanged.
- if (!match) continue;
- const cents =
- Number(match[1].replace(/,/g, '')) * 100 +
- Number(match[2] || '0');
- if (!Number.isSafeInteger(cents)) continue;
- if (cents < LIMIT_DOLLARS * 100) {
- // Removing the INBOX label archives the message.
- Gmail.Users.Messages.modify(
- { removeLabelIds: ['INBOX'] },
- 'me',
- id
- );
- archived++;
- }
- }
- console.log(`Messages archived: ${archived}`);
- } finally {
- lock.releaseLock();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment