AtlatestRepositorysigil-twitch
sigil-twitch / treeREADME.md
1
# sigil-twitch3
Twitch Helix API client library for Sigil.5
Provides functions for stream management, scheduling, analytics, chat6
interaction, and EventSub real-time events.8
## Features10
- **Channel management** — get/modify channel info (title, game, tags)11
- **Stream info** — query live streams, get stream keys12
- **User lookup** — find users by ID or login13
- **Search** — search categories and channels14
- **Schedule** — CRUD for recurring/non-recurring schedule segments, vacation mode, iCal export15
- **Analytics** — followers, subscribers, clips, VODs, extension/game analytics16
- **Chat** — send messages, list chatters, manage settings, announcements17
- **EventSub** — WebSocket message parsing, subscription management, convenience helpers18
- **Pagination** — generic cursor-based paginator for any list endpoint19
- **Record types** — typed records for channels, streams, users, clips, videos, schedule segments21
## Modules23
| Module | Description |24
|--------|-------------|25
| `(twitch)` | Core client, auth, HTTP helpers, channels, streams, users, search, pagination |26
| `(twitch schedule)` | Schedule segments, vacation, iCalendar export |27
| `(twitch analytics)` | Followers, subscribers, clips, VODs, extension/game analytics |28
| `(twitch chat)` | Chat messages, chatters, settings, announcements |29
| `(twitch eventsub)` | EventSub WebSocket message parsing, subscription management |30
| `(twitch oauth)` | OAuth 2.0 integration via sigil-oauth (token management, auto-refresh) |32
## Usage34
### Client Setup36
```scheme37
(import (twitch))39
;; Create a client with your Twitch credentials40
(define client41
(twitch-client client-id: "your-client-id"42
access-token: "your-oauth-token"))44
;; For local development with the Twitch CLI mock server:45
(define mock-client46
(twitch-client client-id: "test-id"47
access-token: "test-token"48
base-url: "http://localhost:8080/mock"))49
```51
### OAuth 2.0 Token Management53
```scheme54
(import (twitch oauth))56
;; Create an OAuth config for Twitch57
(define config58
(twitch-oauth-config "your-client-id" "your-client-secret"))60
;; Load stored tokens (from ~/.config/sigil/twitch/tokens.json)61
(define tokens (twitch-load-tokens))63
;; Build a client with auto-refresh64
(let-values (((client fresh-tokens)65
(twitch-ensure-client config tokens (current-second))))66
;; client is ready to use with both Client-Id and Bearer headers67
(twitch-channel-info client "141981764"))69
;; Run the full authorization flow (opens browser, starts callback server)70
(define tokens71
(oauth-run-authorization-flow config (current-second)))72
(twitch-save-tokens tokens)73
```75
### Channel Info77
```scheme78
;; Get channel info (returns a list of twitch-channel records)79
(let ((channels (twitch-channel-info client "141981764")))80
(for-each (lambda (ch)81
(display (twitch-channel-title ch)))82
channels))84
;; Modify channel title and category85
(twitch-modify-channel client "141981764"86
#{ title: "Building a Twitch bot in Sigil"87
game-id: "509670" })88
```90
### Streams92
```scheme93
;; Get live streams for specific users94
(let ((streams (twitch-streams client95
#{ user-login: "twitchdev" })))96
(for-each (lambda (s)97
(display (twitch-stream-title s))98
(display (twitch-stream-viewer-count s)))99
streams))100
```102
### Users104
```scheme105
;; Look up users by login name106
(let ((users (twitch-users client #{ login: "twitchdev" })))107
(for-each (lambda (u)108
(display (twitch-user-display-name u)))109
users))110
```112
### Pagination114
```scheme115
;; Auto-paginate through all followers116
(import (twitch analytics))118
(let ((all-followers119
(twitch-paginate120
(lambda (cursor)121
(let ((params (list122
(cons "broadcaster_id" "141981764")123
(cons "first" "100")124
(cons "after" cursor))))125
(twitch-get/json client126
(string-append127
(twitch-api-url client "channels" "followers")128
(build-query-string params)))))129
(lambda (item) item) ;; or a custom parse function130
10))) ;; max 10 pages131
(display (length all-followers)))132
```134
### Schedule136
```scheme137
(import (twitch schedule))139
;; Get the schedule140
(let ((result (twitch-schedule client "141981764")))141
(for-each (lambda (seg)142
(display (twitch-schedule-segment-title seg)))143
(dict-ref result segments:)))145
;; Create a recurring weekly segment146
(twitch-create-segment client "141981764"147
"2026-04-04T18:00:00Z" "America/New_York"148
#{ title: "Friday Coding Stream"149
duration: 240150
category-id: "509670"151
is-recurring: #t })152
```154
### Chat156
```scheme157
(import (twitch chat))159
;; Send a chat message160
(twitch-send-chat-message client161
"141981764" ;; broadcaster-id162
"12345678" ;; sender-id (your user ID)163
"Hello from Sigil!")164
```166
### EventSub168
```scheme169
(import (twitch eventsub))171
;; After connecting to the WebSocket and receiving a welcome message:172
(let* ((msg (parse-eventsub-message raw-json))173
(session (eventsub-session-from-welcome msg))174
(session-id (twitch-eventsub-session-id session)))176
;; Subscribe to stream online events177
(twitch-subscribe-stream-online client session-id "141981764")179
;; Later, when notifications arrive:180
(let ((notification (parse-eventsub-message notification-json)))181
(when (string=? (eventsub-message-type notification) "notification")182
(let ((event (eventsub-notification-event notification))183
(type (eventsub-notification-subscription-type notification)))184
(display type) ;; "stream.online"185
(display event))))) ;; event data dict186
```188
## Authentication190
All Helix API requests require both a **Client-Id** and a **Bearer access token**.191
The `twitch-client` record holds both and the library adds the appropriate192
headers to every request automatically.194
### Required Scopes196
| Operation | Scope |197
|-----------|-------|198
| Modify channel | `channel:manage:broadcast` |199
| Read stream key | `channel:read:stream_key` |200
| Manage schedule | `channel:manage:schedule` |201
| Read followers | `moderator:read:followers` |202
| Read subscribers | `channel:read:subscriptions` |203
| Send chat messages | `user:write:chat` |204
| Read chat messages (EventSub) | `user:read:chat` |205
| Create clips | `clips:edit` |206
| Extension analytics | `analytics:read:extensions` |207
| Game analytics | `analytics:read:games` |209
## Building211
```bash212
sigil deps install213
sigil build214
```216
## Testing218
```bash219
sigil test220
```222
## Dependencies224
- sigil-stdlib225
- sigil-http226
- sigil-json227
- sigil-oauth