Advanced متقدم 7 min 7 دق

Dynamic User Context (payload) السياق الديناميكي للمستخدم (payload)

Pass real-time user data to your AI bot — name, account ID, plan, balance — so it can personalize every response using the chat API payload field. أرسل بيانات المستخدم الفعلية للبوت — الاسم ورقم الحساب والباقة والرصيد — ليخصّص كل رد عبر حقل payload في API الدردشة.

The payload field lets you pass dynamic, real-time user context — like their name, account ID, subscription plan, or balance — along with each chat message. The AI uses this data to give personalized, relevant answers instead of generic ones.

💡 Use case: A telecom app sends the subscriber's plan and balance with every message. The bot can then answer "Your FTTH 100MB plan renews in 3 days and your balance is 15.5 IQD" without querying any external system.

How It Works

?
  1. Add a payload object to your message request
  2. Mugib stores it in Redis for the entire session lifetime
  3. On every message, the payload is injected as a [User Context] block at the top of the system prompt
  4. The AI reads this context and uses it to personalize its response

Step 1 — Create a Session

1

Start with a normal session creation:

curl -X POST "https://api.mugib.com/api/v1/sessions" \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json"

# Response:
# { "session_id": "chat_dc3ca9ae" }

Step 2 — Send a Message with Payload

2

Add a payload object to your first message. Any JSON keys are accepted — choose what's meaningful for your use case:

curl -X POST "https://api.mugib.com/api/v1/sessions/chat_dc3ca9ae/messages" \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "message": "What is my current plan?",
    "payload": {
      "name": "Ahmed Al-Rashidi",
      "account_id": "NBT-4521",
      "plan": "FTTH 100MB",
      "balance": 15.5,
      "renewal_days": 3
    }
  }'

The bot will use this context in its reply:

// Response JSON
{
  "reply": "Hello Ahmed! Your current plan is FTTH 100MB.
Your balance is 15.5 IQD and your plan renews in 3 days.
Would you like to renew now or upgrade to a higher tier?",
  "kb_sources": [
    { "id": "42", "title": "FTTH Plans Overview", "category": "Plans" }
  ]
}

Step 3 — Subsequent Messages (No Need to Repeat)

3

Once stored, the payload persists for the full session. You don't need to send it again on every message:

curl -X POST "https://api.mugib.com/api/v1/sessions/chat_dc3ca9ae/messages" \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "message": "Can I upgrade my plan?" }'
# The bot still knows the user's name, plan, balance, etc.

JavaScript / TypeScript Example

4
// Send payload once on the first message
const sendMessage = async (sessionId, message, payload = null) => {
  const body = { message };
  if (payload) body.payload = payload;

  const res = await fetch(
    "https://api.mugib.com/api/v1/sessions/" + sessionId + "/messages",
    {
      method: "POST",
      headers: {
        "X-API-Key": "YOUR_API_KEY",
        "Content-Type": "application/json",
      },
      body: JSON.stringify(body),
    }
  );
  return res.json();
};

// First message — include user context
await sendMessage("chat_dc3ca9ae", "Hello!", {
  name: 'Ahmed Al-Rashidi',
  account_id: 'NBT-4521',
  plan: 'FTTH 100MB',
  balance: 15.5,
});

// Subsequent messages — no payload needed
await sendMessage("chat_dc3ca9ae", "Can I upgrade my plan?");

Python Example

5
import requests

BASE = "https://api.mugib.com/api/v1"
HEADERS = {"X-API-Key": "YOUR_API_KEY"}

# Create session
session_id = requests.post(BASE + "/sessions", headers=HEADERS).json()["session_id"]

# First message with payload
url = BASE + "/sessions/" + session_id + "/messages"
res = requests.post(url, headers=HEADERS, json={
    "message": "What is my plan status?",
    "payload": {
        "name": "Ahmed Al-Rashidi",
        "account_id": "NBT-4521",
        "plan": "FTTH 100MB",
        "balance": 15.5,
    },
})
print(res.json()["reply"])

# Follow-up — no payload required
res2 = requests.post(url, headers=HEADERS, json={"message": "How do I renew?"})
print(res2.json()["reply"])

Key Behaviors

?
  • Optional — omitting payload is perfectly fine. Existing integrations are not affected.
  • Persistent — stored in Redis for the session lifetime (same TTL as conversation history).
  • Updatable — send a new payload on any message to replace the stored one (e.g. after a plan upgrade).
  • Any JSON object — keys and value types are free-form. Use whatever fields your app has.
  • Works with streaming — the ?stream=true SSE endpoint supports payload too.
⚠️ System Prompt Required: The payload is injected as context, but your bot's System Prompt must allow it to use personal information. If the prompt is overly restrictive (e.g. "only answer about our products"), the bot may ignore the payload. Open your project → Configure Bot → update the system prompt to instruct the bot to greet the user by name and reference their account details when available.

حقل payload يسمح لك بإرسال بيانات ديناميكية عن المستخدم — مثل اسمه ورقم حسابه وباقته ورصيده — مع كل رسالة دردشة. يستخدم الذكاء الاصطناعي هذه البيانات لتقديم إجابات شخصية ودقيقة بدلاً من الردود العامة.

💡 مثال عملي: تطبيق اتصالات يُرسل باقة المشترك ورصيده مع كل رسالة. يقدر البوت يرد: "باقتك FTTH 100 ميجا تتجدد بعد 3 أيام ورصيدك 15.5 دينار" دون الحاجة للاستعلام من نظام خارجي.

كيف يعمل؟

?
  1. أضف كائن payload لطلب الرسالة
  2. مجيب يحفظه في Redis طول مدة الجلسة
  3. في كل رسالة، يُحقن الـ payload كبلوك [User Context] في بداية الـ System Prompt
  4. الذكاء الاصطناعي يقرأ هذا السياق ويستخدمه لتخصيص الرد

الخطوة 1 — إنشاء جلسة

1

ابدأ بإنشاء جلسة عادي:

curl -X POST "https://api.mugib.com/api/v1/sessions" \
  -H "X-API-Key: YOUR_API_KEY"

# الرد:
# { "session_id": "chat_dc3ca9ae" }

الخطوة 2 — إرسال رسالة مع Payload

2

أضف كائن payload لأول رسالة. أي مفاتيح JSON مقبولة — اختر ما يناسب تطبيقك:

curl -X POST "https://api.mugib.com/api/v1/sessions/chat_dc3ca9ae/messages" \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "message": "شنو باقتي الحالية؟",
    "payload": {
      "name": "أحمد الراشدي",
      "account_id": "NBT-4521",
      "plan": "FTTH 100 ميجا",
      "balance": 15.5,
      "renewal_days": 3
    }
  }'

البوت يستخدم هذا السياق في رده:

// Response JSON
{
  "reply": "مرحباً أحمد! باقتك الحالية FTTH 100 ميجا.
رصيدك 15.5 دينار وتنتهي باقتك بعد 3 أيام.
تريد تجديد الآن أو الترقية لباقة أعلى؟"
}

الخطوة 3 — الرسائل التالية (بدون إعادة الإرسال)

3

بعد حفظ الـ payload، يبقى طول الجلسة. ما تحتاج ترسله في كل رسالة:

curl -X POST "https://api.mugib.com/api/v1/sessions/chat_dc3ca9ae/messages" \
  -H "X-API-Key: YOUR_API_KEY" \
  -d '{ "message": "أقدر أرفّع باقتي؟" }'
# البوت لا يزال يعرف اسم المستخدم وباقته ورصيده

مثال JavaScript

4
const sendMessage = async (sessionId, message, payload = null) => {
  const body = { message };
  if (payload) body.payload = payload;

  const res = await fetch(
    "https://api.mugib.com/api/v1/sessions/" + sessionId + "/messages",
    {
      method: "POST",
      headers: { "X-API-Key": "YOUR_API_KEY", "Content-Type": "application/json" },
      body: JSON.stringify(body),
    }
  );
  return res.json();
};

// الرسالة الأولى — أرسل بيانات المستخدم
await sendMessage("chat_dc3ca9ae", "مرحبا!", {
  name: "أحمد الراشدي",
  account_id: "NBT-4521",
  plan: "FTTH 100 ميجا",
  balance: 15.5,
});

// الرسائل التالية — بدون payload
await sendMessage("chat_dc3ca9ae", "أقدر أرفّع باقتي؟");

السلوكيات الرئيسية

?
  • اختياري — حذف payload طبيعي تماماً. التكاملات القديمة لا تتأثر.
  • دائم — محفوظ في Redis طول مدة الجلسة.
  • قابل للتحديث — أرسل payload جديد في أي رسالة لتحديث المحفوظ (مثلاً بعد ترقية الباقة).
  • أي JSON — المفاتيح والقيم حرة. استخدم ما يناسب تطبيقك.
  • يدعم الـ Streaming — endpoint الـ ?stream=true يدعم payload كذلك.
⚠️ الـ System Prompt مهم: الـ payload يُحقن كسياق، لكن الـ System Prompt مالتك يجب أن يسمح للبوت باستخدام المعلومات الشخصية. إذا كان الـ prompt مقيّداً جداً، قد يتجاهل البوت الـ payload. افتح مشروعك ← إعداد البوت ← حدّث الـ prompt ليحيّي المستخدم بالاسم ويشير لمعلومات حسابه عند توفرها.

Ready to build your AI agent?

Start your free trial and put what you've learned into practice.

Start Free Trial

جاهز لبناء وكيل AI خاص بك؟

ابدأ تجربتك المجانية وطبّق ما تعلّمته.

ابدأ مجاناً
Ready to automate your support? Start free — no credit card required
Start Free
Try Voice AI