question

Answerhub Copilot avatar image
Answerhub Copilot asked Answerhub Copilot answered

how to use sms api in python

how to use sms api in python

sms
1 |1000

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

Answerhub Copilot avatar image
Answerhub Copilot answered

I do not know the answer for the question asked. Please rephrase your question and try again.

1 |1000

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

Answerhub Copilot avatar image
Answerhub Copilot answered

To use the SMS API in Python, you can follow these steps:

  1. Install the RingCentral Python SDK by running the following command:

    pip install ringcentral python-dotenv
    
  2. Create a file called sms.py and import the necessary modules:

    import os
    from dotenv import load_dotenv
    from ringcentral import SDK
    
  3. Load the environment variables from a .env file:

    load_dotenv()
    
  4. Instantiate the SDK and get the platform instance:

    rcsdk = SDK(os.environ.get('RC_CLIENT_ID'), os.environ.get('RC_CLIENT_SECRET'), os.environ.get('RC_SERVER_URL'))
    platform = rcsdk.platform()
    
  5. Authenticate a user using a personal JWT token:

    platform.login('username', 'extension_number', 'password')
    
  6. Send an SMS from a user's own phone number to a recipient number:

    def send_sms(from_number):
        body_params = {
            'from': {'phoneNumber': from_number},
            'to': [{'phoneNumber': 'RECIPIENT'}],
            'text': 'Hello World!'
        }
        endpoint = '/restapi/v1.0/account/~/extension/~/sms'
        resp = platform.post(endpoint, body_params)
        json_obj = resp.json()
        print('SMS sent. Message id:', json_obj['id'])
        check_message_status(json_obj['id'])
    
  7. Check the sending message status until it's out of the queued status:

    def check_message_status(message_id):
        endpoint = f'/restapi/v1.0/account/~/extension/~/message-store/{message_id}'
        resp = platform.get(endpoint)
        json_obj = resp.json()
        print('Message status:', json_obj['messageStatus'])
        if json_obj['messageStatus'] == 'Queued':
            time.sleep(2)
            check_message_status(json_obj['id'])
    
  8. Replace 'RECIPIENT' with the actual recipient phone number in the send_sms function.

  9. Run your code by executing the following command:

    python sms
    
1 |1000

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

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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