Welcome Message

Hello my dear reader,

Welcome to my blog, which is dedicated to Cisco technologies. On its pages we will talk about the limitless world of telephony and networking.

We will focus mostly on Cisco collaboration solutions and technologies. These are IP PBX based on Cisco Unified Communications Manager and Cisco Unified Communications Manager Express, Cisco contact centers, Cisco Voice Gateways, etc. Also, I will introduce you the education news: Cisco authorized courses, my own developed training programs, our upcoming events, online learning.

If you have any questions regarding my posts, job or activities, please feel free to ask your questions. I will try to answer them when I have time.

If you are satisfied with the content of my blog, isn’t that worth a beer or coffee? Donations help me to continue supporting the blog and creating new posts here — things for which I spend hours of my free time! Thank you very much!

Sincerely, Dmytro Benda

Thursday, October 20, 2022

Saving Information to a Text File in UCCX Repository

Being a Cisco Instructor, I often receive practical questions from my students and customers about some configurations of Cisco UC products. Many scenarios are not clearly described in Cisco documentation and therefore can be found only during real work with Cisco equipment and products. One of these question was related to UCCX. Our customer wanted to save some information like date, time, calling number, etc. in a text file. Actually, the best choice to keep such data is a Database of course, but for some reasons they preferred to save all this in txt format. It is not a problem to do it, but their case was a bit complicated, because they want to update the content of the file after each call and add new rows there with new data. I created a demo UCCX script with such functionality for them and tested it in UCCX 12.5. And in this post we are going to discuss it. 

First, create a blank text file with Notepad (or any other text editor you prefer) and upload this file to UCCX Document Repository. In UCCX you can do it under CCX administration -> Application -> Document Management. Our blank text file is named textfile.txt here (its size is not 0 in this picture, because the screenshot was made in the end of testing when the file already was written with some lines):


Our script will read the content of this file and add new records to it. Every call will create one additional row with the information. Let me show you the script itself. As usual, let's define the required variables first: 


callingNumber - a String variable carrying Calling Number information
dateString - this one will be used to store the information about the date of call
timeString - as you can imagine we will store the time of call here
pinCode - the customer wanted to save PIN codes info in his application. I created a static String variable for demonstration. 
finalString - a variable with the row of text which will be written to the text file
user - it is used to store the user account which will be used to save the file the Repository (for authentication reasons)
doc - a Document type variable for text file content. In the beginning its value is equal to the content of the file textfile.txt in our Document Repository. 

Our demo script looks like that: 


After playing our Welcome message (first Play Prompt in the script), we added a Cache Document step to read the value of the existing doc variable (the current content of our text file) and keep it in the UCCX memory (we will add the new info to the existing one later on). Here are the properties of this step: 


The buffer size depends here on the size of your existing text file. For this demo 50 KB for the buffer was quite ok. 

Get Call Contact Info step extracts the Calling Number from call data and assigns it to our callingNumber variable. Next the three Set steps assign values to PIN Code, date and time variables. All this data the customer wanted to save to his text file. 

The fourth Set step in the script forms the finalString variable. It contains the a text phrase we are going to add to our text file. Its value looks like that:

finalString = callingNumber + "," + pinCode + "," + dateString + "," + timeString + System.getProperty("line separator")

The last part of this formula System.getProperty("line separator") is a Java expression which result in \n Java option meaning the end of line and inserting a newline in the text at this point.

Then we can add the finalString to existing data in our text file (remember that they are in doc variable now after the Cache Document step). The last Set step in our script is responsible for that: 

doc = doc + (Document) finalString 

In this expression the finalString variable is converted into Document format of data to be added then into the file. 

This is where the most interesting part of the story begins. We have to store the updated doc variable as a file. The easiest way to do it is to use the Upload Document step in the UCCX script. However, our customer wanted to have this file in both UCCX drive (to obtain the file with FTP later on) and in the Repository. We decided to demonstrate several options for our customer in this demo script. 

So, the Write Document step allows to write the document to disk on the Cisco Unified CCX server. However, in Linux based UCCX (8.0 and newer) you can write to the Unified CCX customer folder only. Cisco Unified CCX now has only one directory that can be used by scripts to write or retrieve documents from by the customers - so called UCCX customer provided directory. The path can be stated (or declared) as a string data type in this manner:

“/opt/cisco/uccx/Customer/filename.ext”

But, because there is no guarantee of the directory location on future releases, there is a Java method that has also been provided which will produce the path to the directory. The syntax is: System.getProperty("uccx.customer.dir"). The result of this method would be: 

/opt/cisco/uccx/Customer

You would then need to append a forward slash and a filename to end up with a complete path and filename statement. So that's why the properties of our Write Document step in the demo UCCX script looks like that: 


In Filename field we configured System.getProperty("uccx.customer.dir") + "/" + "callresult.txt" expression. It will write our document in callresult.txt to UCCX /opt/cisco/uccx/Customer folder. 

Then you can upload the doc variable to Repository. You can do it directly from the script with the Upload Document step, or you can read the content of the saved file from the UCCX customer provided directory and then upload its content. In the demo script I will show you, how to read the file from the folder and upload it to Repository. 

The step Upload Document requires user authentication. So that's why there are Get User and Authenticate User steps. First one checks if the user exist in UCCX and gets the user data to the User variable named user, and the second step provides the password of this user for authentication. You can find an example of these steps in another post in the blog here

After defining the user, we apply Create File Document step to read the text file from the the UCCX customer provided directory. The content will be placed again to Document variable doc (we specify the path here again with  System.getProperty("uccx.customer.dir") + "/" + "callresult.txt" expression): 



And finally let's upload the variable doc to the UCCX Repository. Once again - we could do it even without reading the file from the UCCX disk, but I showed you previous command just for training purposes. So, here we go:


Now, save your script, upload it to the UCCX repository and don't forget to refresh your UCCX application using this script. Then make test calls, and check if new records appeared in the textfile.txt (you have to download  the file from your UCCX Repository of course to verify it. It can be done through CCX Administration web page). I made four test calls to this script and found four records in the file: 



You can see Calling Number (2001), Pin Code (12345), Call Date (10/20/22) and Call Time (7:01 am). 

1 comment: