Гайд Twitch Stream API

  • 702
  • 2
  • 237
Нам понадобится нугет пакеты
1. TwitchLib.Client


Получение Acces Token
1. Заходим на Twitch Token Generator
1.1 Выбираем Custom Scope Token
2. Выбираем все Available Token Scopes
2.2 Нажимаем Generate Token
3. Копируем Acces Token


Пример бота:
internal class Program
{
    static void Main(string[] args)
    {
        Bot bot = new Bot();
        Console.ReadLine();
    }
}

class Bot
{
    TwitchClient client;

    public Bot()
    {
        ConnectionCredentials credentials = new ConnectionCredentials("TwitchLogin", "AcessToken");
        var clientOptions = new ClientOptions
        {
            MessagesAllowedInPeriod = 750,
            ThrottlingPeriod = TimeSpan.FromSeconds(30)
        };
        WebSocketClient customClient = new WebSocketClient(clientOptions);
        client = new TwitchClient(customClient);
        client.Initialize(credentials, "Channel");

        client.OnLog += Client_OnLog;
        client.OnJoinedChannel += Client_OnJoinedChannel;
        client.OnMessageReceived += Client_OnMessageReceived;
        client.OnWhisperReceived += Client_OnWhisperReceived;
        client.OnNewSubscriber += Client_OnNewSubscriber;
        client.OnConnected += Client_OnConnected;

        client.Connect();
    }

    private void Client_OnLog(object sender, OnLogArgs e)
    {
        Console.WriteLine($"{e.DateTime.ToString()}: {e.BotUsername} - {e.Data}");
    }

    private void Client_OnConnected(object sender, OnConnectedArgs e)
    {
        Console.WriteLine($"Connected to {e.AutoJoinChannel}");
    }

    private void Client_OnJoinedChannel(object sender, OnJoinedChannelArgs e)
    {
        Console.WriteLine("Hey guys! I am a bot connected via TwitchLib!");
        client.SendMessage(e.Channel, "Hey guys! I am a bot connected via TwitchLib!");
    }

    private void Client_OnMessageReceived(object sender, OnMessageReceivedArgs e)
    {
        Console.WriteLine(e.ChatMessage.Channel + " " + e.ChatMessage.Message);
        if (e.ChatMessage.Message.Contains("badword"))
            client.TimeoutUser(e.ChatMessage.Channel, e.ChatMessage.Username, TimeSpan.FromMinutes(30), "Bad word! 30 minute timeout!");
    }
    
    private void Client_OnWhisperReceived(object sender, OnWhisperReceivedArgs e)
    {
        Console.WriteLine(e.WhisperMessage.Username);
        if (e.WhisperMessage.Username == "my_friend")
            client.SendWhisper(e.WhisperMessage.Username, "Hey! Whispers are so cool!!");
    }

    private void Client_OnNewSubscriber(object sender, OnNewSubscriberArgs e)
    {
        if (e.Subscriber.SubscriptionPlan == SubscriptionPlan.Prime)
            client.SendMessage(e.Channel, $"Welcome {e.Subscriber.DisplayName} to the substers! You just earned 500 points! So kind of you to use your Twitch Prime on this channel!");
        else
            client.SendMessage(e.Channel, $"Welcome {e.Subscriber.DisplayName} to the substers! You just earned 500 points!");
    }
}
 
Сверху Снизу