twitch_api 0.8.0

Library for talking with the new Twitch API aka. "Helix", EventSub and more!
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Sends an message in the broadcaster’s chat room.
//! [`send-chat-message`](https://dev.twitch.tv/docs/api/reference#send-chat-message)
//!
//! # Accessing the endpoint
//!
//! ## Request: [SendChatMessageRequest]
//!
//! To use this endpoint, construct a [`SendChatMessageRequest`] with the [`SendChatMessageRequest::new()`] method.
//!
//! ```rust
//! use twitch_api::helix::chat::send_chat_message;
//! let request = send_chat_message::SendChatMessageRequest::new();
//! ```
//!
//! ## Body: [SendChatMessageBody]
//!
//! We also need to provide a body to the request containing what we want to change.
//!
//! ```
//! # use twitch_api::helix::chat::send_chat_message;
//! let body = send_chat_message::SendChatMessageBody::new(
//!     "12826",                       // broadcaster_id
//!     "141981764",                   // sender_id
//!     "Hello, world! twitchdevHype", // message
//! );
//! ```
//!
//! ## Response: [SendChatMessageResponse]
//!
//!
//! Send the request to receive the response with [`HelixClient::req_post()`](helix::HelixClient::req_post).
//!
//! ```rust, no_run
//! use twitch_api::helix::{self, chat::send_chat_message};
//! # use twitch_api::client;
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
//! # let client: helix::HelixClient<'static, client::DummyHttpClient> = helix::HelixClient::default();
//! # let token = twitch_oauth2::AccessToken::new("validtoken".to_string());
//! # let token = twitch_oauth2::UserToken::from_existing(&client, token, None, None).await?;
//! let request = send_chat_message::SendChatMessageRequest::new();
//! let body =
//!     send_chat_message::SendChatMessageBody::new(
//!         "12826",                        // broadcaster_id
//!         "141981764",                    // sender_id
//!         "Hello, world! twitchdevHype"   // message
//!     );
//! let response: helix::chat::SendChatMessageResponse = client.req_post(request, body, &token).await?.data;
//! # Ok(())
//! # }
//! ```
//!
//! You can also get the [`http::Request`] with [`request.create_request(&token, &client_id)`](helix::RequestPost::create_request)
//! and parse the [`http::Response`] with [`SendChatMessageRequest::parse_response(None, &request.get_uri(), response)`](SendChatMessageRequest::parse_response)

use std::marker::PhantomData;

use super::*;
use helix::RequestPost;
use serde::Serialize;

// Not implementing builder since it's not really needed...
/// Query Parameters for [Send Chat message](super::send_chat_message)
///
/// [`send-chat-message`](https://dev.twitch.tv/docs/api/reference#send-chat-message)
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug, Default)]
#[must_use]
#[non_exhaustive]
pub struct SendChatMessageRequest<'a> {
    #[serde(skip)]
    _marker: PhantomData<&'a ()>,
}

impl SendChatMessageRequest<'_> {
    /// Create a new [`SendChatMessageRequest`]
    pub fn new() -> Self { SendChatMessageRequest::default() }
}

/// Body Parameters for [Send Chat message](super::send_chat_message)
///
/// [`send-chat-message`](https://dev.twitch.tv/docs/api/reference#send-chat-message)
#[derive(PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
#[cfg_attr(feature = "typed-builder", derive(typed_builder::TypedBuilder))]
#[must_use]
#[non_exhaustive]
pub struct SendChatMessageBody<'a> {
    /// The ID of the broadcaster whose chat room the message will be sent to.
    #[cfg_attr(feature = "typed-builder", builder(setter(into)))]
    #[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
    pub broadcaster_id: Cow<'a, types::UserIdRef>,
    /// The ID of the user sending the message. This ID must match the user ID in the user access token.
    #[cfg_attr(feature = "typed-builder", builder(setter(into)))]
    #[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
    pub sender_id: Cow<'a, types::UserIdRef>,
    /// The message to send.
    ///
    /// The message is limited to a maximum of 500 characters.
    /// Chat messages can also include emoticons.
    /// To include emoticons, use the name of the emote. The names are case sensitive.
    /// Don't include colons around the name (e.g., :bleedPurple:).
    /// If Twitch recognizes the name, Twitch converts the name to the emote
    /// before writing the chat message to the chat room.
    #[cfg_attr(feature = "typed-builder", builder(setter(into)))]
    #[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
    pub message: Cow<'a, str>,
    /// The ID of the chat message being replied to.
    #[cfg_attr(feature = "typed-builder", builder(setter(into)))]
    #[cfg_attr(feature = "deser_borrow", serde(borrow = "'a"))]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reply_parent_message_id: Option<Cow<'a, types::MsgIdRef>>,
    /// Determines if the chat message is sent only to the source channel (broadcaster_id) during a shared chat session.
    ///
    /// # Notes
    ///
    /// Only available when using an App Access Token.
    /// Has no effect if the message is not sent during a shared chat session.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub for_source_only: Option<bool>,

    /// If true, the message will be sent and immediately pinned.
    ///
    /// Cannot be combined with `reply_parent_message_id` or `for_source_only`.
    /// When pin is true, additionally requires the `moderator:manage:chat_messages` scope and the sender must be the broadcaster or a moderator.
    /// Messages pinned via this endpoint are always pinned for 20 minutes.
    /// If the pin fails, the message is not sent.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pin: Option<bool>,
}

impl<'a> SendChatMessageBody<'a> {
    /// Send a message in the broadcaster's channel
    pub fn new(
        broadcaster_id: impl types::IntoCow<'a, types::UserIdRef> + 'a,
        sender_id: impl types::IntoCow<'a, types::UserIdRef> + 'a,
        message: impl types::IntoCow<'a, str> + 'a,
    ) -> Self {
        Self {
            broadcaster_id: broadcaster_id.into_cow(),
            sender_id: sender_id.into_cow(),
            message: message.into_cow(),
            reply_parent_message_id: None,
            for_source_only: None,
            pin: None,
        }
    }

    /// Set the reply parent message-id
    pub fn reply_parent_message_id(
        mut self,
        reply_parent_message_id: impl types::IntoCow<'a, types::MsgIdRef> + 'a,
    ) -> Self {
        self.reply_parent_message_id = Some(reply_parent_message_id.into_cow());
        self
    }

    /// Send the chat message only to the source channel (broadcaster_id) during a shared chat session.
    ///
    /// # Notes
    ///
    /// Only available when using an App Access Token.
    /// Has no effect if the message is not sent during a shared chat session.
    pub fn for_source_only(mut self, source_only: bool) -> Self {
        self.for_source_only = Some(source_only);
        self
    }

    /// If true, the message will be sent and immediately pinned.
    ///
    /// Cannot be combined with `reply_parent_message_id` or `for_source_only`.
    /// When pin is true, additionally requires the `moderator:manage:chat_messages` scope and the sender must be the broadcaster or a moderator.
    /// Messages pinned via this endpoint are always pinned for 20 minutes.
    /// If the pin fails, the message is not sent.
    pub fn pin(mut self, pin: bool) -> Self {
        self.pin = Some(pin);
        self
    }
}

impl helix::private::SealedSerialize for SendChatMessageBody<'_> {}

impl helix::HelixRequestBody for [SendChatMessageBody<'_>] {
    fn try_to_body(&self) -> Result<hyper::body::Bytes, helix::BodyError> {
        #[derive(Serialize)]
        struct InnerBody<'a> {
            data: &'a [SendChatMessageBody<'a>],
        }

        serde_json::to_vec(&InnerBody { data: self })
            .map_err(Into::into)
            .map(Into::into)
    }
}

// The variants below were automatically generated with the following script on
// https://dev.twitch.tv/docs/irc/msg-id/ (assumed to be the same codes)
// [...document.querySelectorAll("table > tbody > tr")]
//   .map((row) => [...row.querySelectorAll("td")].map((x) => x.textContent))
//   .filter(([id]) => id.startsWith("msg_"))
//   .map(
//     ([id, description]) =>
//       `/// ${description}\n${id
//         .split("_")
//         .map((x) => x[0].toUpperCase() + x.slice(1))
//         .join("")},`,
//   )
//   .join("\n");

/// Code for why a message was dropped.
///
/// See <https://dev.twitch.tv/docs/irc/msg-id/>
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ChatMessageDropCode {
    /// You are permanently banned from talking in `<channel>`.
    MsgBanned,
    /// Your message was not sent because it contained too many unprocessable characters. If you believe this is an error, please rephrase and try again.
    MsgBadCharacters,
    /// Your message was not sent because your account is not in good standing in this channel.
    MsgChannelBlocked,
    /// This channel does not exist or has been suspended.
    MsgChannelSuspended,
    /// Your message was not sent because it is identical to the previous one you sent, less than 30 seconds ago.
    MsgDuplicate,
    /// This room is in emote-only mode. You can find your currently available emoticons using the smiley in the chat text area.
    MsgEmoteonly,
    /// This room is in `<duration>` followers-only mode. Follow `<channel>` to join the community! Note: These msg_followers tags are kickbacks to a user who does not meet the criteria; that is, does not follow or has not followed long enough.
    MsgFollowersonly,
    /// This room is in `<duration1>` followers-only mode. You have been following for `<duration2>`. Continue following to chat!
    MsgFollowersonlyFollowed,
    /// This room is in followers-only mode. Follow `<channel>` to join the community!
    MsgFollowersonlyZero,
    /// This room is in unique-chat mode and the message you attempted to send is not unique.
    MsgR9k,
    /// Your message was not sent because you are sending messages too quickly.
    MsgRatelimit,
    /// Hey! Your message is being checked by mods and has not been sent.
    MsgRejected,
    /// Your message wasn’t posted due to conflicts with the channel’s moderation settings.
    MsgRejectedMandatory,
    /// A verified phone number is required to chat in this channel. Please visit <https://www.twitch.tv/settings/security> to verify your phone number.
    MsgRequiresVerifiedPhoneNumber,
    /// This room is in slow mode and you are sending messages too quickly. You will be able to talk again in `<number>` seconds.
    MsgSlowmode,
    /// This room is in subscribers only mode. To talk, purchase a channel subscription at `https://www.twitch.tv/products/<broadcaster login name>/ticket?ref=subscriber_only_mode_chat`.
    MsgSubsonly,
    /// You don’t have permission to perform that action.
    MsgSuspended,
    /// You are timed out for `<number>` more seconds.
    MsgTimedout,
    /// This room requires a verified account to chat. Please verify your account at <https://www.twitch.tv/settings/security>.
    MsgVerifiedEmail,
    /// An unknown drop-code.
    #[serde(untagged)]
    Unknown(String),
}

impl std::fmt::Display for ChatMessageDropCode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.serialize(f) }
}

/// A drop reason of a sent message.
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct ChatMessageDropReason {
    /// Code for why the message was dropped.
    ///
    /// See [ChatMessageDropCode] for possible values.
    pub code: ChatMessageDropCode,
    /// Message for why the message was dropped.
    pub message: String,
}

/// Return Values for [Send Chat message](super::send_chat_message)
///
/// [`send-chat-message`](https://dev.twitch.tv/docs/api/reference#send-chat-message)
#[derive(PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "deny_unknown_fields", serde(deny_unknown_fields))]
#[non_exhaustive]
pub struct SendChatMessageResponse {
    /// The message id for the message that was sent.
    #[serde(deserialize_with = "crate::deserialize_none_from_empty_string")]
    pub message_id: Option<types::MsgId>,
    /// If the message passed all checks and was sent.
    pub is_sent: bool,
    /// The reason the message was dropped, if any.
    pub drop_reason: Option<ChatMessageDropReason>,
}

impl Request for SendChatMessageRequest<'_> {
    type PaginationData = ();
    type Response = SendChatMessageResponse;

    const PATH: &'static str = "chat/messages";
    /// Requires an app access token or user access token that includes the [user:write:chat][twitch_oauth2::Scope::UserWriteChat] scope.
    /// If app access token used, then additionally requires [user:bot][twitch_oauth2::Scope::UserBot] scope from chatting user,
    /// and either [channel:bot][twitch_oauth2::Scope::ChannelBot] scope from broadcaster or moderator status.
    #[cfg(feature = "twitch_oauth2")]
    const SCOPE: twitch_oauth2::Validator =
        twitch_oauth2::validator![twitch_oauth2::Scope::UserWriteChat];
}

impl<'a> RequestPost for SendChatMessageRequest<'a> {
    type Body = SendChatMessageBody<'a>;

    fn parse_inner_response<'d>(
        request: Option<Self>,
        uri: &http::Uri,
        response: &str,
        status: http::StatusCode,
    ) -> Result<helix::Response<Self, Self::Response>, helix::HelixRequestPostError>
    where
        Self: Sized,
    {
        let resp = match status {
            http::StatusCode::OK => {
                let resp: helix::InnerResponse<[SendChatMessageResponse; 1]> =
                    helix::parse_json(response, true).map_err(|e| {
                        helix::HelixRequestPostError::DeserializeError(
                            response.to_string(),
                            e,
                            uri.clone(),
                            status,
                        )
                    })?;
                let [s] = resp.data;
                s
            }
            _ => {
                return Err(helix::HelixRequestPostError::InvalidResponse {
                    reason: "unexpected status",
                    response: response.to_string(),
                    status,
                    uri: uri.clone(),
                })
            }
        };
        Ok(helix::Response::with_data(resp, request))
    }
}

#[cfg(test)]
#[test]
fn test_success() {
    use helix::*;
    let req = SendChatMessageRequest::new();

    let body = SendChatMessageBody::new("12826", "141981764", "Hello, world! twitchdevHype");

    assert_eq!(
        std::str::from_utf8(&body.try_to_body().unwrap()).unwrap(),
        r#"{"broadcaster_id":"12826","sender_id":"141981764","message":"Hello, world! twitchdevHype"}"#
    );

    dbg!(req.create_request(body, "token", "clientid").unwrap());

    // From twitch docs
    let data = br#"
    {
      "data": [
          {
            "message_id": "abc-123-def",
            "is_sent": true
          }
      ]
    }
    "#
    .to_vec();

    let http_response = http::Response::builder().status(200).body(data).unwrap();

    let uri = req.get_uri().unwrap();
    assert_eq!(
        uri.to_string(),
        "https://api.twitch.tv/helix/chat/messages?"
    );

    dbg!(SendChatMessageRequest::parse_response(Some(req), &uri, http_response).unwrap());
}

#[cfg(test)]
#[test]
fn test_reject() {
    use helix::*;
    let req = SendChatMessageRequest::new();
    let data = br#"
    {
      "data": [
        {
          "message_id": "",
          "is_sent": false,
          "drop_reason": {
            "code": "msg_rejected",
            "message": "Your message is being checked by mods and has not been sent."
          }
        }
      ]
    }
    "#
    .to_vec();

    let http_response = http::Response::builder().status(200).body(data).unwrap();
    let uri = req.get_uri().unwrap();

    let res = SendChatMessageRequest::parse_response(None, &uri, http_response).unwrap();
    assert_eq!(res.data.message_id, None);
    assert_eq!(
        res.data.drop_reason.unwrap().code,
        ChatMessageDropCode::MsgRejected
    );

    let data = br#"
    {
      "data": [
        {
          "message_id": "",
          "is_sent": false,
          "drop_reason": {
            "code": "Foo",
            "message": "Super unknown"
          }
        }
      ]
    }
    "#
    .to_vec();

    let http_response = http::Response::builder().status(200).body(data).unwrap();

    let res = SendChatMessageRequest::parse_response(None, &uri, http_response).unwrap();
    assert_eq!(res.data.message_id, None);
    assert_eq!(
        res.data.drop_reason.unwrap().code,
        ChatMessageDropCode::Unknown("Foo".to_string())
    );
}