skip to Main Content

I am trying to use Azure Communication Services to create a user identity and then fetch relay configuration for network traversal.
Here is the code:

const { CommunicationIdentityClient } = require("@azure/communication-identity");
const { CommunicationRelayClient } = require("@azure/communication-network-traversal");;

const main = async () => {
  console.log("Azure Communication Services - Relay Token Quickstart")

  const connectionString = "CONNECTION STRING"
 // Instantiate the identity client
  const identityClient = new CommunicationIdentityClient(connectionString);

  let identityResponse = await identityClient.createUser();
  console.log(`nCreated an identity with ID: ${identityResponse.communicationUserId}`);

  const relayClient = new CommunicationRelayClient(connectionString);
  console.log("Getting relay configuration");

  const config = await relayClient.getRelayConfiguration(identityResponse);
  console.log("RelayConfig", config);

  console.log("Printing entire thing ",config.iceServers);  
};

main().catch((error) => {
  console.log("Encountered and error");
  console.log(error);
})

I got error as

<h2>Our services aren't available right now</h2><p>We're working to restore all services as soon as possible. Please check back soon.</p>

I searched on community forum and tried to implement the solution but got the same error.

  • The SOLUTION described was:

**Solution:
"we cannot directly access the endpoint as a rest API endpoint there are extensions to it that we need to add so that we can reach the correct destination like in order to place a call using Azure communication services we need to use this endpoint.

{endpoint}/calling/callConnections?api-version=2021-08-30-preview

FYI this will not work until you provide the access token"**

  • To which i modified the code as:
const { CommunicationIdentityClient } = require("@azure/communication-identity");
const axios = require('axios');

const main = async () => {
  console.log("Azure Communication Services - Relay Token Quickstart");

  const connectionString = "CONNECTION STRING";

  // Extract endpoint and access key from the connection string
  const endpoint = connectionString.match(/endpoint=(.*?);/)[1];
  const accessKey = connectionString.match(/accesskey=(.*)/)[1];

  // Instantiate the identity client
  const identityClient = new CommunicationIdentityClient(connectionString);

  try {
    let identityResponse = await identityClient.createUser();
    console.log(`nCreated an identity with ID: ${identityResponse.communicationUserId}`);

    // Issue an access token for the created user
    const tokenResponse = await identityClient.getToken(identityResponse, ["voip"]);
    const accessToken = tokenResponse.token;
    console.log(`nIssued an access token: ${accessToken}`);

    // Construct the relay configuration request
    const relayConfigUrl = `${endpoint}/networkTraversal/:issueRelayConfiguration?api-version=2022-03-01-preview`;
    const response = await axios.post(relayConfigUrl, {
      id: identityResponse.communicationUserId
    }, {
      headers: {
        'Authorization': `Bearer ${accessToken}`,
        'Content-Type': 'application/json'
      }
    });

    console.log("RelayConfig", response.data);
    console.log("Printing entire thing ", response.data.iceServers);
  } catch (error) {
    console.log("Encountered an error");
    console.log(error.response ? error.response.data : error.message);
  }
};

main().catch((error) => {
  console.log("Encountered an error");
  console.log(error);
});

I got SAME error.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks @Sampath. I solved the issue by incorporating twilio. We can also use AWS EC2 instance for it.

    const twilio = require("twilio"); 
    
    accountSid="xxxxxx"
    authToken="xxxxxx"
    
    const client = twilio(accountSid, authToken);
    
    async function createToken() {
      const token = await client.tokens.create();
    
      console.log(token);
    }
    

  2. As per this DOC , the @azure/communication-network-traversal package is no longer maintained and the public preview of Azure Communication Services Network Traversal is ending.

    We need to add extensions to the endpoint in order to reach the desired destination. For example, we need to utilize this endpoint in order to place a call using Azure communication services. Otherwise, we cannot simply access the endpoint as a rest API endpoint. {endpoint}/callConnections/calling?api-version=2021-08-30-preview Just so you know, this won’t function until you give the access token.

    The following are steps to CallMedia play:

    • Create and configure a Postman collection
    • Add the pre-request script from this link
    
    const  dateStr = new  Date().toUTCString();
    
    pm.request.headers.upsert({key:'Date', value: dateStr});
    
    const  hashedBodyStr = CryptoJS.SHA256(pm.request.body.raw).toString(CryptoJS.enc.Base64)
    
      
    
    pm.request.headers.upsert({
    
    key:'x-ms-content-sha256',
    
    value: hashedBodyStr
    
    });
    
    const  endpoint = pm.variables.get('endpoint')
    
    const  hostStr = endpoint.replace('https://','');
    
    const  url = pm.request.url.toString().replace('{{endpoint}}','');
    
      
    
    const  stringToSign = pm.request.method + 'n' + url + 'n' + dateStr + ';' + hostStr + ';' + hashedBodyStr;
    
      
    
    const  key = CryptoJS.enc.Base64.parse(pm.variables.get('key'));
    
    const  signature = CryptoJS.HmacSHA256(stringToSign, key).toString(CryptoJS.enc.Base64);
    
    pm.request.headers.upsert({
    
    key:'Authorization',
    
    value: "HMAC-SHA256 SignedHeaders=date;host;x-ms-content-sha256&Signature=" + signature
    
    });
    
    • Connect the Cognitive Services to the Communication Service
      Enter image description here
      Create two variables and add collection variables for Call Setup:
    • key – This variable is supposed to be one of your keys from the Azure portal’s key page for your Azure Communication Services.
    • endpoint: The endpoint for your Azure Communication Services should be entered into this variable from the key page.
      Ensure you remove the trailing slash. For example, https://contoso.communication.azure.com.

    Enter image description here

    Call Setup with Cognitive Services:

    POST https://contoso.communications.azure.com/calling/callConnections?api-version=2021-08-30-preview
    Authorization: Bearer {access_token}
    
    {
      "callbackUri": "https://app.contoso.com/callback",
      "targets": [
        {
          "kind": "phoneNumber",
          "phoneNumber": {
            "value": "+919"
          }
        }
      ],
      "sourceCallerIdNumber": {
        "value": "+183"
      },
      "sourceDisplayName": "Contoso Support",
      "callIntelligenceOptions": {
        "cognitiveServicesEndpoint": "https://<your-cognitive-service-endpoint>.cognitiveservices.azure.com/"
      }
    }
    

    Enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search