كيفية التحكم في المكالمات وتوجيهها باستخدام أتمتة المكالمات

تستخدم أتمتة المكالمات واجهة REST API لتلقي طلبات الإجراءات وتقديم استجابات لإعلام ما إذا كان الطلب قد تم إرساله بنجاح أم لا. نظرا للطبيعة غير المتزامنة للاتصال، فإن معظم الإجراءات لها أحداث مقابلة يتم تشغيلها عند اكتمال الإجراء بنجاح أو فشله. يغطي هذا الدليل الإجراءات المتوفرة للمكالمات التوجيهية، مثل CreateCall، ونقل، وإعادة التوجيه، وإدارة المشاركين. تقترن الإجراءات بنموذج التعليمات البرمجية حول كيفية استدعاء الرسم التخطيطي للإجراء والتسلسل المذكور الذي يصف الأحداث المتوقعة بعد استدعاء إجراء. تساعدك هذه الرسومات التخطيطية على تصور كيفية برمجة تطبيق الخدمة باستخدام أتمتة المكالمات.

يدعم أتمتة المكالمات إجراءات أخرى مختلفة لإدارة وسائط المكالمات والتسجيلات التي تحتوي على أدلة منفصلة.

كشرط أساسي، نوصيك بقراءة هذه المقالات لتحقيق أقصى استفادة من هذا الدليل:

  1. دليل مفاهيم أتمتة المكالمات الذي يصف نموذج برمجة أحداث الإجراء واستدعاءات الأحداث.
  2. تعرف على معرفات المستخدم مثل CommunicationUserIdentifier و الهاتف NumberIdentifier المستخدم في هذا الدليل.

لكافة نماذج التعليمات البرمجية، client هو كائن CallAutomationClient الذي يمكن إنشاؤه كما هو موضح وهو callConnection الكائن Call الاتصال ion الذي تم الحصول عليه من استجابة Answer أو CreateCall. يمكنك أيضا الحصول عليه من أحداث رد الاتصال التي يتلقاها التطبيق الخاص بك.

var client = new CallAutomationClient("<resource_connection_string>"); 

إجراء مكالمة صادرة

يمكنك إجراء مكالمة 1:1 أو مكالمة جماعية لمستخدم اتصال أو رقم هاتف (رقم عام أو رقم مملوك لخدمات الاتصالات). عند استدعاء نقطة نهاية PSTN، تحتاج أيضا إلى توفير رقم هاتف يستخدم كمعرف المتصل المصدر ويظهر في إعلام المكالمة إلى نقطة نهاية PSTN الهدف. لوضع استدعاء لمستخدم Communication Services، تحتاج إلى توفير كائن CommunicationUserIdentifier بدلا من الهاتف NumberIdentifier.

Uri callbackUri = new Uri("https://<myendpoint>/Events"); //the callback endpoint where you want to receive subsequent events 
var callerIdNumber = new PhoneNumberIdentifier("+16044561234"); // This is the Azure Communication Services provisioned phone number for the caller  
var callThisPerson = new CallInvite(new PhoneNumberIdentifier("+16041234567"), callerIdNumber); // person to call
CreateCallResult response = await client.CreateCallAsync(callThisPerson, callbackUri);

عند إجراء مكالمة جماعية تتضمن رقم هاتف، يجب توفير رقم هاتف يستخدم كرقم معرف المتصل إلى نقطة نهاية PSTN.

Uri callbackUri = new Uri("https://<myendpoint>/Events"); //the callback endpoint where you want to receive subsequent events 
var pstnEndpoint = new PhoneNumberIdentifier("+16041234567");
var voipEndpoint = new CommunicationUserIdentifier("<user_id_of_target>"); //user id looks like 8:a1b1c1-...
var groupCallOptions = new CreateGroupCallOptions(new List<CommunicationIdentifier>{ pstnEndpoint, voipEndpoint }, callbackUri)
{
    SourceCallerIdNumber = new PhoneNumberIdentifier("+16044561234"), // This is the Azure Communication Services provisioned phone number for the caller
};
CreateCallResult response = await client.CreateGroupCallAsync(groupCallOptions);

توفر لك الاستجابة كائن Call الاتصال ion الذي يمكنك استخدامه لاتخاذ مزيد من الإجراءات على هذا الاستدعاء بمجرد توصيله. بمجرد الرد على المكالمة، يتم نشر حدثين إلى نقطة نهاية رد الاتصال التي قدمتها سابقا:

  1. CallConnected حدث يعلم بأن المكالمة قد تم إنشاؤها مع المتصل.
  2. ParticipantsUpdated الحدث الذي يحتوي على أحدث قائمة بالمشاركين في المكالمة. Sequence diagram for placing an outbound call.

الرد على مكالمة واردة

بمجرد الاشتراك في تلقي إعلامات المكالمات الواردة إلى المورد الخاص بك، سوف تجيب على مكالمة واردة. عند الرد على مكالمة، من الضروري توفير عنوان URL لرد الاتصال. تنشر Communication Services جميع الأحداث اللاحقة حول هذا الاستدعاء إلى عنوان url هذا.

string incomingCallContext = "<IncomingCallContext_From_IncomingCall_Event>"; 
Uri callBackUri = new Uri("https://<myendpoint_where_I_want_to_receive_callback_events"); 

var answerCallOptions = new AnswerCallOptions(incomingCallContext, callBackUri);  
AnswerCallResult answerResponse = await client.AnswerCallAsync(answerCallOptions);
CallConnection callConnection = answerResponse.CallConnection; 

توفر لك الاستجابة كائن Call الاتصال ion الذي يمكنك استخدامه لاتخاذ مزيد من الإجراءات على هذا الاستدعاء بمجرد توصيله. بمجرد الرد على المكالمة، يتم نشر حدثين إلى نقطة نهاية رد الاتصال التي قدمتها سابقا:

  1. CallConnected حدث يعلم بأن المكالمة قد تم إنشاؤها مع المتصل.
  2. ParticipantsUpdated الحدث الذي يحتوي على أحدث قائمة بالمشاركين في المكالمة.

Sequence diagram for answering an incoming call.

رفض مكالمة

يمكنك اختيار رفض مكالمة واردة كما هو موضح أدناه. يمكنك تقديم سبب رفض: لا شيء أو مشغول أو محظور. إذا لم يتم توفير أي شيء، يتم اختيار أي منها بشكل افتراضي.

string incomingCallContext = "<IncomingCallContext_From_IncomingCall_Event>"; 
var rejectOption = new RejectCallOptions(incomingCallContext); 
rejectOption.CallRejectReason = CallRejectReason.Forbidden; 
_ = await client.RejectCallAsync(rejectOption); 

لا يتم نشر أي أحداث لإجراء الرفض.

إعادة توجيه مكالمة

يمكنك اختيار إعادة توجيه مكالمة واردة إلى نقطة نهاية أخرى دون الرد عليها. تؤدي إعادة توجيه مكالمة إلى إزالة قدرة تطبيقك على التحكم في المكالمة باستخدام أتمتة المكالمات.

string incomingCallContext = "<IncomingCallContext_From_IncomingCall_Event>"; 
var target = new CallInvite(new CommunicationUserIdentifier("<user_id_of_target>")); //user id looks like 8:a1b1c1-... 
_ = await client.RedirectCallAsync(incomingCallContext, target); 

لإعادة توجيه المكالمة إلى رقم هاتف، قم بإنشاء معرف الهدف والمتصل باستخدام الهاتف NumberIdentifier.

var callerIdNumber = new PhoneNumberIdentifier("+16044561234"); // This is the Azure Communication Services provisioned phone number for the caller
var target = new CallInvite(new PhoneNumberIdentifier("+16041234567"), callerIdNumber);

لا يتم نشر أي أحداث لإعادة التوجيه. إذا كان الهدف هو مستخدم Communication Services أو رقم هاتف مملوك لموردك، فإنه ينشئ حدث IncomingCall جديدا مع تعيين الحقل "إلى" إلى الهدف الذي حددته.

تحويل مشارك في المكالمة

عندما يجيب تطبيقك على مكالمة أو يضع مكالمة صادرة إلى نقطة نهاية، يمكن نقل نقطة النهاية هذه إلى نقطة نهاية وجهة أخرى. يؤدي نقل مكالمة 1:1 إلى إزالة التطبيق من المكالمة وبالتالي إزالة قدرته على التحكم في المكالمة باستخدام أتمتة المكالمات. ستعرض دعوة المكالمة إلى الهدف معرف المتصل لنقطة النهاية التي يتم نقلها. توفير معرف المتصل المخصص غير معتمد.

var transferDestination = new CommunicationUserIdentifier("<user_id>"); 
var transferOption = new TransferToParticipantOptions(transferDestination) {
    OperationContext = "<Your_context>",
    OperationCallbackUri = new Uri("<uri_endpoint>") // Sending event to a non-default endpoint.
};
// adding customCallingContext
transferOption.CustomCallingContext.AddVoip("customVoipHeader1", "customVoipHeaderValue1");
transferOption.CustomCallingContext.AddVoip("customVoipHeader2", "customVoipHeaderValue2");

TransferCallToParticipantResult result = await callConnection.TransferCallToParticipantAsync(transferOption);

عندما يجيب تطبيقك على مكالمة جماعية أو يضع مكالمة جماعية صادرة إلى نقطة نهاية أو يضيف مشاركا إلى مكالمة 1:1، يمكن نقل نقطة نهاية من المكالمة إلى نقطة نهاية وجهة أخرى، باستثناء نقطة نهاية أتمتة المكالمات. يؤدي نقل مشارك في مكالمة جماعية إلى إزالة نقطة النهاية التي يتم نقلها من المكالمة. ستعرض دعوة المكالمة إلى الهدف معرف المتصل لنقطة النهاية التي يتم نقلها. توفير معرف المتصل المخصص غير معتمد.

// Transfer User
var transferDestination = new CommunicationUserIdentifier("<user_id>");
var transferee = new CommunicationUserIdentifier("<transferee_user_id>"); 
var transferOption = new TransferToParticipantOptions(transferDestination);
transferOption.Transferee = transferee;

// adding customCallingContext
transferOption.CustomCallingContext.AddVoip("customVoipHeader1", "customVoipHeaderValue1");
transferOption.CustomCallingContext.AddVoip("customVoipHeader2", "customVoipHeaderValue2");

transferOption.OperationContext = "<Your_context>";
transferOption.OperationCallbackUri = new Uri("<uri_endpoint>");
TransferCallToParticipantResult result = await callConnection.TransferCallToParticipantAsync(transferOption);

// Transfer PSTN User
var transferDestination = new PhoneNumberIdentifier("<target_phoneNumber>");
var transferee = new PhoneNumberIdentifier("<transferee_phoneNumber>"); 
var transferOption = new TransferToParticipantOptions(transferDestination);
transferOption.Transferee = transferee;

// adding customCallingContext
transferOption.CustomCallingContext.AddSipUui("uuivalue");
transferOption.CustomCallingContext.AddSipX("header1", "headerValue");

transferOption.OperationContext = "<Your_context>";

// Sending event to a non-default endpoint.
transferOption.OperationCallbackUri = new Uri("<uri_endpoint>");

TransferCallToParticipantResult result = await callConnection.TransferCallToParticipantAsync(transferOption);

يوضح الرسم التخطيطي التسلسلي التدفق المتوقع عندما يقوم التطبيق الخاص بك بوضع مكالمة صادرة ثم نقلها إلى نقطة نهاية أخرى.

Sequence diagram for placing a 1:1 call and then transferring it.

إضافة مشارك إلى مكالمة

يمكنك إضافة مشارك (مستخدم أو رقم هاتف Communication Services) إلى مكالمة موجودة. عند إضافة رقم هاتف، من الضروري توفير معرف المتصل. يظهر معرف المتصل هذا عند إعلام المكالمة إلى المشارك الذي تتم إضافته.

// Add user
var addThisPerson = new CallInvite(new CommunicationUserIdentifier("<user_id>"));
// add custom calling context
addThisPerson.CustomCallingContext.AddVoip("myHeader", "myValue");
AddParticipantsResult result = await callConnection.AddParticipantAsync(addThisPerson);

// Add PSTN user
var callerIdNumber = new PhoneNumberIdentifier("+16044561234"); // This is the Azure Communication Services provisioned phone number for the caller
var addThisPerson = new CallInvite(new PhoneNumberIdentifier("+16041234567"), callerIdNumber);
// add custom calling context
addThisPerson.CustomCallingContext.AddSipUui("value");
addThisPerson.CustomCallingContext.AddSipX("header1", "customSipHeaderValue1");

// Use option bag to set optional parameters
var addParticipantOptions = new AddParticipantOptions(new CallInvite(addThisPerson))
{
    InvitationTimeoutInSeconds = 60,
    OperationContext = "operationContext",
    OperationCallbackUri = new Uri("uri_endpoint"); // Sending event to a non-default endpoint.
};

AddParticipantsResult result = await callConnection.AddParticipantAsync(addParticipantOptions); 

لإضافة مستخدم Communication Services، قم بتوفير CommunicationUserIdentifier بدلا من الهاتف NumberIdentifier. معرف المتصل ليس إلزاميا في هذه الحالة.

ينشر AddParticipant حدثا AddParticipantSucceeded أو AddParticipantFailed ، إلى جانب ParticipantUpdated توفير أحدث قائمة بالمشاركين في المكالمة.

Sequence diagram for adding a participant to the call.

إلغاء طلب إضافة مشارك

// add a participant
var addThisPerson = new CallInvite(new CommunicationUserIdentifier("<user_id>"));
var addParticipantResponse = await callConnection.AddParticipantAsync(addThisPerson);

// cancel the request with optional parameters
var cancelAddParticipantOperationOptions = new CancelAddParticipantOperationOptions(addParticipantResponse.Value.InvitationId)
{
    OperationContext = "operationContext",
    OperationCallbackUri = new Uri("uri_endpoint"); // Sending event to a non-default endpoint.
}
await callConnection.CancelAddParticipantOperationAsync(cancelAddParticipantOperationOptions);

إزالة مشارك من مكالمة

var removeThisUser = new CommunicationUserIdentifier("<user_id>"); 

// remove a participant from the call with optional parameters
var removeParticipantOptions = new RemoveParticipantOptions(removeThisUser)
{
    OperationContext = "operationContext",
    OperationCallbackUri = new Uri("uri_endpoint"); // Sending event to a non-default endpoint.
}

RemoveParticipantsResult result = await callConnection.RemoveParticipantAsync(removeParticipantOptions);

سيقوم RemoveParticipant بنشر RemoveParticipantSucceeded حدث أو RemoveParticipantFailed ، إلى جانب ParticipantUpdated حدث يوفر أحدث قائمة بالمشاركين في المكالمة. يتم حذف المشارك الذي تمت إزالته من القائمة.
Sequence diagram for removing a participant from the call.

قطع المكالمة

يمكن استخدام إجراء قطع الاتصال لإزالة التطبيق الخاص بك من المكالمة أو لإنهاء مكالمة جماعية عن طريق تعيين معلمةEveryone إلى true. لإجراء مكالمة 1:1، ينهي قطع المكالمة مع المشارك الآخر بشكل افتراضي.

_ = await callConnection.HangUpAsync(forEveryone: true); 

يتم نشر الحدث CallDisconnected بمجرد اكتمال إجراء hangUp بنجاح.

الحصول على معلومات حول مشارك في مكالمة

CallParticipant participantInfo = await callConnection.GetParticipantAsync(new CommunicationUserIdentifier("<user_id>"));

الحصول على معلومات حول جميع المشاركين في المكالمات

List<CallParticipant> participantList = (await callConnection.GetParticipantsAsync()).Value.ToList(); 

الحصول على أحدث المعلومات حول مكالمة

CallConnectionProperties callConnectionProperties = await callConnection.GetCallConnectionPropertiesAsync();