Getting random strings from an array can be useful when posting to several different platforms with one Applet, while ensuring each of your posts is unique.
The below filter code will post a different notification to Facebook, Twitter, and Reddit when you go live on Twitch:
let postContent = [
Twitch.newStreamByYou.ChannelName + " now streaming: " + Twitch.newStreamByYou.Game,
"I'm streaming now! Playing " + Twitch.newStreamByYou.Game + " at " + Twitch.newStreamByYou.ChannelUrl,
"Content 3",
"Content 4"
//add extra posts here
]
let indexFacebook = Math.floor((Math.random() * postContent.length));
let indexTwitter = Math.floor((Math.random() * postContent.length));
let indexReddit = Math.floor((Math.random() * postContent.length));
while (indexFacebook === indexTwitter) {
indexTwitter = Math.floor((Math.random() * postContent.length));
if (indexFacebook !== indexTwitter) {
break;
}
}
while (indexReddit === indexTwitter || indexReddit === indexFacebook) {
indexReddit = Math.floor((Math.random() * postContent.length));
if (indexReddit !== indexTwitter && indexReddit !== indexFacebook) {
break;
}
}
FacebookPages.createLinkPage.setMessage(postContent[indexFacebook]);
Twitter.postNewTweet.setTweet(postContent[indexTwitter]);
Reddit.submitLinkReddit.setTitle(postContent[indexReddit]);
This code will pick one of the strings from the postContent
array at random for each of your actions. Then it will compare the posts chosen for Facebook and Twitter, forcing Twitter to pick again until they are no longer the same. Finally, this is repeated for Reddit, checking against the Facebook and Twitter posts and retrying until all three are different.
Note that the above is customized to work with the Twitch - New stream started by you trigger, plus the Facebook Pages - Create a link post, Twitter - Post a tweet, and Reddit - Submit a new link actions. The trigger and actions can all be changed, but you'll need to update the corresponding ingredients and methods above.
You can also add or change the strings in the postContent
array, just be sure to include at least as many options as you have actions.
If you need a hand getting this set up, reach out to IFTTT support and we'd be happy to help!