Skip to main content

We have created a Python object that sends SMS/text messages using Twilio, and it is working (mostly - see below) .

Code:

import os                                                            
from twilio.rest import Client                                       
                                                                     
def SendSMS(FromNumber,ToNumber,MessageText):                        
    account_sid = "*****************"               
    auth_token = "****************""                  
    client = Client(account_sid, auth_token)                         
                                                                     
    message = client.messages                                      
                .create(                                             
                     body=MessageText,                               
                     from_=FromNumber,                               
                     to=ToNumber                                     
                 )                                                   
                                                                     
    return message.sid  

  

When we call this from Universe, the first time it runs properly. If we then call it again immediately, we get the following message on-screen in Universe, and the message does not send:

Can anyone tell us what we are doing incorrectly and/or how we can fix this?

Frank Hanshaw

Circle Commerce

We have created a Python object that sends SMS/text messages using Twilio, and it is working (mostly - see below) .

Code:

import os                                                            
from twilio.rest import Client                                       
                                                                     
def SendSMS(FromNumber,ToNumber,MessageText):                        
    account_sid = "*****************"               
    auth_token = "****************""                  
    client = Client(account_sid, auth_token)                         
                                                                     
    message = client.messages                                      
                .create(                                             
                     body=MessageText,                               
                     from_=FromNumber,                               
                     to=ToNumber                                     
                 )                                                   
                                                                     
    return message.sid  

  

When we call this from Universe, the first time it runs properly. If we then call it again immediately, we get the following message on-screen in Universe, and the message does not send:

Can anyone tell us what we are doing incorrectly and/or how we can fix this?

Frank Hanshaw

Circle Commerce

Hi Frank,

That's a warning. Does the text message still go through?  Is it possible to just ignore it? If you're using an older version of python you could try updating it.

I've been using Twilio from Universe for a while. I wrote a stand alone python script that works from the command line. I had to do it that way since we don't have python licensed on our universe system. It's pretty close to what you're doing so maybe it will be useful. This works directly on our linux Centos 7 machine. It looks like you're using Windows. I haven't tried it there. If you're doing it on Windows through an AccuTerm or other terminal emulator, this would let you run it directly on your host machine. You would have to install Twilio and Python on your host but it worked for me.

I mentioned that I've been using it for a while. We originally would write items to a shared folder and then had a process running on a Windows machine that would read it and send the text. This eliminated all that and made the whole process cleaner and more reliable.

sendsms.py

#/usr/bin/env/ python3

# A program using twilio to send an SMS text message. The phone number
# and message are passed through the command line. Since the message will
# usually have spaces it needs to be enclosed in single or double quotes.
# This program will normally be evoked with the BASIC function 
# BARS.UTL.BP FN.SEND.SMS.TEXT.MESAGE.

# Created 2022-11-14 by Joe Goldthwaite


import sys
from twilio.rest import Client
from twilio.base.exceptions import TwilioRestException

def sendSMStwilio(to_number, message):
    
    account_sid = 'xxxxxxxx'
    auth_token = 'xxxxxxxx'
    
    try:
        client = Client(account_sid, auth_token)
    except TwilioRestException as err:
        return False, str(err)
    
    try:
        #print("Using client")
        client.messages.create(
            to="+1" + to_number,
            from_="+1xxxxxxxxxx",
            body=message
        ) 
    except TwilioRestException as err:
        return False, str(err)
    
    return True, "Success"

if __name__ == '__main__':
    """This programs uses twilio to send a text SMS message to the phone number passed on the command line"""
    to_number = sys.argv[1]
    message = sys.argv[2]
    
    message = message.replace('|','
')

    result, result_message = sendSMStwilio(to_number, message)
    
    print(result, result_message)

Next I have a function that I call to execute the python script using the SH command:

PYTHON.PY FN.SEND.SMS.TEXT.MESSAGE

FUNCTION FN.SEND.SMS.TEXT.MESSAGE(PHONE.NUMBER, MESSAGE)
!
* This program sends a text SMS message to the passed int phone number and message.
* The message can have multiple lines separated by attribute marks. It works by
* executing a python program PYTHON.PY sendsms.py. The python routine gets the
* phone number and message from the command line and uses twilio to send
* the actual message.

* Created 2022-11-15 by Joe Goldthwaite

MESSAGE = CHANGE(MESSAGE, @AM, '|')
RAW.COMMAND = "/usr/bin/python3 /accts/source/accts/mgt/EIS/PYTHON.PY/sendsms.py ":PHONE.NUMBER:" '":MESSAGE:"'"
SHELL.COMMAND = 'SH -c "':RAW.COMMAND:'"'
EXECUTE SHELL.COMMAND CAPTURING OUTPUT
RETURN(OUTPUT)

Since I'm passing the message through the command line you need to keep the text message plane and simple. No single or double quotes.


We have created a Python object that sends SMS/text messages using Twilio, and it is working (mostly - see below) .

Code:

import os                                                            
from twilio.rest import Client                                       
                                                                     
def SendSMS(FromNumber,ToNumber,MessageText):                        
    account_sid = "*****************"               
    auth_token = "****************""                  
    client = Client(account_sid, auth_token)                         
                                                                     
    message = client.messages                                      
                .create(                                             
                     body=MessageText,                               
                     from_=FromNumber,                               
                     to=ToNumber                                     
                 )                                                   
                                                                     
    return message.sid  

  

When we call this from Universe, the first time it runs properly. If we then call it again immediately, we get the following message on-screen in Universe, and the message does not send:

Can anyone tell us what we are doing incorrectly and/or how we can fix this?

Frank Hanshaw

Circle Commerce

Sorry about the crap formatting. I wasn't sure how to format code and didn't associate {;} with the code formatting option. I used the HTML option with <pre></pre> around my code. It looked ok in the forum but it stripped all the leading spaces from the email.  That breaks python code. I'll try the {;} next time.


We have created a Python object that sends SMS/text messages using Twilio, and it is working (mostly - see below) .

Code:

import os                                                            
from twilio.rest import Client                                       
                                                                     
def SendSMS(FromNumber,ToNumber,MessageText):                        
    account_sid = "*****************"               
    auth_token = "****************""                  
    client = Client(account_sid, auth_token)                         
                                                                     
    message = client.messages                                      
                .create(                                             
                     body=MessageText,                               
                     from_=FromNumber,                               
                     to=ToNumber                                     
                 )                                                   
                                                                     
    return message.sid  

  

When we call this from Universe, the first time it runs properly. If we then call it again immediately, we get the following message on-screen in Universe, and the message does not send:

Can anyone tell us what we are doing incorrectly and/or how we can fix this?

Frank Hanshaw

Circle Commerce

Hi Frank, can you show how you are calling the python module from UniVerse?  Based on the following from stackoverflow, https://stackoverflow.com/questions/49757245/fixing-warning-message-related-to-mpd-setminalloc, I suspect that UniVerse is initializing the Python runtime with each run of the python code, and the python decimal module is emitting a warning that it was already intialized.

That said, as Joe stated, and further explained in the above article, it appears to be just a warning.  So I'm not sure why this would cause the SMS to not get sent beyond the first time.

Steve


We have created a Python object that sends SMS/text messages using Twilio, and it is working (mostly - see below) .

Code:

import os                                                            
from twilio.rest import Client                                       
                                                                     
def SendSMS(FromNumber,ToNumber,MessageText):                        
    account_sid = "*****************"               
    auth_token = "****************""                  
    client = Client(account_sid, auth_token)                         
                                                                     
    message = client.messages                                      
                .create(                                             
                     body=MessageText,                               
                     from_=FromNumber,                               
                     to=ToNumber                                     
                 )                                                   
                                                                     
    return message.sid  

  

When we call this from Universe, the first time it runs properly. If we then call it again immediately, we get the following message on-screen in Universe, and the message does not send:

Can anyone tell us what we are doing incorrectly and/or how we can fix this?

Frank Hanshaw

Circle Commerce

Hi Frank, Here is a link to a  article on sending SMS. i didn't see this issue with my example.

How to Send SMS Text Messages From U2 With Python | Rocket Software


We have created a Python object that sends SMS/text messages using Twilio, and it is working (mostly - see below) .

Code:

import os                                                            
from twilio.rest import Client                                       
                                                                     
def SendSMS(FromNumber,ToNumber,MessageText):                        
    account_sid = "*****************"               
    auth_token = "****************""                  
    client = Client(account_sid, auth_token)                         
                                                                     
    message = client.messages                                      
                .create(                                             
                     body=MessageText,                               
                     from_=FromNumber,                               
                     to=ToNumber                                     
                 )                                                   
                                                                     
    return message.sid  

  

When we call this from Universe, the first time it runs properly. If we then call it again immediately, we get the following message on-screen in Universe, and the message does not send:

Can anyone tell us what we are doing incorrectly and/or how we can fix this?

Frank Hanshaw

Circle Commerce

Hi Frank,

I just tested here locally, the twilio.rest module is not compatible with the re-initializaion of Python that occurs within UniVerse/UniData. This is why it will run fine the first time, and the second will fail.

To fix this, in a fresh UV/UD session run 'REINIT.PYTHON OFF' 

Then run your python program back to back and it should work.

You can add the REINIT.PYTHON OFF to a LOGIN paragraph to make this a permanent change. 

Regards,

Dave Weinstiein