(codeLang, apiKey)
| 306 | } |
| 307 | |
| 308 | const getWebhookCode = (codeLang, apiKey) => { |
| 309 | const url = `${baseURL}/api/v1/webhook/${dialogProps.chatflowid}` |
| 310 | const sendsBody = webhookMethod !== 'GET' && webhookMethod !== 'DELETE' |
| 311 | const isJson = webhookContentType === 'application/json' |
| 312 | const sampleBodyJs = isJson ? '{ example: "value" }' : '"example=value"' |
| 313 | const sampleBodyPy = isJson ? '{"example": "value"}' : '"example=value"' |
| 314 | const sampleBodyCurl = isJson ? '{"example": "value"}' : 'example=value' |
| 315 | |
| 316 | if (codeLang === 'Python') { |
| 317 | const headers = [`"Content-Type": "${webhookContentType}"`] |
| 318 | if (apiKey) headers.push(`"Authorization": "Bearer ${apiKey}"`) |
| 319 | if (webhookEnableAuth) headers.push(`"${webhookSignatureHeader}": "<signature>"`) |
| 320 | const bodyArg = sendsBody ? (isJson ? `, json=${sampleBodyPy}` : `, data=${sampleBodyPy}`) : '' |
| 321 | return `import requests |
| 322 | |
| 323 | API_URL = "${url}" |
| 324 | headers = {${headers.join(', ')}} |
| 325 | |
| 326 | response = requests.${webhookMethod.toLowerCase()}(API_URL, headers=headers${bodyArg}) |
| 327 | print(response.json()) |
| 328 | ` |
| 329 | } else if (codeLang === 'JavaScript') { |
| 330 | const headers = [`"Content-Type": "${webhookContentType}"`] |
| 331 | if (apiKey) headers.push(`Authorization: "Bearer ${apiKey}"`) |
| 332 | if (webhookEnableAuth) headers.push(`"${webhookSignatureHeader}": "<signature>"`) |
| 333 | const bodyLine = sendsBody ? `,\n body: ${isJson ? `JSON.stringify(${sampleBodyJs})` : sampleBodyJs}` : '' |
| 334 | return `const response = await fetch( |
| 335 | "${url}", |
| 336 | { |
| 337 | method: "${webhookMethod}", |
| 338 | headers: { ${headers.join(', ')} }${bodyLine} |
| 339 | } |
| 340 | ); |
| 341 | const result = await response.json(); |
| 342 | console.log(result); |
| 343 | ` |
| 344 | } else if (codeLang === 'cURL') { |
| 345 | const parts = [`curl ${url}`, `-X ${webhookMethod}`, `-H "Content-Type: ${webhookContentType}"`] |
| 346 | if (apiKey) parts.push(`-H "Authorization: Bearer ${apiKey}"`) |
| 347 | if (webhookEnableAuth) parts.push(`-H "${webhookSignatureHeader}: <signature>"`) |
| 348 | if (sendsBody) parts.push(`-d '${sampleBodyCurl}'`) |
| 349 | return parts.join(' \\\n ') |
| 350 | } |
| 351 | return '' |
| 352 | } |
| 353 | |
| 354 | const getCode = (codeLang) => { |
| 355 | if (isWebhookFlow) return getWebhookCode(codeLang, null) |
no outgoing calls
no test coverage detected