• Home
  • About
  • Resume

Tweet images with Scriptr and IFTTT

By , 02/28/2017 4:34 PM

Sev Trek

Looks like everybody is doing Twitter bots these days so I decided to try my hand on one as well. And it had to be something lighthearted – to let people’s mind off things for a while. Way back in early 2000s there was a funny parody comic of Star Trek called Sev Trek. Original site is gone now, but archive of images was saved at http://sevspace.com/stupidarchive/sevtrek.asp.htm. Images are numbered sequentially – ideal for automatic processing. My bot would tweet random images every couple hours.

To host bot’s script I decided to go with Scriptr which offers powerful JavaScript backend and multiple expansion modules. And IFTTT has a cool Twitter service, one that can tweet image from an URL. So the idea was:

  1. Code hosted at Scriptr generates URL pointing to an image at sevspace.com
  2. Code then call Maker service of IFTTT – custom service capable of accepting HTTP requests
  3. Maker service triggers Twitter service and passes URL of image it got from Scriptr code
  4. IFTTT Twitter service tweets the image

Scriptr code has to make sure image is random and not repeating. Here’s how it’s done:

var http = require("http");
var no;
var url;

var sevArray, sArray;

function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function padToThree(number) {
  if (number<=999) { number = ("00"+number).slice(-3); }
  return number;
}

//populating array with given values
function populateArray(array, min, max) {
  for (var i=min; i<= max; i++){
     array.push(i);
  }
}

//if arrays don't exist yet - create and populate them
sevArray = storage.local.sevArray;
if (!sevArray) {
  sevArray = [];
  populateArray(sevArray, 1, 289)
} else {
  sevArray = storage.local.sevArray;
}

sArray = storage.local.sArray;
if (!sArray) {
   sArray = [];
   populateArray(sArray, 1, 580)
} else {
   sArray = storage.local.sArray;
}


// Determining which archive to display
if (Math.random() > 0.4) { // doing general sci-fi
  
   no = getRandomInt(0, sArray.length-1);
   url = 'http://sevspace.com/stupidarchive/s' + sArray[no] + '.jpg';
  
   sArray.splice(no, 1);
   if (sArray.length == 0) populateArray(sArray, 100, 500);
   storage.local.sArray = sArray;  

} else { // doing Sev Trek
  
   no = getRandomInt(0, sevArray.length-1);
   url = 'http://sevspace.com/stupidarchive/sevtrek' + padToThree(sevArray[no]) + '.jpg';
  
   sevArray.splice(no, 1);
   if (sevArray.length == 0) populateArray(sevArray, 3, 289);
   storage.local.sevArray = sevArray;  
  
}

var requestObject = {
  "url": "https://maker.ifttt.com/trigger/image_url_posted/with/key/getyourownkey",
  "params": {"value1":url},
  "method": "POST" 
}

http.request(requestObject);

There’re 2 types of images in Sev Archive – Trek and other sci-fi. So we declare 2 arrays sevArray and sArray (line 05). Then arrays data (image numbers) is loaded from local storage, or, if it doesn’t exist yet – arrays are populated (lines 23-38). After that the code decides which type of images to display, giving slightly better chance to general sci-fi because it has more images (line 42). Then the script generates random array index, retrieves image number from that index and creates image URL based on that number (lines 44-60). This part of code also removes used number from the array to avoid repletion, repopulates the array if it’s used up and stores the array in local storage. And finally lines 62-68 call Maker service at IFTTT and pass URL that was just created.

And this is what IFTTT applet looks like on the receiving end:

IFTTT Applet

Scriptr allows you to schedule script so it can run completely autonomus. You can see this bot in action at https://twitter.com/sevtrekarchive

Leave a Reply

Panorama Theme by Themocracy