News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
Python SMS Script os.envrion.get Questions And Working SMS Python Script
Tags: sms and text messaging
Sep 17, 2021 at 1:34pm   •   1 replies  •  0 likes
William Hancock

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()

1 Answer
answered on Sep 20, 2021 at 8:49am  

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.


 0



A new Community is coming to RingCentral!

Posts are currently read-only as we transition into our new platform.

We thank you for your patience
during this downtime.

Try Workflow Builder

Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!

Try RingCentral Workflow Builder

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us