@@ -3,36 +3,42 @@ import { headers } from 'next/headers';
3
3
4
4
import Mux from '@mux/mux-node' ;
5
5
6
- const { Knock } = require ( " @knocklabs/node" ) ;
7
- const knockClient = new Knock ( process . env . KNOCK_API_KEY ) ;
6
+ import { Knock } from ' @knocklabs/node' ;
7
+ const knockClient = new Knock ( process . env . KNOCK_SECRET_KEY ) ;
8
8
9
- const mux = new Mux ( {
10
- webhookSecret : process . env . MUX_WEBHOOK_SECRET ,
11
- } ) ;
9
+ // const mux = new Mux({
10
+ // webhookSecret: process.env.MUX_WEBHOOK_SECRET,
11
+ // });
12
12
13
13
export async function POST ( request : Request ) {
14
- const headersList = headers ( ) ;
15
- const body = await request . text ( ) ;
16
- const event = mux . webhooks . unwrap ( body , headersList ) ;
14
+ const headersList = headers ( ) ;
15
+ const body = await request . text ( ) ;
16
+ const event = JSON . parse ( body ) ;
17
+ console . log ( event ) ;
18
+ // const event = mux.webhooks.unwrap(body, headersList);
17
19
18
- switch ( event . type ) {
19
- case 'video.live_stream.active' :
20
- case 'video.asset.ready' :
21
- // The key of the workflow (from Knock dashboard)
22
- await knockClient . notify ( "dinosaurs-loose" , {
23
- // user id of who performed the action
24
- actor : "dnedry" ,
25
- // list of user ids for who should receive the notification
26
- recipients : [ "jhammond" , "agrant" , "imalcolm" , "esattler" ] ,
27
- // data payload to send through
28
- data : event . data ,
29
- // an optional identifier for the tenant that the notifications belong to
30
- tenant : "jurassic-park" ,
31
- } ) ;
32
- break ;
33
- default :
34
- break ;
35
- }
20
+ switch ( event . type ) {
21
+ case 'video.live_stream.active' :
22
+ console . log ( '📺 live stream started.....' ) ;
23
+ const channel = await knockClient . objects . get ( 'channels' , 'knock' ) ;
24
+ const { workflow_run_id } = await knockClient . workflows . trigger (
25
+ 'new-stream' ,
26
+ {
27
+ recipients : [ { collection : 'channels' , id : 'knock' } ] ,
28
+ data : {
29
+ channel,
30
+ } ,
31
+ }
32
+ ) ;
33
+ break ;
34
+ case 'video.asset.ready' :
35
+ console . log ( '🎞️ new asset ready.....' ) ;
36
36
37
- return Response . json ( { message : 'ok' } ) ;
37
+ console . log ( `📨 new workflow run started: ${ workflow_run_id } ` ) ;
38
+ break ;
39
+ default :
40
+ break ;
41
+ }
42
+
43
+ return Response . json ( { message : 'ok' } ) ;
38
44
}
0 commit comments