Toky Phone JS SDK

The Toky Phone JS SDK Developer Hub

Welcome to the Toky Phone JS SDK developer hub. You'll find comprehensive guides and documentation to help you start working with Toky Phone JS SDK as quickly as possible, as well as support if you get stuck. Let's jump right in!

Get Started    

Toky Phone JS SDK

This page will help you get started with Toky Phone JS SDK

Toky Phone JS SDK

License: MITLicense: MIT

The Toky Phone JS SDK is a WebRTC Javascript library providing an abstraction to the Toky phone system, including its main features.

Prerequisites

  • You will need an active Toky Account
  • The App should be Browser-based
  • An API key from the Toky Web App

What can you do?

Make calls, receive web calls, all of the operations related to a call, mute, hold, recording, transfer calls to other Agents, Groups or Numbers, Blind transfers, and Warm.

Alpha version

During the alpha period, we may change aspects of the API based on authentication and feedback.

Installation

Clone the repository or download the zip or install from npm.

$ npm install --save toky-phone-js-sdk

Authentication

Development & Production environment

The two environments require .env files, and it works like this.

In compilation time, it automatically uses the .env file depending on your git branch. It looks like this:

Git Branch   Environment file
==========   ================
main         .env.prod
staging      .env.staging
dev          .env.dev

.env.example

TOKY_API_URL="https://api.toky.co"
TOKY_RESOURCES_URL="https://app.toky.co"
PUSHER_KEY="" # Warm transfer feature
AGENT_ID="" # Your Toky Agent Id

Development

You can use the .env.example as the base for your .env.dev file.

In the main.js in the example folder, replace the variable currentAppId with your corresponding generated App Id, this in mandatory:

const currentAppId = 'yourappid'
const currentAppKey = 'yourappkey'

Then you can run:

npm run dev

This script runs the app in development mode and the Dialer in the /example folder.

It would open a tab in your browser, starting the authentication flow.

Production

For the production environment, we need an .env.prod file also based on the .env.example

npm run build

Builds the SDK for production to the /dist folder, ready to use as a Module or in a script tag.

Connecting and registering

The .init() method is making an automatic registration with the phone system.

import TokySDK from 'toky-phone-js-sdk'

const { TokyClient } = TokySDK

const Client = new TokyClient({
  accessToken: '{{access_token}}',
  account: {
    user: '[email protected]',
    type: 'agent',
    acceptInboundCalls: false,
  },
  transportLib: 'sip.js',
})

await Client.init()

Refresh Token

The refresh token method is useful when an access token has expired

Client.refreshToken('yournewaccesstoken')

TokyClient instance events

Registration events

const { ClientStatus } = TokySDK

Client.on(ClientStatus.REGISTERED, () => { /* Your code here */ })
Client.on(ClientStatus.UNREGISTERED, () => { /* Your code here */ })

The Connecting event is emitted whenever a call is starting and is the first event before a Ringing event in the Session instance

const { ClientStatus } = TokySDK

Client.on(ClientStatus.CONNECTING, () => { /* Your code here */ })

The Invite event is emitted when you are receiving a call (only if the acceptInboundCalls param is true)

Client.on(ClientStatus.INVITE, () => { /* Your code here */ })

Media status events

const { MediaStatus } = TokySDK

Client.on(MediaStatus.READY, () => { /* Your code here */ })
Client.on(MediaStatus.UPDATED, () => { /* Your code here */ })
Client.on(MediaStatus.INPUT_UPDATED, () => { /* Your code here */ })
Client.on(MediaStatus.OUTPUT_UPDATED, () => { /* Your code here */ })
Client.on(MediaStatus.PERMISSION_GRANTED, () => { /* Your code here */ })
Client.on(MediaStatus.PERMISSION_REVOKED, () => { /* Your code here */ })

Outgoing call

You can list the available Phone Numbers of the company, pick one, and establish a call.

https://toky-js-sdk.toky.co/reference#agentsdids

let tokySession = Client.startCall({
  phoneNumber: '+595217288659' /* example number */,
  callerId: '+13344413569'/* example caller id from the company */,
})

Once the call is established, we get a session (tokySession) of that call and
later we will use it to make the following operations

Session instance methods

Mute call

tokySession.mute()

Hold Call

tokySession
  .hold()
  .then(() => {
    console.warn('--- HOLD action success')
  })
  .catch(() => {
    console.warn('--- HOLD action unsuccess')
  })

Record Call

This option will work if the agent has the corresponding permissions.

tokySession
  .record()
  .then(() => {
    console.warn('--- RECORD action success')
  })
  .catch(() => {
    console.warn('--- RECORD action unsuccess')
  })

Transfer call with Blind and Warm options

const { TransferEnum, TransferOptionsEnum } = TokySDK

tokySession.makeTransfer({
  type: TransferEnum.AGENT,
  destination: '[email protected]',
  option: TransferOptionsEnum.BLIND,
})

tokySession.makeTransfer({
  type: TransferEnum.NUMBER,
  destination: '+595217288659',
  option: TransferOptionsEnum.BLIND,
})

Cancel transfer

The cancel transfer option will work only for Warm Transfers.

tokySession
  .cancelTransfer()
  .then(() => {
    console.warn('--- Cancel Transfer action success')
  })
  .catch(() => {
    console.warn('--- Cancel Transfer action unsuccess')
  })

End Call

tokySession.endCall()

Session instance events

tokySession.on(SessionStatus.MUTED, () => { /* Your code here */ })
tokySession.on(SessionStatus.UNMUTED, () => { /* Your code here */ })
tokySession.on(SessionStatus.HOLD, () => { /* Your code here */ })
tokySession.on(SessionStatus.UNHOLD, () => { /* Your code here */ })
tokySession.on(SessionStatus.HOLD_NOT_AVAILABLE, () => { /* Your code here */ })
tokySession.on(SessionStatus.RECORDING, () => { /* Your code here */ })
tokySession.on(SessionStatus.NOT_RECORDING, () => { /* Your code here */ })
tokySession.on(SessionStatus.RECORDING_NOT_AVAILABLE, () => { /* Your code here */ })

Transfer call events

We have several call events for transfer calls.

TRANSFER_FAILED is related to the phone system rejecting the transfer operation, i.e. use an invalid agent sip username to make a blind transfer.

tokySession.on(SessionStatus.TRANSFER_BLIND_INIT, () => { /* Your code here */ })
tokySession.on(SessionStatus.TRANSFER_WARM_INIT, () => { /* Your code here */ })
tokySession.on(SessionStatus.TRANSFER_WARM_ANSWERED, () => { /* Your code here */ )
tokySession.on(SessionStatus.TRANSFER_WARM_NOT_ANSWERED, () => { /* Your code here */ })
tokySession.on(SessionStatus.TRANSFER_WARM_COMPLETED, () => { /* Your code here */ })
tokySession.on(SessionStatus.TRANSFER_WARM_NOT_COMPLETED, () => { /* Your code here */ })
tokySession.on(SessionStatus.TRANSFER_WARM_CANCELED, () => { /* Your code here */ })
tokySession.on(SessionStatus.TRANSFER_FAILED, () => { /* Your code here */ })

Audio device selection

TokyMedia is the singleton in charge of everything related to Devices/Media.

The MediaStatus.READY is emitted when the user's permissions had been allowed.

const { TokyMedia, MediaStatus } = TokySDK

TokyMedia.on(TokyMedia.READY, () => {
  /* The device id */
  const outputDevice = '123asd123asd123'
  TokyMedia.setOutputDevice(outputDevice).then(() => {
    console.log('Output device updated successfully!')
  })

  /**
  * This is applied for established calls
  * it allows you to switch audio devices mid-call
  */
  const inputSelected = 'asd123asd123asd'
  if (tokySession) {
    const connection = tokySession.getConnection()
    TokyMedia.setInputDevice(inputSelected, connection).then(() => {
      console.log('Input device updated successfully!')
    })
  } else {
    TokyMedia.setInputDevice(inputSelected).then(() => {
      console.log('Input device updated successfully!')
    })
  }

  /* The list of available devices, and can be used to switch devices */
  console.log(TokyMedia.inputs, TokyMedia.outputs)

  /* List current selected devices, input and output respectively */
  console.log(`Selected input: ${TokyMedia.selectedInputDevice.name}`)
  console.log(`Selected ouput: ${TokyMedia.selectedOutputDevice.name}`)
})

License

MIT License

Copyright (c) 2020 Toky Phone JS SDK

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Updated 2 months ago

Toky Phone JS SDK


This page will help you get started with Toky Phone JS SDK

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.