1+ import express , { Request , Response } from 'express' ;
2+ import { client } from "./src/client" ;
3+ import { HumanResponseEvent } from './src/events' ;
4+
5+ const app = express ( ) ;
6+ app . use ( express . json ( ) ) ;
7+ const port = process . env . PORT || 4000 ;
8+
9+ app . post ( '/' , async ( req : Request , res : Response ) => {
10+ console . log ( `Received webhook from gotoHuman` , req . body )
11+
12+ try {
13+ // Our gotoHuman review form also contains a buttonSelect with the ID 'publishDecision', value: publish|discard
14+ const workflowResponse = await client . sendWorkflowEvent ( {
15+ event : {
16+ name : HumanResponseEvent . name ,
17+ input : {
18+ linkedInPost : req . body ?. responseValues ?. linkedInPost ?. value || "" ,
19+ publishDecision : req . body ?. responseValues ?. publishDecision ?. value || ""
20+ } ,
21+ } ,
22+ workflow : {
23+ workflowId : req . body ?. meta ?. restackWorkflowId ,
24+ runId : req . body ?. meta ?. restackRunId ,
25+ } ,
26+ } ) ;
27+ console . log ( `Sent event to workflow. Returned ${ workflowResponse } ` )
28+
29+ res . status ( 200 ) . json ( { msg : 'Processed human feedback ' + workflowResponse } )
30+ } catch ( e ) {
31+ console . error ( "Sending event to workflow failed!" , e )
32+ res . status ( 500 ) . json ( { error : `Could not process webhook!` } )
33+ }
34+ } ) ;
35+
36+ app . listen ( port , ( ) => {
37+ console . log ( `Server running at http://localhost:${ port } ` ) ;
38+ } ) ;
0 commit comments