How to Send SMS Text Messages from U2 with Python
Short Message Service (SMS) text messages are a popular means of communication all over the world. It is easy to send SMS text messages with Python using a web Application Programming Interface (API). This article will show how to add SMS capability to your U2 UniVerse application.
UniVerse 11.3.2 and higher with Python licensed (free add-on package).
- Refer to the Python User Guide on licensing process.
- Twilio Python helper library, version 6.0.0 or later
Using a Web API
The example BASIC code uses the Python API to run a script which calls a Web API module that sends an SMS and returns a confirmation. The parameters include the phone number and the message to text. The example script requires a Twilio trial account (free) and the Python helper library version 6 to send out text messages. This library requires Python 3.7 which is available in UniVerse 11.3.2 and higher. Python 3.7 support is also available in UniData 8.2.2 and D3 10.3.1.
The Twilio trial account allows you to send text messages to verified phone numbers only. When you want to send SMS to any phone number you can upgrade your account.
After signing up for your free trial account, you will get an account id, authentication token and a phone number. You will need to include this information when calling their Web API. The example shows how to send a text, but you can also receive texts. We plan to show how in a future article.
Installing helper library
To install the helper library, do the following depending on the OS. (Be sure you have internet access.)
For Windows, run the dos command prompt CMD.EXE and go to the directory where UniVerse has installed the Python scripts directory. The default is c:\u2\uv\python\scripts. At the dos prompt run the command:
pip install twilio
The above screenshot shows the helper library being installed.
For Linux, start a terminal session and go to the directory where UniVerse has installed the python bin directory. Default is /usr/uv/python/bin. Run the command:
./pip3.7 install twilio
In the above screenshot the helper library was already installed so messages are different.
After you have successfully installed the helper library, you can login to the XDEMO account that should have been installed with UniVerse. If you don’t have that account refer to the Python User Guide for instructions on adding custom paths for your accounts.
The following is the BASIC code to add to the PBP directory in the XDEMO account. This example is named SENDSMS.
Note the variable “ModuleName” has the name of the Python Script to call and the variable “FuncName” has the name of the function to call. The INPUT variables are the phone number to text and the message to send. All four variables are then passed to the Python API ‘PyCallFunction’. The variable “pyresult” will contain the message confirmation id if successful otherwise “@EXCEPTIONTYPE” will contain the error.
The following Python script should be added to the PP directory in the XDEMO account. It is named ‘send_sms.py’ matching the content of the variable ‘ModuleName’ minus the ‘.py’ extension from the BASIC code above.
Note the Twilio account id, token and phone number to use are “hardcoded” into the script for simplicity. You can also pass that information from BASIC if you prefer.
Running the SENDSMS program
In summary, using Python to call Web APIs can save U2 Developers time and effort and add new value to existing applications. We hope you found this article to be of interest.