TuBrief
Subscribed Channels
Videos
Community

Practical Chatbot Form Data Normalization and Serverless DB Integration

TuBrief Editorial
August 21, 2026
0
Computing/Software

Written with AI assistance from the source video. The video is the authority.

English한국어Español中文العربيةहिन्दीDeutschFrançaisPortuguêsРусскийBahasa Indonesia日本語

Related Video

▲  Chat SDK: Build a Form Bot29:07

▲ Chat SDK: Build a Form Bot

Vercel

More from the community

사내 시스템에 llm api 붙일 때 마주하는 현실적인 한계와 대응법

September 13, 2026

레거시 백엔드에 GPT-6 Astra 붙일 때 예산 승인과 보안 통과를 먼저 끝내는 법이 있습니다

September 13, 2026

에이전트끼리 대화하다 6천만 원 청구서가 나오는 이유

September 13, 2026

사내 RAG 벡터 검색에 Okta 권한 필터를 직접 거는 방법

September 13, 2026

브라우저 에이전트에게 내 구글 계정을 통째로 넘기면 안 되는 이유

September 12, 2026

Apple Won the AI Race

September 12, 2026

Comments (0)

Log in to leave a comment

No posts yet

© 2026 . All rights reserved.

TuBrief
Subscribed Channels
Videos
Community
Log in

Practical Chatbot Form Data Normalization and Serverless DB Integration

Handling form inputs coming from company messenger bots and inserting them into a database is more tedious than you might think. This is because the payload structures of Slack and Discord are completely different. Spending time every time on API integrations while looking at SDK documentation often causes an existential crisis for a backend developer.

Handling Multi-Platform Schema Fragmentation

Slack passes data as a triple-nested object when a modal is submitted. Discord sends values in the form of a component array. This is compounded by the pressure of having to respond within 3 seconds of receiving the webhook.

We combine the Adapter pattern and Zod to build a domain normalization layer.

  1. Define the backend standard schema using Zod.
  2. Implement SlackPayloadAdapter and DiscordPayloadAdapter respectively.
  3. Parse the data paths, which vary by channel, and bind them to the common schema.

`typescript
import { z } from 'zod';

export const CommonFormSchema = z.object({
platform: z.enum(['SLACK', 'DISCORD']),
userId: z.string().min(1),
formId: z.string().min(1),
submittedAt: z.date(),
fields: z.object({
applicantName: z.string().min(2),
contactEmail: z.string().email(),
category: z.enum(['BUG', 'FEATURE', 'INQUIRY']),
description: z.string().max(2000),
}),
});

export type NormalizedFormData = z.infer;

export class SlackPayloadAdapter {
static adapt(rawPayload: any): NormalizedFormData {
const values = rawPayload.view?.state?.values || {};
return CommonFormSchema.parse({
platform: 'SLACK',
userId: rawPayload.user?.id,
formId: rawPayload.view?.callback_id,
submittedAt: new Date(),
fields: {
applicantName: values['name_block']?.['name_action']?.value,
contactEmail: values['email_block']?.['email_action']?.value,
category: values['category_block']?.['category_action']?.selected_option?.value,
description: values['desc_block']?.['desc_action']?.value,
},
});
}
}

export class DiscordPayloadAdapter {
static adapt(rawPayload: any): NormalizedFormData {
const components = rawPayload.data?.components || [];
const fieldMap: Record<string, string> = {};

for (const row of components) {
  for (const comp of row.components || []) {
    if (comp.custom_id) {
      fieldMap[comp.custom_id] = comp.value || comp.values?.[0];
    }
  }
}

return CommonFormSchema.parse({
  platform: 'DISCORD',
  userId: rawPayload.member?.user?.id || rawPayload.user?.id,
  formId: rawPayload.data?.custom_id,
  submittedAt: new Date(),
  fields: {
    applicantName: fieldMap['applicant_name'],
    contactEmail: fieldMap['contact_email'],
    category: fieldMap['category'],
    description: fieldMap['description'],
  },
});

}
}

`

Separating the adapters means you don't have to modify the business logic even if a new messenger is added, reducing maintenance overhead.

Serverless Environment Connection Pooling and Transactions

Vercel serverless functions spin up an instance per request. Connecting directly to Postgres in a traditional way exceeds the max connections limit and causes errors to blow up.

Using Neon's serverless pooler can drastically lower connection latency and handle concurrent requests. Duplicate saves must be prevented using an idempotency key.

`typescript
import { Pool } from '@neondatabase/serverless';

const pool = new Pool({ connectionString: process.env.POSTGRES_URL });

export async function insertNormalizedFormsBulk(forms: NormalizedFormData[]) {
const client = await pool.connect();

try {
await client.query('BEGIN');

const insertQuery = `
  INSERT INTO form_responses (
    idempotency_key,
    platform,
    user_id,
    form_id,
    payload,
    created_at
  )
  VALUES ($1, $2, $3, $4, $5, $6)
  ON CONFLICT (idempotency_key) 
  DO UPDATE SET 
    payload = EXCLUDED.payload,
    created_at = EXCLUDED.created_at
  RETURNING id;
`;

for (const form of forms) {
  const idempotencyKey = `${form.platform}:${form.userId}:${form.formId}:${form.submittedAt.getTime()}`;

  await client.query(insertQuery, [
    idempotencyKey,
    form.platform,
    form.userId,
    form.formId,
    JSON.stringify(form.fields),
    form.submittedAt,
  ]);
}

await client.query('COMMIT');

} catch (error) {
await client.query('ROLLBACK');
throw error;
} finally {
client.release();
}
}

`

It is safer for webhooks to quickly return a 200 response and handle the actual storage asynchronously. That is the only way to avoid the 3-second timeout.

Input Exceptions and State Management

Users get exhausted if you just close the modal and throw an error because of a typo. Feedback should be given directly within the open modal.

`typescript
export function formatSlackValidationErrorResponse(zodError: z.ZodError) {
const errorMap: Record<string, string> = {};

for (const issue of zodError.issues) {
const fieldName = issue.path[issue.path.length - 1];
if (fieldName === 'contactEmail') {
errorMap['email_block'] = issue.message;
} else if (fieldName === 'applicantName') {
errorMap['name_block'] = issue.message;
} else if (fieldName === 'description') {
errorMap['desc_block'] = issue.message;
}
}

return {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
response_action: 'errors',
errors: errorMap,
}),
};
}

`

Managing sessions with Redis and setting a 15-minute TTL helps. Collecting error logs reveals that most issues stem from email format or character length limits. Modifying modal hints and adding real-time feedback lowers the input error rate.