question

William Hancock avatar image
William Hancock asked Phong Vu answered

Python SMS Script os.envrion.get Questions And Working SMS Python Script

I have been asked to set up SMS testing as part of a RingCentral rollout at work, and after trying and failing to get the .Net version of SMS Quickstart set up, I decided to try my luck with Python3 instead.


I have been able to successfully set up the Python script in a Linux machine, though it required modification to get working, so I thought I would ask a question about why the original code did not work and share the simple changes I had to make to get it working.

The instructions in the quickstart say to save the example script as sms.py and then add in your account access information into the following variables:


# CLIENTID = os.environ.get('RC_CLIENT_ID')

# CLIENTSECRET = os.environ.get('RC_CLIENT_SECRET')

# SERVER = os.environ.get('RC_SERVER_URL')

# USERNAME = os.environ.get('RC_USERNAME')

# PASSWORD = os.environ.get('RC_PASSWORD')

# EXTENSION = os.environ.get('RC_EXTENSION')

# RECIPIENT = os.environ.get('RECIPIENT_PHONE')


I tried changing the data in the get('USER_DATA_VARIABLES') to the data that is available in my developer account API, but this caused the "Unable to authenticate to platform. Check credentials" error to be thrown.

Digging into this problem, it appears that the get method is intended to pull environment variables, so I figured that this was used to set the user data in a single place and make it easy to maintain changes, but when I searched around I learned that this is not possible with Python and would require some Bash shell trickery if it were going to work as expected. I am hoping someone can advise me on the benefits of using the os.envrion.get() methods and how they should be used and set, because this seems somewhat redundant:

import os,sys


os.environ['RC_CLIENT_ID'] = 'myClientID'

CLIENTID = os.environ.get('RC_CLIENT_ID')

print(CLIENTID)


When we could just do this and not have to worry about environment variables:

import os,sys


CLIENTID = 'myClientID'

print(CLIENTID)


My question is: What is the benefit of using environment variables if there isn't a way to set them for other scripts in a way that would let us call all of our various RingCentral scripts while easily changing the API validation information from a single source?


So, like I said, I wanted to the modification to the Python quickstart script that did work for me, I literally just replaced the right side of the = with my API validation information and once that was complete, it was working as expected. Even though it is an easy fix, I am sure there are other people like me who have never used Python before that would like a "plug and play" solution, and if I am really lucky someone who knows Python can chime in on the use cases for the os.envrion.get and how to set that up to persist across all of the various RingCentral scripts that a company may choose to deploy.


Here's the Python script:


#!/usr/bin/env python3

# -*- coding: utf-8 -*-

"""

Created on Fri Sep 17 18:57:34 2021


@author: dietpi

"""


#!/usr/bin/env python

# quick-start.py - This script helps developers send their first SMS message

#

# Variables:

# RC_CLIENT_ID, RC_CLIENT_SECRET, RC_SERVER_URL: Connection info

# RC_USERNAME, RC_PASSWORD, RC_EXTENSION: Auth credentials

# RECIPIENT_PHONE: The phone number to send the SMS to

#

# License: MIT

# Copyright: 2021 RingCentral, Inc.

from ringcentral import SDK

import os,sys


env_var = os.environ


pprint.pprint(dict(env_var), width = 1)


# we could choose to set the envrionment variables and then use a get

# function to set the variables that the script usese here

# !!!! NOTE, the code below is not included in the QuickStart Python Script


# os.environ('RC_CLIENT_ID') = 'EnterYourValidationInfoHere'

# os.environ('RC_CLIENT_SECRET') = 'EnterYourValidationInfoHere'

# os.environ('RC_SERVER_URL') = 'EnterYourValidationInfoHere'

# os.environ('RC_USERNAME') = 'EnterYourValidationInfoHere'

# os.environ('RC_PASSWORD') = 'EnterYourValidationInfoHere'

# os.environ('RC_EXTENSION') = 'EnterYourValidationInfoHere'

# os.environ('RECIPIENT_PHONE') = 'EnterYourValidationInfoHere'



# the code below will get the values you just loaded in the step above and

# put them into script variables for use in the API calls


# CLIENTID = os.environ.get('RC_CLIENT_ID')

# CLIENTSECRET = os.environ.get('RC_CLIENT_SECRET')

# SERVER = os.environ.get('RC_SERVER_URL')

# USERNAME = os.environ.get('RC_USERNAME')

# PASSWORD = os.environ.get('RC_PASSWORD')

# EXTENSION = os.environ.get('RC_EXTENSION')

# RECIPIENT = os.environ.get('RECIPIENT_PHONE')


# This replaces the two code bocks above and goes directly to the

# "load the variables" step. You can get this information by logging into

# your developer account and then clicking on Console in the Blue

# Navigation bar towards the top of the page, then select the App you wish

# to use (or create app if you didn't do that part yet).

# once you are looking at the app, in the Sandbox Credentials heading,

# click the "View All Credentials" link on the right hand side

# you just copy/paste the relivent data

# NOTE: make sure to escape any special characters reuqired in Python


CLIENTID = 'YourClientID'

CLIENTSECRET = 'YourClientSecret'

SERVER = 'AppServerURL'

USERNAME = '+YourAccountUserName'

PASSWORD = 'YourAccountPassword'

EXTENSION = 'YourExtension'

RECIPIENT ='PhoneNumberToTextIncludingCountryCode'



rcsdk = SDK( CLIENTID, CLIENTSECRET, SERVER )

platform = rcsdk.platform()

try:

platform.login(USERNAME, EXTENSION, PASSWORD)

except:

sys.exit("Unable to authenticate to platform. Check credentials.")


def read_extension_phone_number():

try:

resp = platform.get("/restapi/v1.0/account/~/extension/~/phone-number")

jsonObj = resp.json()

except e:

sys.exit("Unable to fetch SMS-enabled phone numbers")

for record in jsonObj.records:

for feature in record.features:

if feature == "SmsSender":

return send_sms(record.phoneNumber)

sys.exit("No SMS-enabled phone number found")


def send_sms(fromNumber):

try:

resp = platform.post('/restapi/v1.0/account/~/extension/~/sms',

{

'from' : { 'phoneNumber': fromNumber },

'to' : [ {'phoneNumber': RECIPIENT} ],

'text' : 'Hello World from Python'

})

jsonObj = resp.json()

except:

sys.exit("Unable to send SMS")

print (jsonObj.messageStatus)


read_extension_phone_number()

sms and text messaging
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Phong Vu avatar image
Phong Vu answered

Sorry for the confusion. That quick started code some how still shows the old way. It is a good practice to not add sensitive information, such as app credentials and user login credentials, directly in the code and we thought we should show that best practice to developers. However, many developers are not familiar with that and get confused. That is why removed such a code from all other getting started but this one is left with that part w/o the instruction how to put sensitive data to the environment.

If you check the code of other getting started topics you will see these lines:

RINGCENTRAL_CLIENTID = '<ENTER CLIENT ID>'
RINGCENTRAL_CLIENTSECRET = '<ENTER CLIENT SECRET>'
RINGCENTRAL_SERVER = 'https://platform.devtest.ringcentral.com'

RINGCENTRAL_USERNAME = '<YOUR ACCOUNT PHONE NUMBER>'
RINGCENTRAL_PASSWORD = '<YOUR ACCOUNT PASSWORD>'
RINGCENTRAL_EXTENSION = '<YOUR EXTENSION, PROBABLY "101">'

So thanks for your inputs, we will fix the problem soon.

1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys