1+
2+ # -------------------------------------------------------------------------
3+ # Copyright (c) Microsoft Corporation. All rights reserved.
4+ # Licensed under the MIT License. See License.txt in the project root for
5+ # license information.
6+ # --------------------------------------------------------------------------
7+
8+ """
9+ FILE: callingserver_samples_async.py
10+ DESCRIPTION:
11+ These samples demonstrate create a callingserver client, create a call_connection,
12+ create a server_call_with_calllocator.
13+ The CommunicationIdentityClient is authenticated using a connection string.
14+ The CallingServerClient is authenticated using AzureAD Managed Identity.
15+ USAGE:
16+ python callingserver_samples_async.py
17+ Set the environment variables with your own values before running the sample:
18+ 1) AZURE_COMMUNICATION_SERVICE_ENDPOINT - Communication Service endpoint url
19+ 2) COMMUNICATION_SAMPLES_CONNECTION_STRING - Azure Communication Service resource connection string
20+ """
21+
22+ import os
23+
24+ class CallingServerClientSamplesAsync (object ):
25+ from azure .communication .identity .aio import CommunicationIdentityClient
26+ connection_string = os .environ .get ("COMMUNICATION_SAMPLES_CONNECTION_STRING" , None )
27+ if not connection_string :
28+ raise ValueError ("Set COMMUNICATION_SAMPLES_CONNECTION_STRING env before run this sample." )
29+
30+ identity_client = CommunicationIdentityClient .from_connection_string (connection_string )
31+ user = identity_client .create_user ()
32+
33+ endpoint = os .environ .get ("AZURE_COMMUNICATION_SERVICE_ENDPOINT" , None )
34+ if not endpoint :
35+ raise ValueError ("Set AZURE_COMMUNICATION_SERVICE_ENDPOINT env before run this sample." )
36+
37+ _thread_id = None
38+
39+ def create_callingserver_client_async (self ):
40+ endpoint = self .endpoint
41+ # [START create_callingserver_client]
42+ from azure .communication .callingserver .aio import CallingServerClient
43+ from azure .identity .aio import DefaultAzureCredential
44+
45+ # set `endpoint` to an existing ACS endpoint
46+ calling_server_client_async = CallingServerClient (endpoint , DefaultAzureCredential ())
47+ # [END create_callingserver_client]
48+
49+ async def create_call_connection_async (self ):
50+ endpoint = self .endpoint
51+ from_user = self .user
52+ # [START create_call_connection]
53+ from azure .communication .callingserver import PhoneNumberIdentifier , CreateCallOptions , MediaType , EventSubscriptionType
54+ from azure .communication .callingserver .aio import CallingServerClient
55+ from azure .identity .aio import DefaultAzureCredential
56+ to_user = PhoneNumberIdentifier (self .to_phone_number )
57+ options = CreateCallOptions (
58+ callback_uri = "<your-callback-uri>" ,
59+ requested_media_types = [MediaType .AUDIO ],
60+ requested_call_events = [EventSubscriptionType .PARTICIPANTS_UPDATED , EventSubscriptionType .DTMF_RECEIVED ]
61+ )
62+ # set `endpoint` to an existing ACS endpoint
63+ calling_server_client_async = CallingServerClient (endpoint , DefaultAzureCredential ())
64+ call_connection_async = await calling_server_client_async .create_call_connection (
65+ source = from_user ,
66+ targets = [to_user ],
67+ options = options ,
68+ )
69+ # [END create_call_connection]
70+ # [START hang_up_call]
71+ await call_connection_async .hang_up ()
72+ # [END hang_up_call]
73+
74+ async def create_server_call_with_calllocator_async (self ):
75+ endpoint = self .endpoint
76+ from_user = self .user
77+ # [START create_server_call_with_calllocator]
78+ # set `endpoint` to an existing ACS endpoint
79+ from azure .communication .callingserver import ServerCallLocator , CommunicationUserIdentifier , MediaType , EventSubscriptionType , JoinCallOptions
80+ from azure .communication .callingserver .aio import CallingServerClient
81+ from azure .identity .aio import DefaultAzureCredential
82+
83+ join_options = JoinCallOptions (
84+ callback_uri = "<your-callback-uri>" ,
85+ requested_media_types = [MediaType .AUDIO ],
86+ requested_call_events = [EventSubscriptionType .PARTICIPANTS_UPDATED ]
87+ )
88+ # set `endpoint` to an existing ACS endpoint
89+ calling_server_client_async = CallingServerClient (endpoint , DefaultAzureCredential ())
90+ server_call_id = "<your-server-call-id>"
91+ call_locator = ServerCallLocator (server_call_id )
92+ call_connection_async = await calling_server_client_async .join_call (call_locator , CommunicationUserIdentifier (from_user ), join_options )
93+ # [END create_server_call_with_calllocator]
94+ # [START hang_up_call]
95+ await call_connection_async .hang_up ()
96+ # [END hang_up_call]`
97+
98+ if __name__ == '__main__' :
99+ sample = CallingServerClientSamplesAsync ()
100+ sample .create_callingserver_client_async ()
101+ sample .create_server_call_with_calllocator_async ()
0 commit comments