3therk1ll
3therk1ll

Reputation: 2421

Twilio return method as POST value

I am trying to build on a test Twilio script in order that the POST request can just use values returned by method is a script. The values are set as environmental variables and I have checked that they are correct in the Python shell using the same command as the script. Python 2.7 being used. The environmental variables are stored as strings and return as such.

from twilio.rest import TwilioRestClient
import os

ACCOUNT_SID = os.environ['ACCOUNT_SID']
AUTH_TOKEN = os.environ['AUTH_TOKEN']
client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)

def target_number():
    return os.environ['TARGET_NUMBER']

def my_number():
    return os.environ['MY_NUMBER']

message = client.messages.create(
    body="foo bar",  # Message body, if any
    to=target_number(),
   from_=my_number(),
)
print message.body

I am getting the following error:

python my_twilio_testing_script.py 
Traceback (most recent call last):
  File "my_twilio_testing_script.py", line 19, in <module>
    to=target_number(),
  File "my_twilio_testing_script.py", line 9, in target_number
    return os.environ['TARGET_NUMBER']
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py", line 23, in __getitem__
    raise KeyError(key)
KeyError: 'TARGET_NUMBER'

Upvotes: 0

Views: 96

Answers (1)

philnash
philnash

Reputation: 73057

Twilio developer evangelist here.

It appears that some of your environment variables are working, otherwise the script would blow up when you set the account SID and auth token.

So it seems that maybe TARGET_NUMBER is perhaps not set. In the terminal window in which you run the script, are you able to perform the following bash command:

$ echo $TARGET_NUMBER

Does that show the number you expect? If not, you may need to export the environment variable again.

Upvotes: 1

Related Questions