Pop up Window ketika telepon masuk di Ozekiphone

How to display an HTML popup window when my phone rings with JavaScript API

Below you can find an excellent way on how to follow your calls easily using JavaScript API. By displaying an HTTP popup window, it is quite simple to receive notifications about the calls made through your own website.

Figure 1 - System architecture

For this purpose, you only have to execute the following steps:
After these steps, you will receive a notification in the form of Popup on your own site every time when a call is created in the system. Of course, there are more possibilities in JavaScript API, but this article gives information about about the call created and call state changed events. 

Let’s see each step in details!

Step 1: Check the full code of the example

  1. <!DOCTYPE html>
  2. <!--Important:This file is have to be hosted on a web server(e.g. Apache,IIS etc.)-->
  3. <html>
  4. <head>
  5. <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/ui-darkness/jquery-ui.min.css" />
  6. <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js" />
  7. <script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" />
  8. <script type="text/javascript" src="http://ozekixepbx.ip:7777/WebphoneAPI" />
  9. <script type="text/javascript">
  10. //////// Use your settings //////////
  11. var serverAddress = "ozekixepbx.ip";
  12. var securityToken = "vQTJzve0J0mYsnvWjnnoUA";
  13. var numberToSubscribe = "1001";
  14. ////////////////////////////////////
  15. $( document ).ready(function() {
  16. OzWebClient.onConnectionStateChanged(connectionStateChanged);
  17. OzWebClient.connect(serverAddress, securityToken);
  18. });
  19. function connectionStateChanged(info) {
  20. console.log(info.State);
  21. if (info.State == ConnectionState.ACCESS_GRANTED){
  22. OzWebClient.onSessionCreated (sessionCreated);
  23. OzWebClient.onSessionClosed (sessionClosed);
  24. }
  25. else if (info.State == ConnectionState.ACCESS_DENIED)
  26. console.log("Please check the validity of your Security Token!");
  27. else
  28. console.log("Please check the availability of the PBX!");
  29. }
  30. function sessionCreated(session) {
  31. session.onSessionStateChanged(sessionStateChanged);
  32. }
  33. function sessionClosed(session) {
  34. closePopUp();
  35. }
  36. function sessionStateChanged(session, sessionState ) {
  37. if (session.caller == numberToSubscribe || session.callee == numberToSubscribe){
  38. showPopUp(session.source, session.destination, session.direction, session.sessionState);
  39. }
  40. }
  41. //Close the Pop-up if is exist
  42. function closePopUp(){
  43. if ($("#NoficationPopup"))
  44. $("#NoficationPopup").dialog("destroy").remove();
  45. }
  46. //Show or modify the Pop-up
  47. function showPopUp(caller, callee, direction, state){
  48. if ($("#NoficationPopup").length > 0){
  49. $("#lbCaller").html('Caller: ' + caller);
  50. $("#lbCallee").html('Callee: ' + callee);
  51. $("#lbDirection").html('Direction: ' + direction);
  52. $("#lbState").html('State: ' + state);
  53. }
  54. else {
  55. $('<div id="NoficationPopup" title=\"' + direction + '\">' +
  56. '<div align=left>' +
  57. '<label id="lbCaller">Caller: '+caller+'</label> <BR />' +
  58. '<label id="lbCallee">Callee: '+callee+'</label> <BR />' +
  59. '<label id="lbDirection">Direction: '+direction+'</label><BR/>' +
  60. '<label id="lbState">State: '+state+'</label> <BR />' +
  61. '</div> ' +
  62. '</div>')
  63. .dialog({
  64. resizable: false, position: [0,0], minWidth: 250, minHeight: 100,
  65. close: function () {
  66. $(this).dialog("destroy").remove();
  67. }});
  68. }
  69. }
  70. </script>
  71. </head>
  72. <body>
  73. </body>
  74. </html>
Code example 1 - Full code of the downloadable example

Step 2: Generate Security Token for the authentication

In order to be able to see the created calls in the system, you have to get access from the PBX for your application. You can do this by generate a Security Token for your program and with that token you can connect to PBX.
You have the possibility to use a generated Security Token for the signin. In case of the Security Token it can be given what it can be used for and until what time, 1 day is default. Such possibilities for usage are:
  • Sending a message
  • Initiating calls inside or outside the system (mobile and line calls)
  • Query of the Call Information
  • and a lot of others
We can get a Security Token through HTTP API (if it is enabled), or we can generate it in the system after logging in with the GenerateSecurityToken Command of the HTTP API Tester.
To get a Security Token through HTTP API, you should send a simple HTTP request to the PBX and the needed token is going to be in the given response.
In the code example below you can see what request parameters should be sent to the PBX.
  1. Command=GenerateSecurityToken
  2. AuthXml=<?xml version="1.0"?>
  3. <SecurityToken>
  4. <AllowSessionModification>False</AllowSessionModification>
  5. </SecurityToken>
Code example 2 - The original HTTP Request
The specified url parameter list and the whole request is in the code below. You can send the request like you copy the url encoded text below to the headline of your browser. Of course you have to change the IP address to the address of the PBX here, too:
Code example 3 - Url encoded request
On the summary page below you can get further information about the HTTP API and the commands of it.
The other method for getting the needed Security Token is that you login to the PBX and generate the token on your own with the aid of HTTP API Tester.
For this, choose the Productivity/HTTP API menu fom the menu line above in the PBX. Then choose theGenerateSecurityToken Command, click on the Test tab, then to the Send request button!

Figure 2 - Generate Security Token with HTTP API Tester

The generated Security Token can be found in the received response between the <SecurityToken> tags.

Step 3: Connect to Ozeki Phone System XE through JavaScript API

As for the start create a simple HTML page where you refer a few scripts and JavaScript API, too. The created file has to be hosted and run on a Web-server, because it will result in an error if it is simply run on the file system (for example from Windows Explorer).
  1. <!DOCTYPE html>
  2. <!-- Important: This file has to be hosted on a web server (e.g. Apache, IIS etc.) -->
  3. <html>
  4. <head>
  5. <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/ui-darkness/jquery-ui.min.css" />
  6. <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
  7. <script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
  8. <script type="text/javascript" src="http://ozekixepbx.ip:7777/WebphoneAPI"></script>
  9. <script type="text/javascript">
  10. <!-- Your scripts will be here -->
  11. </script>
  12. </head>
  13. <body>
  14. </body>
  15. </html>
Code example 4 - The basic HTML page that is going to be extended step by step.

Figure 3 - Address of your PBX

The row below is going to add reference to the JavaScript API via the running Ozeki Phone System XE. Please use your own IP address of your installed PBX instead of the ozekixepbx.ip.
  1. <script type="text/javascript" src="http://ozekixepbx.ip:7777/WebphoneAPI"/>;
Code example 5 - Adding reference to the JavaScript API
When you are ready with this, you can start to write the more interesting code parts. First we will see some variables and the connection.
  1. <script type="text/javascript">
  2. <!-- Your scripts will be here -->
  3. //////// Use your settings //////////
  4. var serverAddress = "ozekixepbx.ip";
  5. var numberToSubscribe = "1001";
  6. var securityToken = "nixtwtqHr0qTRk9FcrV8Sg" ;
  7. ////////////////////////////////////
  8. $( document ).ready(function() {
  9. OzWebClient.onConnectionStateChanged(connectionStateChanged);
  10. OzWebClient.connect(serverAddress, securityToken);
  11. });
  12. </script>
Code example 6 - Variables to be changed, and connection to the PBX
The numberToSubscribe should be a phone number. We take it as a basic that we are curious for the events of only this phone number. But in case of a need it is extendable, of course, and this requirement can even be left.
After clarifying the role of the variables we can see the real connection. Before we connect, we should subscribe for the OzWebClient.onConnectionStateChanged event. We can achieve this by subscribing theconnectionStateChanged method that is going to be mentioned later. After this we should call theOzWebClient.connect method. There is the address of the PBX and the requested Security Token.

Step 4: Subscribe for the sessionCreated and sessionStateChanged event

We have already initiated the connection request towards the server earlier, and we get the response from it in the subscribing connectionStateChange method:
  1. function connectionStateChanged(info) {
  2. if (info.State == ConnectionState.ACCESS_GRANTED)
  3. OzWebClient.onSessionCreated (sessionCreated);
  4. else if (info.State == ConnectionState.ACCESS_DENIED)
  5. console.log("Please check the validity of your Security Token!");
  6. else
  7. console.log("Please check the availability of the PBX!");
  8. }
Code example 7 - Subscribing function that gives back the response of the PBX to the login request.
We can see that the info.State parameter of the function carries the response of the server. If the login was successful, we subscribe for the OzWebClient.onSessionCreated event. So when a new call is created in the PBX, the subscribing method will be called:
  1. function sessionCreated(session) {
  2. session.onSessionStateChanged(sessionStateChanged);
  3. }
Code example 8 - Function that is called when a call is created.
We subscribe for the changes of the call state in the function, so every time when it gets into another state (Setup, Ringing, Incall, Completed etc. ) we receive a notification through the sessionStateChangedmethod.

Step 5: When the sessionStateChanged event is triggered, show a Popup

As soon as the onSessionStateChanged event is triggered, the function below is called:
  1. function sessionStateChanged(session, sessionState ) {
  2. if (session.caller == numberToSubscribe || session.callee == numberToSubscribe){
  3. showPopUp(session.source, session.destination, session.direction, session.sessionState);
  4. }
  5. }
Code example 9 - Function called when the call state changes
We get the actual session in the function, that carries the information about the actual call in its parameters. In the function above we check whether the phone number of the caller or the callee is the same as the phone number given by us (numberToSubscribe), and if it is, then we call the showPopUp method with the parameters of the call:
  1. function showPopUp(source, destination, direction, state){
  2. if ($("#NoficationPopup").length > 0){
  3. $("#lbCaller").html('Caller: ' + caller);
  4. $("#lbCallee").html('Callee: ' + callee);
  5. $("#lbDirection").html('Direction: ' + direction);
  6. $("#lbState").html('State: ' + state);
  7. }
  8. else
  9. $('<div id="NoficationPopup" title=\"' + direction + '\">' +
  10. '<div align=left>' +
  11. '<label id="lbCaller">Caller: '+source+'</label> <BR />' +
  12. '<label id="lbCallee">Callee: '+destination+'</label> <BR />' +
  13. '<label id="lbDirection">Direction: '+direction+'</label> <BR />' +
  14. '<label id="lbState">State: '+state+'</label> <BR />' +
  15. '</div> ' +
  16. '</div>')
  17. .dialog({
  18. resizable: false, position: [0,0], minWidth: 250, minHeight: 100,
  19. close: function () {
  20. $(this).dialog("destroy").remove();
  21. }});
  22. }
Code example 10 - A function that executes the showing of a Popup.
We can see that when the Popup is not in the foreground, we create it, having uploaded with the appropriate values. If we have already showed this dialogue earlier, we modify its values.