Get Started with Connect Call to Second Person Using Go

    Overview

    You may want to request your application to dial out for you, such that you’ll receive a call on your phone, which will then connect you to the user you want. With the help of this tutorial named connect a call to the second person, you may accomplish this. Step-by-step instructions are as follows:

    1. Make an outbound call to the caller.
    2. When the first user answers the phone, a new call is placed to a different user (second user).
    3. Both calls (first and second user) are bridged once the second user answers the call.

    Common Connect call to second use cases include:

    • Call Forward: You can forward calls to another user whom you wish for.
    • Click to Call: You can direct calls to the right representative as well as the person who clicked on the link.
    • Lead assignment and lead routing: For inbound sales calls, you can set up an IVR menu with a set of qualifying questions to discover a customer’s interests, then redirect their call to a representative based on their responses.

    This guide shows how to build Connect a user to second person on the Plivo platform, either by using our PHLO visual workflow builder or our APIs and XML documents. Follow the instructions in one of the tabs below.

    To implement connect a call to second user use-case, you can create and deploy a PHLO with a few clicks on the PHLO canvas. PHLO also lets you visually construct your entire use-case. With PHLO, you only pay for calls you make/receive, and building with PHLO is free.

    Implementation

    In this section, we will guide you to create a PHLO to implement connect a call to second use-case.

    Prerequisites

    1. Plivo Auth Id and Auth Token: You will find your Plivo Auth Id and Auth Token on the home screen of your Plivo Console. Click here to sign-up for a Plivo account if you haven’t already! Find Your Auth Credentials on Plivo Console
    2. Set up your Dotnet dev environment: To set up your dev environment in your .NET(C#) programming language, please refer to the instructions available in the Set up Your Dotnet Dev Environment section.
    3. Buy a Plivo number(optional): You must have a voice-enabled Plivo phone number if you are willing to receive incoming calls. You can purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API. Buy a New Plivo Number

    Create the PHLO

    Create a PHLO for Connect a Call to Second User

    1. On the top navigation bar, click PHLO. The PHLO page will appear and display your existing PHLOs, if any exist. If this is your first PHLO, then the PHLO page will be empty.
    2. Click CREATE NEW PHLO to build a new PHLO.
    3. In the Choose your use-case window, click Build my own. The PHLO canvas will appear with the Start node. Note: The Start node is the starting point of any PHLO. You can choose between the four available trigger states of the Start node; Incoming SMS, Incoming Call, and API Request. For this PHLO, we will use the API Request trigger state.
    4. Click the Start node to open the Configurations tab, and then enter the information to retrieve from the HTTP Request payload. Only the keys from, to, and second_user will be filled in here.
    5. Click Validate to save the configurations for the node.
    6. From the list of components on the left-hand side, drag and drop the Initiate Call component onto the canvas. This will add a Initiate call node onto the canvas.
    7. Connect the Start node with the Initiate Call node, using the API Request trigger state.
    8. Configure the Initiate Call node to select the From and To numbers from the Start node. Note:
      • Enter two curly brackets to view all available variables.
      • The values for the From and To numbers are picked from the HTTP Request Payload. Make sure to clearly define the variables in the Start node.
    9. Click Validate to save the configurations for the node.
    10. From the list of components on the left-hand side, drag and drop the Play Audio component onto the canvas. This will add a Play Audio node onto the canvas.
    11. Connect the Initiate Call node with the Play Audio node, using the Answered trigger state.
    12. Configure the Play Audio node with the text of your choice, For example: Please wait while we connect your call to the second number.
    13. Click Validate to to save the configurations of the node.
    14. From the list of components on the left-hand side, drag and drop the Call Forward component onto the canvas. This will add a Call Forward node onto the canvas.
    15. Connect the Call Forward node with the Play Audio node, using the Prompt Completed trigger state.
    16. Configure the Call Forward node to select the From and To numbers from the Start node. Note:
      • Enter two curly brackets to view all available variables.
      • The values for the From and To numbers are picked from the HTTP Request Payload. Make sure to clearly define the variables in the Start
    17. Click Validate to to save the configurations of the node.
    18. After you complete the configurations, click Save. You can trigger your PHLO using the PHLO URL to test it out. Your complete PHLO will look like this:

    Connect Second User

    • After you complete the configurations, provide a friendly name for your PHLO and click Save. Your PHLO is now ready. You can trigger the PHLO and test it out. For more information, refer to the below section.

    Set up Your Go Dev Environment

    You must set up and install Go and Plivo’s Go SDK. Here’s how.

    Install Go

    You can install Go from the Official Installer.

    Install Plivo Go Package

    • Create a project directory, run the following command:

      $ mkdir mygoapp
      
    • Change the directory to our project directory in the command line:

      $ cd mygoapp
      
    • You can install the Plivo Go package using the go command.

      $ go get github.com/plivo/plivo-go
      
    • You can also install by cloning this repository into your GOPATH.

    Trigger the PHLO

    Once you have created and configured your PHLO, copy the PHLO_ID from here. You can integrate a PHLO into your application workflow by making an API request to trigger the PHLO with the required payload.

    With Dynamic Payload

    To use dynamic values for the parameters, you can use the liquid templating params while creating the PHLO and pass the values while triggering the PHLO, follow the below steps.

    • Select the Start node - under API request, fill the Payload key as from, to & second_user and keep the values empty, then click validate & save.

    With Dynamic Payload

    Code

    Now, create a file called TriggerPhlo.go and paste the following code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    
    package main
    
    import (
    	"fmt"
    	"plivo-go"
    )
    // Initialize the following params with corresponding values to trigger resources
    const authId = "<auth_id>"
    const authToken = "<auth_token>"
    const phloId = "phlo_id"
    
    func main() {
    	testPhloRunWithParams()
    }
    
    func testPhloRunWithParams() {
    	phloClient, err := plivo.NewPhloClient(authId, authToken, &plivo.ClientOptions{})
    	if err != nil {
    		panic(err)
    	}
    	phloGet, err := phloClient.Phlos.Get(phloId)
    	if err != nil {
    		panic(err)
    	}
    	//pass corresponding from and to values
    	type params map[string]interface{}
    	response, err := phloGet.Run(params{
    		"from": "+14157778888",
    		"to":   "+14157778889",
    		"second_user":   "+14157778887",
    	})
    
    	if err != nil {
    		println(err)
    	}
    	fmt.Printf("Response: %#v\n", response)
    }
    

    You can get your Auth_ID and Auth_token from your dashboard AUTHID

    You can find the PHLO_ID on the PHLO Listing page. PHLO Listing

    Test and Validate

    You can save the file and use the below command to run it.

    go run TriggerPhlo.go
    
    Note: If you are using a Plivo Trial account for this example, you can only make calls to phone numbers that have been verified with Plivo. Phone numbers can be verified at the Sandbox Numbers page.

    To implement connect a call to second person use-case in the traditional XML way, you can refer to the instructions in the below section to begin your implementation.

    Implementation

    In this section, we will guide you in setting up an app to make a call to a number and when answered, connect the call to second person it using Plivo XML. In this example, when an outgoing call is answered, the call will be made to the second person using the <Dial> XML.

    Prequisites

    1. Plivo Auth Id and Auth Token: You will find your Plivo Auth Id and Auth Token on the home screen of your Plivo Console. Click here to sign-up for a Plivo account if you haven’t already!

      Find Your Auth Credentials on Plivo Console

    2. Plivo Phone Number(Optional): Purchase numbers from the Numbers section of your Plivo Console and use the same as the caller ID for the outbound call. This number will also help you receive incoming calls as you must have a voice-enabled Plivo phone number to do the same. Please note that you can also purchase numbers using the Numbers API.

      Buy a New Plivo Number

    3. Answer URL: When an outgoing call is made number, you can control the call flow by declaring an answer URL. Plivo will invoke the answer URL specified and expect a valid XML response to handle the call. In the outbound API call example in the Make-Outbound-Calls use-case guide, we specified the answer URL along with the make call API request, whereas in the case of incoming calls to Plivo numbers, the answer URL is specified in the Plivo application associated with the phone number.

    4. Set Up Your Web Server: To be able to host Answer URLs and to be able to provide valid XMLs and accept notifications on these URLs respectively, you need to host a webserver at your end. To set up your Web Server in your preferred programming language, please refer to the instructions available in the Set Up Go Server section.

    Set Up Go Server

    In this section, we’ll walk you through how to set up a Go server in under five minutes and start handling calls & callbacks.

    Install Plivo Go and Gin-Gonic packages

    • Create a project directory, run the following command:

      $ mkdir mygoapp
      
    • Change the directory to our project directory in the command line:

      $ cd mygoapp
      
    • You can install the Plivo and Gin-Gonic Go packages using the go command.

      $ go get github.com/plivo/plivo-go
      $ go get github.com/gin-gonic/gin
      
    • You can also install by cloning the Plivo Go repository and Gin-Gonic repository into your GOPATH.

    Create a Go Server to Connect Calls to second person

    Use the following code snippet to start a local server.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    
    package main
    
    import (
        "fmt"
    
        "github.com/gin-gonic/gin"
        "github.com/plivo/plivo-go"
        "github.com/plivo/plivo-go/xml"
    )
    
    func main() {
        r: = gin.Default()
        r.GET("/outbound-call", func(c * gin.Context) {
            c.Header("Content-Type", "application/JSON")
            fmt.Println("https://" + c.Request.Host + "/connect")
            client, err: = plivo.NewClient("<auth_id>", "<auth_token>", & plivo.ClientOptions {})
            if err != nil {
                panic(err)
            }
            response, err: = client.Calls.Create(
                plivo.CallCreateParams {
                    From: "+14151234567",
                    To: "+14151234567",
                    AnswerURL: "https://" + c.Request.Host + "/connect",
                },
            )
            if err != nil {
                panic(err)
            }
            fmt.Printf("Response: %#v\n", response)
            c.JSON(200, response)
        })
    
        r.POST("/connect", func(c * gin.Context) {
            c.Header("Content-Type", "text/xml")
            response: = xml.ResponseElement {
                Contents: [] interface {} {
                    new(xml.SpeakElement).
                    AddSpeak("Please wait while we connect your call to the second number", "WOMAN", "en-US", 1),
                        new(xml.DialElement).
                    SetContents(
                        [] interface {} {
                            new(xml.NumberElement).
                            SetContents("+14151234567"), // Dial to second number
                        },
                    ),
                },
            }
            c.String(200, response.String())
        })
        r.Run()
    }
    

    Save this code in any file (name the file something like connect_call.go). To run this file on the server, go to the folder where this file resides and use the following command:

    $ go run connect_call.go
    

    And you should see your basic server app in action on http://localhost:8080/outbound-call/

    Exposing your local server to the internet

    Your local server should be able to connect with Plivo API service, Ngrok is a tunneling software used to expose a web server running on your local machine to the internet. Using Ngrok you can set webhooks which can talk to Plivo server.

    ngrok block diagram

    You can download and install ngrok from here. Follow the detailed configuration instructions to get started.

    Run ngrok on the port which currently hosts your application. For example, if your port number is 80, run the following command:

    ./ngrok http <port_on_which_your_local_server_is_running>
    

    This will give you a UI with links that look like ngrok.io/* which you can use to access your local server using the public network.

    Sample ngrok CLI

    In this case, the ngrok url will be something like https://37347786acf6.ngrok.io/outbound-call/ and you can check the XML document using any browser.

    Test and validate

    Make a call to a regular mobile phone. Plivo will send a request to your Answer URL requesting for a valid XML response and connect the call to second user. Meanwhile, the parameters listed in the XML Request - call status documentation will also be sent to your server.