Ana içeriğe geç

HTTP İşlemleri

Pi v2'de HTTP istekleri göndermek için kullanılan fonksiyonlar.

get(url, headers?)

HTTP GET isteği gönderir.

Parametreler:

  • url (string) - İstek gönderilecek URL
  • headers (object, opsiyonel) - HTTP başlıkları

Dönüş Değeri:

  • Promise<string> - Yanıt içeriği (string olarak)

Örnek:

// Basit GET isteği
const response = await get("https://api.example.com/data");
await addLog("Yanıt:", response);

// Başlıklar ile GET isteği
const headers = {
"Authorization": "Bearer token123",
"Content-Type": "application/json"
};
const response = await get("https://api.example.com/protected", headers);

// JSON yanıtı işleme
try {
const jsonResponse = await get("https://api.example.com/users");
const data = str2JSON(jsonResponse);
await addLog("Kullanıcı sayısı:", data.length);
} catch (error) {
console.error("API hatası:", error);
}

post(url, data, headers?)

HTTP POST isteği gönderir.

Parametreler:

  • url (string) - İstek gönderilecek URL
  • data (any) - Gönderilecek veri
  • headers (object, opsiyonel) - HTTP başlıkları

Dönüş Değeri:

  • Promise<string> - Yanıt içeriği (string olarak)

Örnek:

// JSON veri gönderme
const userData = {
name: "Ahmet",
phone: $arayan,
timestamp: new Date().toISOString()
};

const headers = {
"Content-Type": "application/json"
};

try {
const response = await post(
"https://api.example.com/users",
JSON2str(userData),
headers
);
await addLog("Kullanıcı oluşturuldu:", response);
} catch (error) {
await addLog("POST hatası:", error);
}

// Form verisi gönderme
const formData = "name=Ahmet&phone=" + encodeURIComponent($arayan);
const formHeaders = {
"Content-Type": "application/x-www-form-urlencoded"
};

const response = await post(
"https://api.example.com/form",
formData,
formHeaders
);

// API anahtarı ile kimlik doğrulama
const apiData = {
action: "log_call",
caller: $arayan,
called: $aranan,
app_id: $uygulamaId
};

const authHeaders = {
"Authorization": "Bearer " + $apiKey,
"Content-Type": "application/json"
};

const logResponse = await post(
"https://logging.example.com/api/calls",
JSON2str(apiData),
authHeaders
);

Hata Yönetimi

HTTP istekleri sırasında oluşabilecek hataları yakalamak için try-catch kullanın:
try {
const response = await get("https://api.example.com/data");
// Başarılı yanıt işleme
const data = str2JSON(response);
await tts(`Veri alındı: ${data.message}`);
} catch (error) {
// Hata durumu işleme
await addLog("HTTP hatası:", error.message);
await tts("Üzgünüz, bir hata oluştu");
}

Yaygın Kullanım Örnekleri

CRM Entegrasyonu

// Müşteri bilgilerini CRM'den al
const customerInfo = await get(
`https://crm.example.com/api/customer/${$arayan}`
);
const customer = str2JSON(customerInfo);

if (customer.name) {
await tts(`Merhaba ${customer.name}, size nasıl yardımcı olabilirim?`);
} else {
await tts("Merhaba, size nasıl yardımcı olabilirim?");
}

Çağrı Kayıt Sistemi

// Çağrı başlangıcını kaydet
const callData = {
caller: $arayan,
called: $aranan,
start_time: new Date().toISOString(),
app_id: $uygulamaId
};

await post(
"https://calllog.example.com/api/start",
JSON2str(callData),
{ "Content-Type": "application/json" }
);

Webhook Bildirimleri

// Önemli olayları webhook ile bildir
const webhookData = {
event: "call_answered",
caller: $arayan,
timestamp: new Date().toISOString(),
channel: $kanal
};

await post(
"https://webhook.example.com/notify",
JSON2str(webhookData),
{ "Content-Type": "application/json" }
);