Skip to content

agent #31

@MJ0612

Description

@MJ0612

// index.js (Node.js, Express)
import express from "express";
import bodyParser from "body-parser";
import fetch from "node-fetch";

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

// Replace with your WhatsApp provider webhook handler
app.post("/webhook/whatsapp", async (req, res) => {
const message = req.body.message; // adapt to provider payload
const from = req.body.from;
const text = (message.text || "").trim();

// Simple rule based first
if (/hi|hello|hey/i.test(text)) {
await sendWhatsApp(from, "Hi! I’m KK-Agent. I can give a quick quote or compare plans. Reply: 1 for quote, 2 for compare, 3 for claim help.");
return res.sendStatus(200);
}

// For other texts, call model
const prompt = You are KK-Agent for an insurance consultant firm. User: "${text}". Respond concisely in friendly Hindi/English.;
const reply = await callOpenAIChat(prompt);
await sendWhatsApp(from, reply);
res.sendStatus(200);
});

async function callOpenAIChat(prompt) {
// Replace with your OpenAI chat completions call
const resp = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: { "Content-Type":"application/json", Authorization: Bearer ${process.env.OPENAI_KEY} },
body: JSON.stringify({
model: "gpt-5-thinking-mini", // example model name
messages: [{role:"system", content:"You are KK-Agent."},{role:"user", content:prompt}],
max_tokens: 400
})
});
const data = await resp.json();
return data.choices?.[0]?.message?.content ?? "Sorry, something went wrong.";
}

async function sendWhatsApp(to, text) {
// Implement using Twilio / Gupshup API
console.log("Send to", to, text);
}

app.listen(3000, () => console.log("Server running 3000"));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions