diff --git a/packages/bot-engine/blocks/integrations/webhook/executeWebhookBlock.ts b/packages/bot-engine/blocks/integrations/webhook/executeWebhookBlock.ts index d691c33d36a..afe0b849a8b 100644 --- a/packages/bot-engine/blocks/integrations/webhook/executeWebhookBlock.ts +++ b/packages/bot-engine/blocks/integrations/webhook/executeWebhookBlock.ts @@ -205,7 +205,7 @@ export const executeWebhook = async ( json: !isFormData && body && isJson ? body : undefined, body: (isFormData && body ? body : undefined) as any, timeout: isNotDefined(env.CHAT_API_TIMEOUT) - ? undefined + ? false : params.timeout && params.timeout !== defaultTimeout ? Math.min(params.timeout, maxTimeout) * 1000 : isLongRequest @@ -215,30 +215,39 @@ export const executeWebhook = async ( try { const response = await ky(request.url, omit(request, 'url')) - const body = await response.text() + const body = response.headers.get('content-type')?.includes('json') + ? await response.json() + : await response.text() logs.push({ status: 'success', description: webhookSuccessDescription, details: { statusCode: response.status, - response: safeJsonParse(body).data, + response: typeof body === 'string' ? safeJsonParse(body).data : body, request, }, }) return { response: { statusCode: response.status, - data: safeJsonParse(body).data, + data: typeof body === 'string' ? safeJsonParse(body).data : body, }, logs, startTimeShouldBeUpdated: true, } } catch (error) { if (error instanceof HTTPError) { - const responseBody = await error.response.text() + const responseBody = error.response.headers + .get('content-type') + ?.includes('json') + ? await error.response.json() + : await error.response.text() const response = { statusCode: error.response.status, - data: safeJsonParse(responseBody).data, + data: + typeof responseBody === 'string' + ? safeJsonParse(responseBody).data + : responseBody, } logs.push({ status: 'error', @@ -255,13 +264,15 @@ export const executeWebhook = async ( const response = { statusCode: 408, data: { - message: `Request timed out. (${(request.timeout ?? 0) / 1000}ms)`, + message: `Request timed out. (${ + (request.timeout ? request.timeout : 0) / 1000 + }ms)`, }, } logs.push({ status: 'error', description: `Webhook request timed out. (${ - (request.timeout ?? 0) / 1000 + (request.timeout ? request.timeout : 0) / 1000 }s)`, details: { response, diff --git a/packages/lib/JSONParse.ts b/packages/lib/JSONParse.ts index f28c2754e70..a00c6695843 100644 --- a/packages/lib/JSONParse.ts +++ b/packages/lib/JSONParse.ts @@ -12,8 +12,7 @@ - After the match there is , OR } without " after it OR ] without " after it */ export const JSONParse = (json: string) => { - const numbersBiggerThanMaxInt = - /(?<=[^\\]":[\[]?|[^\\]":\[.*)(-?\d{17,}|-?(?:[9](?:[1-9]07199254740991|0[1-9]7199254740991|00[8-9]199254740991|007[2-9]99254740991|007199[3-9]54740991|0071992[6-9]4740991|00719925[5-9]740991|007199254[8-9]40991|0071992547[5-9]0991|00719925474[1-9]991|00719925474099[2-9])))(?=,|\}[^"]|\][^"])/g + const numbersBiggerThanMaxInt = /(? {