#twitch #oauth #api #event-sub #api-bindings

twitch_eventsub

一个用于处理Twitch EventSub Helix API所有事情的库

2次发布

0.1.1 2024年7月13日
0.1.0 2024年6月29日

#202 in 身份验证

Download history 124/week @ 2024-06-24 19/week @ 2024-07-01 114/week @ 2024-07-08 15/week @ 2024-07-15 37/week @ 2024-07-29

166 每月下载量

MIT 许可证

80KB
2.5K SLoC

TwitchEventSub-rs

一个简单的Rust库,用于处理那个讨厌的Twitch API,特别是事件订阅。

快速入门

设置身份验证密钥。

在工作目录中创建一个 .secrets.env 或 .example.env 文件,并填写以下内容

// Required
TWITCH_CLIENT_ID = "CLIENT_ID from twitch console app"
TWITCH_CLIENT_SECRET = "CLIENT_SECRET from twitch console app"
TWITCH_BROADCASTER_ID = "Your broadcaster ID as numbers"
TWITCH_BOT_ID = "Your broadcaster ID as numbers"

示例用法

fn main() {
  let mut twitch = TwitchEventSubApi::builder(keys)
    .set_redirect_url(redirect_url)
    .generate_new_token_if_insufficent_scope(true)
    .generate_new_token_if_none(true)
    .generate_access_token_on_expire(true)
    .auto_save_load_created_tokens(".user_token.env", ".refresh_token.env")
    .add_subscription(SubscriptionPermission::ChatMessage)
    .add_subscription(SubscriptionPermission::CustomRedeem)
    .add_subscription(SubscriptionPermission::BanTimeoutUser)
    .add_subscription(SubscriptionPermission::DeleteMessage)
    .add_subscription(SubscriptionPermission::AdBreakBegin);

   let mut api = {
     match event_sub_api.build() {
       Ok(api) => api,
       Err(EventSubError::TokenMissingScope) => {
         panic!("Reauthorisation of toke is required for the token to have all the requested subscriptions.");
       }
       Err(EventSubError::NoSubscriptionsRequested) => {
         panic!("No subsciptions passed into builder!");
       }
       Err(EventSubError::NoScopedOuthTokenProvided) => {
         // Provide a Scoped Oauth key or get a new one

         panic!("");
       }
       Err(e) => {
         // some other error
         panic!("{:?}", e);
       }
     }
   };


  // users program main loop simulation
  loop {
    // non blocking for loop of messages
    for msg in api.receive_messages() {
      match msg {
        MessageType::Message(message_data) => {
          let message = message_data.message;
          let username = message_data.username;
          println!("{} said: {}", username, message);
          api.send_chat_message(MessageType::ChannelMessage(format!("Thank you for chatting {}!", username));
        }
        MessageType::CustomRedeem((username, input, reward)) => {
            println!(
              "{} redeemed {} with {} Channel Points: {}",
              username, reward.title, reward.cost, input,
            );
        }
        MessageType::Close => println!("Twitch requested socket close."),
        _ => {
          // Events that you don't care about or are not subscribed to, can be ignored.
        }
      }
    }
  }
}

构建

cargo build --release

常见问题解答

  • 错误:错误的重定向URL不匹配!
 Parameter+redirect_uri+does+not+match+registered+URI error

如果你收到这个错误,你可能忘记包含你的app redirect_url的http://前缀,因为它必须与你在Twitch应用控制台中设置的OAuth重定向URL完全匹配。

许可证

TwitchEventSub-rs 包是在MIT许可证下授权的,有关更多详细信息,请参阅LICENSE

依赖项

~14–42MB
~727K SLoC