[Custom API] Generate gift sub link for mobile users

Hi everyone! The Twitch mobile app currently doesn’t have a way for people to gift subscriptions without navigating the desktop site in a browser. This is a pain. In response I’ve created a simple custom API that generates a gift sub link when fed a username and a sub tier.

So what exactly does the code do? Typing !giftsub will explain how to use the command: “To all mobile users, if you wish to gift a sub please type: !giftsub [user you want to give a sub to] [tier you want to subscribe them at (defaults to 1)]” Typing !giftsub [username] will return a tier 1 giftsub link. Typing !giftsub [tier] will return an error asking for a username. Typing !giftsub [username] [tier] or !giftsub [tier] [username] will return a link for the appropriate user and tier. An incorrect tier (tier 4 for example) will return a tier 1 link. The code should also be able to handle and ignore random garbage input after the username and tier. So you could, if someone said, “Hey I want to gift a sub to X,” tag their name after using the command. The command also handles and removes @ signs before usernames. so you can use the @ sign to quick fill a username.

You can add this functionality by typing in your chat

!addcom !giftsub $(urlfetch http://twitchgift.mygamesonline.org/mobilegift.php?streamer=[replacewithyourusername]&gifter=$(user)&target=$(querystring))

Please note that you must replace [replacewithyourusername] with whatever username appears when you click subscribe on your channel. THIS MAY BE DIFFERENT THAN YOUR CURRENT USERNAME. The link to subscribe to your channel does NOT change when you change your username and thus may be your old username. You can check what this is by clicking the subscribe button on your channel and reading the URL of the new page that is opened.

For anyone with more technical know-how that wants to host the code themselves, here is the php:

<?php
$streamer = htmlspecialchars(!empty($_GET["streamer"])? $_GET["streamer"] : false);
$gifter = htmlspecialchars(!empty($_GET["gifter"])? $_GET["gifter"] : false);
	$target = htmlspecialchars(!empty($_GET["target"])? $_GET["target"] : false);
	if (!$target){
		echo "To all mobile users, if you wish to gift a sub please type: !giftsub [user you want to give a sub to] [tier you want to subscribe them at (defaults to 1)]";
		exit();
	}
	$targetArr = explode(" ",$target,3);
	if (strlen($targetArr[0])==1 || strcmp(str_replace("$","",$targetArr[0]),"4.99")==0 || strcmp(str_replace("$","",$targetArr[0]),"9.99")==0 || strcmp(str_replace("$","",$targetArr[0]),"24.99")==0) {
		$tier = $targetArr[0];
		$user = $targetArr[1];
	} else {
		$tier = $targetArr[1];
		$user = $targetArr[0];
	}
	$user = str_replace("@","",$user);
	if (strlen($user) < 2) {
		echo "Please enter a valid username for someone you wish to gift a sub to. Type: !giftsub [user you want to give a sub to] [tier you want to subscribe them at (defaults to 1)]";
		exit();
	}
	switch ($tier){
		case "1":
		case "4.99":
		case "$4.99":
			echo $gifter . " https://www.twitch.tv/products/" . $streamer . "/ticket?recipient=" . $user ;
			break;
		case "2":
		case "9.99":
		case "$9.99":
			echo $gifter . " https://www.twitch.tv/products/" . $streamer . "_2000/ticket?recipient=" . $user ;
			break;
		case "3":
		case "24.99":
		case "$24.99":
			echo $gifter . " https://www.twitch.tv/products/" . $streamer . "_3000/ticket?recipient=" . $user ;
			break;
		default:
			echo $gifter . " https://www.twitch.tv/products/" . $streamer . "/ticket?recipient=" . $user ;
			break;
	}
?>

Rather than using a custom API this could be accomplished with a simple eval script:

@$(user): $(eval let[recipient,tier]=`$(query)`.split(' ');switch(tier){case "2":case "9.99":case "$9.99":tier='_2000';break;case "3":case "24.99":case "$24.99":tier='_3000';break;default:tier=''}`https://www.twitch.tv/products/$(channel)${tier}/ticket?recipient=${encodeURIComponent(recipient)}`)

I had no idea nightbot had this functionality. Where’s the documentation on this?

In the Nightbot docs: https://docs.nightbot.tv/commands/variables/eval

Also It’s worth mentioning that the subscribe link for channels doesn’t change when the channel changes their name and therefore using $(channel) might not be the best idea.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.