Available on crate feature eventsub only.
Expand description

Holds serializable EventSub stuff

Use CreateEventSubSubscription to subscribe to an event according to the EventSub guide. Parse the response payload text with Payload::parse or Payload::parse_http.

Examples

Subscribe to a channel’s follow events:

use twitch_api2::eventsub::{channel::ChannelFollowV1, Transport, TransportMethod};
use twitch_api2::helix::{self, eventsub::{
    CreateEventSubSubscriptionBody, CreateEventSubSubscriptionRequest,
}};

let event = ChannelFollowV1::builder()
    .broadcaster_user_id("1234")
    .build();
let transport = Transport::webhook(
    "https://example.org/eventsub/channelfollow",
    String::from("secretabcd"),
);

let request = CreateEventSubSubscriptionRequest::default();
let body = CreateEventSubSubscriptionBody::builder()
    .subscription(event)
    .transport(transport)
    .build();

let event_information = client.req_post(request, body, &token).await?.data;

println!("event id: {:?}", event_information.id);

You’ll now get a http POST request to the url you specified as the callback. You need to respond to this request from your webserver with a 200 OK response with the challenge as the body. After this, you’ll get notifications

use twitch_api2::eventsub::{Event, Payload, Message};
pub fn parse_request(
    request: &http::Request<Vec<u8>>,
) -> Result<http::Response<Vec<u8>>, Box<dyn std::error::Error + Send + Sync + 'static>> {
    // First, we verify the response, assuring it's legit.
    if !Event::verify_payload(request, b"secretabcd") {
        return Err(todo!());
    }
    match Event::parse_http(request)? {
        Event::ChannelFollowV1(Payload {
            message: Message::VerificationRequest(ver),
            ..
        }) => {
            // We've verified the request, so we can respond to it with the challenge
            Ok(http::Response::builder()
                .status(200)
                .body(ver.challenge.into_bytes())?)
        },
        Event::ChannelFollowV1(Payload {
            message: Message::Notification(notif),
            ..
        }) => {
            // make sure you save the `Twitch-Eventsub-Message-Id` headers value,
            // twitch may resend notifications, and in those cases you should just return 200 OK.
             
            // Do whatever you need to do with the event. Preferably send the event to a channel.
            println!("user {:?} followed {:?}", notif.user_name, notif.broadcaster_user_name);
            Ok(http::Response::builder().status(200).body(vec![])?)
        }   
        _ => Ok(http::Response::builder().status(200).body(vec![])?),
    }
}

Modules

Subscription types regarding channels

EventSub events and their types

Subscription types regarding streams

Subscription types regarding users

Structs

General information about an EventSub subscription.

Metadata about the subscription.

Notification received

Transport setting for event notification

Transport response on event notification

Verification Request

Enums

A notification with an event payload. Enumerates all possible Payloads

Event types

Subscription message/payload. Received on events and other messages.

Errors that can happen when parsing payload

Subscription request status

Transport method

Traits

An EventSub subscription.