Search the OSCAR Documentation
Ring Central Fax Gateway
Now install the Python Script
A python script should be installed in /usr/local/bin/RCfax.py. You need to have Python 3 to run it and to provide credentials for your Ring Central account for SERVER_URL = CALLER = CLIENT_ID CLIENT_SECRET and JWT_TOKEN
#!/usr/bin/env python3
# https://developers.ringcentral.com/guide/messaging/fax/sending-faxes#python
# extended by Tom Le
import re
import json
import os.path
import base64
import logging
import shutil, os
import sys, getopt
import time
from ringcentral import SDK
#****************************
# Tom Le added the following to handle fax info., eg. fax #, filename to be fax,
#****************************
# get command line args
myopts, args = getopt.getopt(sys.argv[1:],"n:f:s:b:")
#*******
# o == option
# a == argument passed to the o
#*******
# process the options
for o, a in myopts:
if o == '-n':
fax_no=a
elif o == '-f':
fax_file=a
elif o == '-s':
cover_subject=a
elif o == '-b':
archDir=a
else:
print("Usage: %s -n faxnumber -f file -s subject -b archDir" % sys.argv[0])
sys.exit
print (" ")
print("* Backup Dir. is : %s " % (archDir))
print("* Fax file is : %s " % (fax_file))
print ("* Fax number is : %s" % fax_no)
# Tom Le added this to get the fax number and pdf files to ready for archive
temp_filename = str(os.path.splitext(fax_file)[0])
print ("* Fax number filename is : %s" % temp_filename+".txt")
print ("* Fax PDF filename is : %s" % temp_filename+".pdf")
print (" ")
# should be the number in the filename in temp fax directory with .txt extension
RECIPIENT = fax_no
# should be the PDF filename that in temp fax directory with .pdf extension
FILE_TO_FAX = fax_file
#****************************
#****************************
# 1) uncomment the correct fax SERVER_URL
# 2) need to provide data for all variables in CAPITAL
#****************************
# For TESTING
#SERVER_URL = "https://platform.devtest.ringcentral.com"
#
# For PRODUCTION
#SERVER_URL = "https://platform.ringcentral.com"
#
CALLER = ""
CLIENT_ID = ""
CLIENT_SECRET = ""
JWT_TOKEN = ""
#****************************
# Send a high resolution fax message to a recipient number
def send_fax():
try:
builder = rcsdk.create_multipart_builder()
builder.set_body({
'to': [{ 'phoneNumber': RECIPIENT }],
# To send fax to multiple recipients, add more 'phoneNumber' object. E.g.
#
# to: [
# { 'phoneNumber': "Recipient1-Phone-Number" },
# { 'phoneNumber': "Recipient2-Phone-Number" }
# ],
'faxResolution': "High",
'coverPageText': ""
})
#with open('test.jpg', "rb") as f: #ORIGINAL CODE
with open(FILE_TO_FAX, "rb") as f:
content = f.read()
#attachment = ('test.jpg', content)
attachment = (FILE_TO_FAX, content)
builder.add(attachment)
request = builder.request('/restapi/v1.0/account/~/extension/~/fax')
resp = platform.send_request(request)
jsonObj = resp.json()
print ("Fax sent. Message id: " + str(jsonObj.id))
check_fax_message_status(jsonObj.id)
except Exception as e:
print (e)
# Check the sending message status until it's no longer in the queued status
def check_fax_message_status(messageId):
try:
endpoint = "/restapi/v1.0/account/~/extension/~/message-store/" + str(messageId)
resp = platform.get(endpoint)
jsonObj = resp.json()
print ("Message status: " + jsonObj.messageStatus)
if jsonObj.messageStatus == "Queued":
time.sleep(30)
check_fax_message_status(jsonObj.id)
#****************************
# Tom Le added this to move the fax to archive if fax successfully submitted,
# eg. got a messageStatus as Sent from RC
#****************************
print (" ")
if jsonObj.messageStatus == "Sent":
print (" ")
dst_txt_filename = archDir + os.path.basename(temp_filename)+".txt"
dst_pdf_filename = archDir + os.path.basename(temp_filename)+".pdf"
shutil.move(temp_filename+".txt", dst_txt_filename)
outputStr1 = "- Moved fax_no File : " + temp_filename + ".txt TO " + dst_txt_filename
print (outputStr1)
shutil.move(temp_filename+".pdf", dst_pdf_filename)
outputStr2 = "- Moved FAXED file : " + temp_filename + ".pdf TO " + dst_pdf_filename
print (outputStr2)
#****************************
except Exception as e:
print (e)
# Authenticate a user using a personal JWT token
def login():
try:
#platform.login( jwt=os.environ.get('RC_JWT') ) #ORIGINAL CODE
platform.login( jwt= JWT_TOKEN )
send_fax()
except Exception as e:
print ("Unable to authenticate to platform. Check credentials." + str(e))
##########
# Instantiate the SDK and get the platform instance
##########
rcsdk = SDK(CLIENT_ID, CLIENT_SECRET, SERVER_URL)
platform = rcsdk.platform()
login()
##########
