تماس خروجی
نمونه کدهای آماده تماس خروجی
نمونه کدهای اجرایی و تستشده برای اتصال و برقراری تماس خروجی در cURL, Python, Node.js, PHP و Go
نمونه کدهای آماده و کامل در ۵ زبان و پلتفرم متداول جهت پیادهسازی سریع وبسرویس تماس خروجی در زیر آماده شده است. زبانه مورد نظر خود را انتخاب فرمایید:
curl -X POST "https://api.poshtyar.com/api/v1/external/calls/originate" \
-H "Content-Type: application/json" \
-H "X-API-KEY: poshtyar_live_YOUR_API_KEY" \
-d '{
"phone_number": "09123456789",
"operator_id": "66f10c3b8417d3b5b1234567",
"recipient_name": "علی احمدی",
"topic": "خوشآمدگویی و پیگیری ثبتنام",
"description": "کاربر به تازگی در وبسایت عضو شده است. به او خوشآمد بگو و بپرس آیا نیاز به راهنمایی دارد؟",
"opener_text": "سلام جناب احمدی وقت بخیر، از پشتیبانی تماس میگیرم جهت خوشآمدگویی.",
"retry_policy": [300, 1800, 7200]
}'import requests
API_URL = "https://api.poshtyar.com/api/v1/external/calls/originate"
API_KEY = "poshtyar_live_YOUR_API_KEY"
headers = {
"Content-Type": "application/json",
"X-API-KEY": API_KEY
}
payload = {
"phone_number": "09123456789",
"operator_id": "66f10c3b8417d3b5b1234567",
"recipient_name": "علی احمدی",
"topic": "تایید سفارش و هماهنگی ارسال",
"description": "سفارش هدفون بیسیم ثبت شده، از خریدار تشکر کن و آدرس را تایید بگیر.",
"opener_text": "سلام وقت بخیر، در خصوص سفارش ثبتشده شما تماس گرفتم.",
"retry_policy": [300, 1800, 7200] # تلاش مجدد در فواصل ۵ دقیقه، ۳۰ دقیقه و ۲ ساعت
}
response = requests.post(API_URL, json=payload, headers=headers)
if response.status_code == 200:
print("تماس در صف شمارهگیری قرار گرفت:", response.json())
else:
print(f"خطا ({response.status_code}):", response.text)interface OriginatePayload {
phone_number: string;
operator_id: string;
recipient_name?: string;
topic?: string;
description?: string;
opener_text?: string;
retry_policy?: number[];
outbound_type?: 'ai' | 'audio_file';
}
async function placeOutboundCall(payload: OriginatePayload) {
const API_URL = "https://api.poshtyar.com/api/v1/external/calls/originate";
const API_KEY = process.env.POSHTYAR_API_KEY || "poshtyar_live_YOUR_API_KEY";
const response = await fetch(API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-KEY": API_KEY,
},
body: JSON.stringify(payload),
});
const result = await response.json();
if (!response.ok) {
throw new Error(`خطا در برقراری تماس: ${result.error || response.statusText}`);
}
return result;
}
// نمونه فراخوانی تابع
placeOutboundCall({
phone_number: "09123456789",
operator_id: "66f10c3b8417d3b5b1234567",
recipient_name: "سارا حسینی",
topic: "پیگیری سبد خرید رها شده",
description: "مشتری محصول را در سبد خرید رها کرده است، علت را جویا شو و کد تخفیف ۱۰ درصدی ارائه بده.",
retry_policy: [600, 3600]
});<?php
$url = "https://api.poshtyar.com/api/v1/external/calls/originate";
$apiKey = "poshtyar_live_YOUR_API_KEY";
$data = [
"phone_number" => "09123456789",
"operator_id" => "66f10c3b8417d3b5b1234567",
"recipient_name" => "محمد رضایی",
"topic" => "هماهنگی رزرو و نوبت",
"description" => "نوبت ویزیت برای روز دوشنبه ساعت ۱۷ رزرو شده، حضور ایشان را جویا شو.",
"opener_text" => "سلام جناب رضایی وقت بخیر، جهت تایید نوبت دوشنبه تماس گرفتم.",
"retry_policy" => [300, 1800, 7200]
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"X-API-KEY: " . $apiKey
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
echo "تماس با موفقیت برقرار شد: " . $response;
} else {
echo "خطا در برقراری تماس ($httpCode): " . $response;
}
?>package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
type OriginateRequest struct {
PhoneNumber string `json:"phone_number"`
OperatorID string `json:"operator_id"`
RecipientName string `json:"recipient_name,omitempty"`
Topic string `json:"topic,omitempty"`
Description string `json:"description,omitempty"`
OpenerText string `json:"opener_text,omitempty"`
RetryPolicy []int `json:"retry_policy,omitempty"`
OutboundType string `json:"outbound_type,omitempty"`
}
func main() {
url := "https://api.poshtyar.com/api/v1/external/calls/originate"
apiKey := "poshtyar_live_YOUR_API_KEY"
reqBody := OriginateRequest{
PhoneNumber: "09123456789",
OperatorID: "66f10c3b8417d3b5b1234567",
RecipientName: "مهندس علیزاده",
Topic: "پیگیری تیکت پشتیبانی",
Description: "تیکت شماره ۴۸۵ پاسخ داده شد، جویا شو آیا مشکل مرتفع گردیده است یا خیر.",
RetryPolicy: []int{300, 1800},
OutboundType: "ai",
}
jsonData, err := json.Marshal(reqBody)
if err != nil {
panic(err)
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-API-KEY", apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Printf("Status: %d\nResponse: %s\n", resp.StatusCode, string(body))
}