Accept an Incoming Call

👍

You're ready for this tutorial if you've got the following:

A FreeClimb account
A registered application
A configured FreeClimb Number
Your tools and language installed


Node.js

An incoming call is initiated by FreeClimb POSTing to the resource specified in the Voice URL in App Config. In response to a POST request from FreeClimb, the application may supply a number of PerCL actions in the JSON encoded response. In this sample the Say, Pause and Hangup PerCL actions are utilized.

Create your package.json file and save in the root directory of your project:

{
  "name": "node-accept-incoming-call-tutorial",
  "version": "1.0.0",
  "dependencies": {
    "@freeclimb/sdk": "^1.0.0",
    "body-parser": "^1.19.0",
    "dotenv": "^8.1.0",
    "express": "^4.17.1"
  }
}

Install the package by running the following in the command line/terminal:

yarn install

Example code:

// Imports and setup
require('dotenv').config()
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const freeclimbSDK = require('@freeclimb/sdk')
 
app.use(bodyParser.json())
var port = process.env.PORT || 3000
const freeclimb = freeclimbSDK()
// Handles incoming calls
app.post('/incomingCall', (req, res) => {
 
  // Get callStatusCallBack
  const callStatusCallback = req.body
 
  // Create PerCL say script
  const say = freeclimb.percl.say('Hello. Thank you for invoking the accept incoming call tutorial.')
 
  // Create PerCL pause script with a duration of 100 milliseconds
  const pause = freeclimb.percl.pause(100)
 
  // Create PerCL say script
  const sayGoodbye = freeclimb.percl.say('Goodbye')
 
  // Create PerCL hangup script
  const hangup = freeclimb.percl.hangup()
 
  // Build scripts
  const percl = freeclimb.percl.build(say, pause, sayGoodbye, hangup)
 
  // Convert PerCL container to JSON and append to response
  res.status(200).json(percl)
})
// Specify this route with 'Status Callback URL' in App Config
app.post('/status', (req, res) => {
  // handle status changes
  res.status(200)
})

Start the server:

app.listen(port, () => {
  console.log(`Starting server on port ${port}`)
})

Java

An incoming call is initiated by FreeClimb POSTing to the InboundCall controller. In response to a POST request from FreeClimb, the application may supply a number of PerCL actions in the JSON encoded response. In this sample the Say, Pause and Hangup PerCL actions are utilized.

Create your build.gradle file and save it to the root directory in your project:

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This is a general purpose Gradle build.
 * Learn how to create Gradle builds at https://guides.gradle.org/creating-new-gradle-builds
 */

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
  //Add the dependency
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:2.1.6.RELEASE"
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'gs-spring-boot'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
    maven { url 'https://jitpack.io' }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile "org.springframework.boot:spring-boot-starter-web"
    testCompile "junit:junit"
    compile 'com.github.FreeClimbAPI:FreeClimb-Java-SDK:3.0.0'
}

sourceSets {
    main {
        java {
            srcDirs = ['src'] // changed line
        }
    }
}

Build the file by running the following in your terminal/command line:

gradle build

Example code:

/* 
 * AFTER RUNNING PROJECT WITH COMMAND: 
 * `gradle build && java -Dserver.port=0080 -jar build/libs/gs-spring-boot-0.1.0.jar`
 * CALL NUMBER ASSOCIATED WITH THE ACCOUNT (CONFIGURED IN FreeClimb DASHBOARD)
 * EXPECT MESSAGE TO BE REPEATED TO YOU: 
 * 'Hello. Thank you for invoking the accept incoming call tutorial. Goodbye.'
*/

package main.java.accept_call;

import org.springframework.web.bind.annotation.RestController;
import com.vailsys.freeclimb.api.FreeClimbException;
import com.vailsys.freeclimb.api.call.CallStatus;
import com.vailsys.freeclimb.percl.Hangup;
import com.vailsys.freeclimb.percl.Language;
import com.vailsys.freeclimb.percl.Pause;
import com.vailsys.freeclimb.percl.PerCLScript;
import com.vailsys.freeclimb.percl.Say;
import com.vailsys.freeclimb.webhooks.StatusCallback;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RestController
public class AcceptCall {

  // To properly communicate with FreeClimb's API, set your FreeClimb app's
  // VoiceURL endpoint to '{yourApplicationURL}/InboundCall' for this example
  // Your FreeClimb app can be configured in the FreeClimb Dashboard
  @RequestMapping(value = {
      "/InboundCall" }, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResponseEntity<?> inboundCall(@RequestBody String str) {
    PerCLScript script = new PerCLScript();

    StatusCallback statusCallback;
    try {
      // Convert JSON into a call status callback object
      statusCallback = StatusCallback.fromJson(str);
    } catch (FreeClimbException pe) {
      PerCLScript errorScript = new PerCLScript();
      Say sayError = new Say("There was a problem processing the incoming call.");
      sayError.setLanguage(Language.ENGLISH_US);
      errorScript.add(sayError);
      return new ResponseEntity<>(errorScript.toJson(), HttpStatus.OK);
    }

    if (statusCallback.getCallStatus() == CallStatus.RINGING) {
      // Create PerCL say script with US English as the language
      Say say = new Say("Hello. Thank you for invoking the accept incoming call tutorial.");
      say.setLanguage(Language.ENGLISH_US);

      // Add PerCL say script to PerCL container
      script.add(say);

      // Create PerCL pause script with a duration of 100 milliseconds
      Pause pause = new Pause(100);

      // Add PerCL pause script to PerCL container
      script.add(pause);

      // Create PerCL say script with US English as the language
      Say sayGoodbye = new Say("Goodbye.");
      sayGoodbye.setLanguage(Language.ENGLISH_US);

      // Add PerCL say script to PerCL container
      script.add(sayGoodbye);

      // Create PerCL hangup script
      Hangup hangup = new Hangup();

      // Add PerCL hangup script to PerCL container
      script.add(hangup);
    }

    // Convert PerCL container to JSON and append to response
    return new ResponseEntity<>(script.toJson(), HttpStatus.OK);
  }
}

C#

An incoming call is initiated by FreeClimb POSTing to the InboundCall controller. In response to a POST request from FreeClimb, the application may supply a number of PerCL actions in the JSON encoded response. In this sample the Say, Pause and Hangup PerCL actions are utilized.

Example code:

[HttpPost("InboundCall")]
    public ActionResult InboundCall (CallStatusCallback freeClimbRequest) {
      // Create an empty PerCL script container
      PerCLScript script = new PerCLScript ();
      // Verify inbound call is in proper state
      if (freeClimbRequest.getCallStatus == ECallStatus.Ringing) {
        // Create PerCL say script with US English as the language
        Say say = new Say ();
        say.setLanguage (ELanguage.EnglishUS);
        // Set prompt to record message
        say.setText ("Hello. Thank you for invoking the accept incoming call tutorial.");
 
        // Add PerCL say script to PerCL container
        script.Add (say);
 
        // Create PerCL pause script with a duration of 100 milliseconds
        Pause pause = new Pause (100);
 
        // Add PerCL pause script to PerCL container
        script.Add (pause);
 
        // Create PerCL say script with US English as the language
        Say sayGoodbye = new Say ();
        sayGoodbye.setLanguage (ELanguage.EnglishUS);
        // Set prompt
        sayGoodbye.setText ("Goodbye.");
 
        // Add PerCL say script to PerCL container
        script.Add (sayGoodbye);
 
        // Create PerCL hangup script
        Hangup hangup = new Hangup ();
 
        // Add PerCL hangup script to PerCL container
        script.Add (new Hangup ());
      }
      // Convert PerCL container to JSON and append to response
      return Content (script.toJson (), "application/json");
    }