// Replace 'YOUR_API_KEY' with your actual API key

const apiKey = 'YOUR_API_KEY';


// Function to authenticate and log in an agent

async function loginAgent(username, password) {

    try {

        const response = await fetch('https://api.ziwo.io/v1/auth/login', {

            method: 'POST',

            headers: {

                'Content-Type': 'application/json',

                'Authorization': `Bearer ${apiKey}`

            },

            body: JSON.stringify({

                username: username,

                password: password

            })

        });


        if (!response.ok) {

            throw new Error('Login failed');

        }


        const data = await response.json();

        const authToken = data.token;

        console.log('Agent logged in successfully:', authToken);

        return authToken;

    } catch (error) {

        console.error('Error logging in agent:', error);

        return null;

    }

}


// Function to make a call using Ziwo API

async function makeCall(authToken, toNumber, fromNumber) {

    try {

        const response = await fetch('https://api.ziwo.io/v1/calls', {

            method: 'POST',

            headers: {

                'Content-Type': 'application/json',

                'Authorization': `Bearer ${authToken}`

            },

            body: JSON.stringify({

                to: toNumber,

                from: fromNumber,

                // Additional parameters can be added here, such as custom caller ID, etc.

            })

        });

        

        if (!response.ok) {

            throw new Error('Failed to make call');

        }

        

        const callData = await response.json();

        console.log('Call successfully initiated:', callData);

    } catch (error) {

        console.error('Error making call:', error);

    }

}


// Example usage: Login agent and make a call

async function loginAndMakeCall(username, password, toNumber, fromNumber) {

    const authToken = await loginAgent(username, password);

    if (authToken) {

        makeCall(authToken, toNumber, fromNumber);

    }

}


// Example usage: Login agent and make a call

const agentUsername = 'AGENT_USERNAME';

const agentPassword = 'AGENT_PASSWORD';

const destinationNumber = '+1234567890';

const callerId = '+9876543210';


loginAndMakeCall(agentUsername, agentPassword, destinationNumber, callerId);

Comments

Popular posts from this blog

How to seperate character from string in php

How to Delete record using PHP Ajax

Uploads Only 10 files in month step by step