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{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 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.. }

Like my (creative) content? Tell your friends, your pets or anyone you know on social media (?), maybe they like the silly stuff posted here too, thanks for being part of the Blushies community! and related sites are supported by people that tipped / subbed @ Twitch channel and / or are a Patreon sub. ♥ Thank you for making Blushies community site real (really silly and stupid xD) ♥

Sign In or Register to comment.