Check if channel is live [C#, Discord]

Hello.
My problem is very simple: I’m trying to make my discord bot check if a channel has gone live, and I cannot figure out how to do it. I get it has something to do with Json and html and whatnot but I don’t actually know how to work with JSON or Html, and I’m “self-taught” so I fear I may not be knowledgeable enough to solve this on my own :frowning:
I also want it to check the “title” of the stream which is set with Nightbot.
I don’t even have any code yet.

You need to use C# to load (GET) a web URL - https://api.twitch.tv/helix/streams?user_login=lightstreak Replace lightstreak with the user you are interested in.
You need to set a Header with your Client-ID.

Then, when you load the URL, you will get a response in JSON format - this is why you need to parse JSON.

If the user is not online, you will get a response like this:

{
  "data": [],
  "pagination": {}
}

If the user is online, you will get a response like this:

{
  "data": [
    {
      "id": "28110269104",
      "user_id": "26490481",
      "game_id": "497078",
      "community_ids": [],
      "type": "live",
      "title": "FC5 PC First Playthrough - [ Follow @summit1g ]",
      "viewer_count": 25871,
      "started_at": "2018-03-28T19:29:14Z",
      "language": "en",
      "thumbnail_url": "https://static-cdn.jtvnw.net/previews-ttv/live_user_summit1g-{width}x{height}.jpg"
    }
  ],
  "pagination": {
    "cursor": "eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6MX19"
  }
}

Note that type is “live”

so would I do something like “if data is null , then send message streamer not online else streamer is online”.
I have a script that handles commands. Would I put that in there?

Have a look at System.Net.HttpClient (GetStringAsync, DefaultRequestHeaders) for making the API request and Newtonsoft.Json (DeserializeObject<T>) for parsing the response.

If the returned array contains the stream, it’s online. Otherwise it’s offline.

DO I need to use some specific librairy

He told you:

You don’t need to use a library, but it can expedite the process and make it a bit more friendly. The library I use to make rest requests is called RestSharp. It’s easy to use and easy to customize. I also use Newtonsoft in combination with RestSharp to deserialize the result. RestSharp has a native serializer, but Newtonsoft is much more powerful, feature rich, and faster.

But, this is all for quality of life purposes. You can do all of this with the .NET framework in C# if you really wanted to.

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