Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!








Get subscriptions list from API + latest sub + resub example

edited December 2017 in Developers
To get a list of subscribers you need to use the subscriptions endpoint https://api.twitch.tv/kraken/channels/{channelid}/subscriptions?api_version=5&oauth_token=yourtoken

c# example (Json.NET)

Create a class called Subscription with the fields you need:

public class Subscription
{
[JsonProperty(PropertyName = "_total")]
public string Total;

[JsonProperty(PropertyName = "_id")]
public string id;

[JsonProperty(PropertyName = "created_at")]
public DateTime CreatedAt = new DateTime(1970, 1, 1, 0, 0, 0, 0);

//etc

}



Parse the subscription list:
// grab endpoint data here
var subscribers = new List<Helper.Subscription>();
var json = JObject.Parse(Util.GetJsonFromUrl(_endpoint));
var subs = JArray.Parse(json["subscription"].ToString());

foreach (var sub in subs)
{
subscribers.Add(new Helper.Subscription()
{
CreatedAt = (DateTime)sub["created_at"],
id = sub["_id"].ToString(),
SubPlan = sub["sub_plan"].ToString(),
SubPlanName = sub["sub_plan_name"].ToString(),
User = JsonConvert.DeserializeObject<User>(sub["user"].ToString())
//etc
});
}


Write latest sub to a file (Note that I filter out the channel name since that count as a subscriber):
var sub = Api.Twitch.Subscription.GetSubscribers("channelid").Where(w => !w.User.Name.ToLower().Equals(IrcClient.Channel)).OrderByDescending(o => o.User.UpdatedAt).Take(1).SingleOrDefault();
if (sub != null)
{
using (var latestSub = new StreamWriter(Configuration.RootDirectory + @"\files\latestsub.txt"))
{
latestSub .WriteLine(sub.User.Name);
}
}




Get re-subs
At the moment it is not possible to get re-subs with the current API but an alternative way is to grab the chat message in the USERNOTICE event with the msg-id tag being resub (requires twitch.tv/tags) and then update the text file accordingly. The BlushyFaceBot Twitch library handles this in the IrcMessageHandler.UserNotice(IrcMessage message) to parse the USERNOTICE.

Basically this is all there is to it:

if(message.Tags.GetTagValue("msg-id").Equals("resub")) {
//this is a re-sub. Handle whatever here..
}

Hi every Blushies! | My website | My Twitch stream | My web comics
Consider supporting my creative content if you want me to create content regularly! Donate / tip: https://twitch.streamlabs.com/blushyface | Patreon page: https://patreon.com/Blushyface
Sign In or Register to comment.