TWITCH API : Get Total Bits Donated From User Id

This will need to pass the userId argument before the Execute Code
then after the script you can use totalBitsDonated as a variable that will return the lifetime amount of bits donated from that user on your channel.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;

public class Datum {
    public string user_id { get; set; }
    public string user_login { get; set; }
    public string user_name { get; set; }
    public int rank { get; set; }
    public int score { get; set; }}

public class Root {
    public List<Datum> data { get; set; }
    // public DateRange date_range { get; set; }
    public int total { get; set; }}

public class LeaderBoard{
    public string userName { get; set; }
    public int rankNumber { get; set; }
    public int bits { get; set; }}

public class AllDatas{
    public List<LeaderBoard> leaderBoard { get; set; }
    public DateTime start { get; set; }
    public DateTime end { get; set; }}

public class CPHInline
{
    public bool Execute()
    {
        Task<AllDatas> getAllDatasTask = FunctionGetAllDatas();
        getAllDatasTask.Wait();
        AllDatas datas = getAllDatasTask.Result;
        if (datas.leaderBoard.Count > 0)
        {
        string rank0 = datas.leaderBoard[0].rankNumber.ToString();
        string userName0 = datas.leaderBoard[0].userName;
        string bits0 = datas.leaderBoard[0].bits.ToString();
        CPH.SetArgument("totalBitsDonated", bits0);
        }
        else
        {
            CPH.LogDebug("no data");
            CPH.SetArgument("totalBitsDonated", 0);
        }
        return true;
    }
public HttpClient client = new HttpClient();
    public async Task<Root> FunctionCallTwitchAPI()
    {
        string userId = args["userId"].ToString();
        string tokenValue = CPH.TwitchOAuthToken;
        string clientIdValue = CPH.TwitchClientId;
        client.DefaultRequestHeaders.Clear();
        client.DefaultRequestHeaders.Add("client-ID", clientIdValue);
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenValue);
        HttpResponseMessage response = await client.GetAsync("https://api.twitch.tv/helix/bits/leaderboard?user_id=" + userId);
        HttpContent responseContent = response.Content;
        string responseBody = await response.Content.ReadAsStringAsync();
        CPH.LogDebug(responseBody.ToString());
        Root root = JsonConvert.DeserializeObject<Root>(responseBody);
        return root;
    }

    public async Task<AllDatas> FunctionGetAllDatas()
    {
        AllDatas datas = new AllDatas()
        {
            leaderBoard = new List<LeaderBoard>()
        };
        Root root = await FunctionCallTwitchAPI();
        foreach (Datum datum in root.data)
        {
            LeaderBoard newData = new LeaderBoard
            {
                userName = datum.user_name,
                rankNumber = datum.rank,
                bits = datum.score
            };
            datas.leaderBoard.Add(newData);
        }

        return datas;
    }
}
4 Likes

Screenshot 2024-02-25 104112