Skip to main content

Function Creation and Webhook Setup Guide

Introduction

This guide explains how to create functions for your Peki chatbot and set up a webhook server to handle data processing. Functions allow your chatbot to collect specific information from users and send this data to your webhook server for processing.

Dashboard Access

The Peki Dashboard is accessible at:

https://dashboard.peki.ai/dashboard/{chatbotId}/chatbot/functions

Important: Replace {chatbotId} with your actual chatbot ID from the Peki platform.

Process Overview

  1. First, we'll create a function using the Peki Dashboard
  2. Then, we'll set up a webhook server to process the data
  3. Finally, we'll test the integration

When a user interacts with your chatbot:

  • The AI collects required information using defined properties
  • The data is sent to your webhook server using the HTTP Action
  • Your server processes the data and sends back a response
  • The AI presents the results to the user using the response template

Function Configuration Fields

1. Basic Information

FieldExample ValueDescription
NameCredit Limit CheckDisplay name of the function
DescriptionImportant: Collects necessary parameters from user to check credit score and determine limit.Detailed explanation of function's purpose

2. Properties Configuration

Each property requires the following fields:

FieldTypeRequiredDescription
NameStringYesUnique identifier for the property
DescriptionStringYesInstructions for AI to collect the information
IsRequiredBooleanYesWhether this property must be collected

Example Properties Table

NameDescriptionIsRequired
bankName of the bank to check credit scoreYes
loanAmountAmount of loan to be takenYes

3. HTTP Action Configuration

FieldExample ValueDescription
NameHttp ActionDisplay name of the action
Urlhttp://localhost:3001/webhook/creditEndpoint URL for the webhook
MethodPOSTHTTP method (GET or POST)

4. Response Configuration

FieldExample ValueDescription
ResponseSummarize the response from {bank} for loan amount {loanAmount}Template for AI to format response

Function Creation Process

1. Access Dashboard

  • Navigate to the Peki Dashboard using your chatbot ID
  • Click "Create New Function"

2. Configure Basic Settings

Field to FillWhat to Enter
NameYour function name (e.g., "Credit Limit Check")
DescriptionClear explanation of function purpose

3. Add Properties

StepAction
1Click "Add Property"
2Enter property name (e.g., "bank")
3Write clear description
4Set Required flag
5Repeat for all properties

4. Configure HTTP Action

StepAction
1Enter webhook URL
2Select HTTP method
3Save action settings

5. Set Response Template

StepAction
1Write response template using {propertyName} syntax
2Test template with sample values

Webhook Setup

1. Basic Node.js Server Setup

const express = require("express");
const bodyParser = require("body-parser");

const app = express();
app.use(bodyParser.json());

2. Create Webhook Endpoint

app.post("/webhook/credit", async (req, res) => {
try {
const { arguments } = req.body;

// Extract user and function data
const {
chatUserId, // Unique chat user ID
platformId, // User ID from script
bank, // Collected by AI
loanAmount, // Collected by AI
} = arguments;

// Process credit check logic
const creditResult = await processCreditCheck({
userId: platformId,
bank,
loanAmount,
});

// Send response
res.json({
success: true,
data: creditResult,
});
} catch (error) {
console.error("Webhook error:", error);
res.status(500).json({
success: false,
message: error.message,
});
}
});

3. Start Server

const PORT = process.env.PORT || 3001;
app.listen(PORT, () => {
console.log(`Webhook server running on port ${PORT}`);
});

Function Execution Flow

StepDescription
1. AI TriggerAI recognizes intent and starts property collection
2. Data CollectionAI asks user for each required property
3. ValidationSystem ensures all required properties are collected
4. Webhook CallSystem sends data to webhook URL
5. ProcessingWebhook processes request and returns result
6. ResponseAI formats response using template and presents to user

Best Practices

1. Property Naming

DoDon't
Use clear, descriptive namesUse generic names
Use camelCase or snake_caseUse spaces or special characters
Keep names conciseUse very long names

2. Property Descriptions

DoDon't
Write clear instructionsUse technical jargon
Include format requirementsBe vague or unclear
Specify any constraintsAssume AI understands context

3. Webhook Implementation

PracticeDescription
SecurityUse HTTPS in production
ValidationCheck all incoming data
Error HandlingImplement proper error handling
LoggingLog all requests and responses
TimeoutsInclude timeout handling

4. Testing

Test CaseWhat to Check
Happy PathBasic successful flow
Missing DataHandling of missing properties
Invalid DataError handling for invalid input
Response FormatCorrect template processing

Troubleshooting

IssueSolution
Webhook not receiving dataCheck URL and network settings
Missing propertiesVerify property configuration
Response not formattingCheck template syntax
Server errorsCheck logs and error handling