Jump to: navigation, search

Sample Python code to interact with Genesys API

import uuid
import urllib.parse
import http.client
def main():
    "Method to send HTTP requests to Genesys API"

    username        = "USERNAME"                        # The username provided to the client by Genesys
    password        = "PASSWORD"                        # The password provided to the client by Genesys 
    message         = "Hey"                             # The message to be sent. 
    messageId       = uuid.uuid1()                      # A unique message id.
                                                        # In this example we use UUID to generate an ID, but any value can be used as long as it's unique
    phoneNumber     = "12345"                           # The phone number to which the message is to be sent. 
    shortCode       = "99222"                           # The shortcode on this message.
    carrier         = "99999"                           # The carrier ID can be found in the reach list.

    genesysURL      = "https://smsc-api.genesyscloud.com"
    headers = {
            "Content-Type" : "application/x-www-form-urlencoded",
            "Accept" : "text/plain" }
    # QueryString to be sent to Genesys API
    messageBody = {
            "login" : username,
            "password" : password,
            "phone_number" : phoneNumber,
            "shortcode" : shortCode,
            "carrier" : carrier,
            "message_id" : messageId,
            "message" : message}
    messageBody= urllib.parse.urlencode(messageBody)

    # Sending the request to Genesys API
    conn= http.client.HTTPConnection(genesysURL)
    conn.request("POST","/httpapi/receiver", messageBody, headers)

    response= conn.getresponse()
    # Response Code. If not in 2XX region then the request failed
    if response.status < 200 or response.status > 299:
        print("Request not successful. Please try again.")
        resultCode = response.read().decode().split('=')[1]

    if resultCode == '0':
        print("Success: The message has been accepted for processing.")
    elif resultCode == '1':
        print("Failure: Either one(or more) require parameters are missing. ")
    elif resultCode == '2':
        print("Failure: The credentials provided are either wrong or do not exist.")
    elif resultCode == '3':
        print("Failure: The carrier ID is not an Integer value or it does not exist in the reach list.")
    elif resultCode == '4':
        print("Failure: The shortcode is not an Integer.")
    elif resultCode == '5':
        print("Failure: The shortcode is not registered for the client.")
    elif resultCode == '6':
        print("Failure: The client is trying to send more messages in the allotted time than they are allowed.")
    elif resultCode == '7':
        print("Failure: This message is a duplicate.")
    elif resultCode == '8':
        print("Failure: The message is larger than allowed for the client.")
    elif resultCode == '98':
        print("Failure: Internal Server Error. Please try again")
        print("Failure: There was some error in processing request. Please try again")

if __name__ == '__main__':
This page was last edited on November 18, 2019, at 15:11.
Comments or questions about this documentation? Contact us for support!