Python

This tutorial provides you with a step-by-step walkthrough on how to send a message using the Live Link 365 API.

This is not a production-ready application. Please take your time to enhance it for production so that it meets your specific business requirements.

Steps

Code

keyboard_arrow_down
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# _Step_1
import json
import requests
import configparser

# Introduction..
# In this Tutorial we will write a simple Python code that can send a message to any phone number
# You can run this application either on Terminal or in your preferred IDE.
# The application will receive 2 input parameters that will be asked for the user upon its execution
# First parameter will be the Message to be sent.
# Second parameter will be the phone that will receive the message (Phone numbers have to be in E.164 format).
# Requirements:
# Basic Programming experience with Python.
# Python 3 Installed.
# requests Library Installed (http://docs.python-requests.org/en/master/).
# Generated an app_key and app_secret through the Live Link 365 Portal
# For that, go to the portal https://livelink.sapdigitalinterconnect.com/guides/
# Then go to Getting Started >> Step 3: Create your first APP Key


# SETUP ENVIROMENT VARIABLES #
# On this step we will setup all the needed Variables to make the Live Link 365 System Work.
# base_url -> the Live Link 365 URL that will be used to make the calls to Live Link 365 API
# app_key -> the key generated through the Live Link 365 Portal
# app_secret -> the secret that you received when creating a new app_key through the Live Link 365 Portal
# default_origin -> that is the origin that you want the phone number to receive messages from.
# All this information will be used in the next functions.

# PROXY
http=http://proxy:8080
https=http://proxy:8080

config = configparser.ConfigParser()
config.read('./resources/config.ini')

base_url = config['API']['base_url']
app_key = config['API']['app_key'] # Insert your generated app key here
app_secret = config['API']['app_secret'] # Insert your generated app secret here
default_origin = config['API']['default_origin'] # Insert your default origin here

# Extra info.
# If you are Behind a corporate proxy, you need to set the proxy to send HTTP Calls
# global_proxy = {
#  'http': config['PROXY']['http'],
#  'https': config['PROXY']['https']
# }

# _Step_3
# Defining Function to retrieve the oAuth2 access_token through client_credentials
def getAccessToken(key, secret, proxy=None):
    oauth_url = base_url + "/oauth/token"

    payload = {'grant_type': "client_credentials"}

    headers = {
        'content-type': "application/x-www-form-urlencoded"
    }

    if 'global_proxy' in globals(): proxy = global_proxy
    # possible to implement some logic to validate expiration time so don't request token for each  call.

    print("...... executing authorization request with key: " + key)
    result = requests.post(
        url=oauth_url,
        data=payload,
        headers=headers,
        auth=(key, secret),
        proxies=proxy
    )

    if result.status_code != 200:
        return false

    access_token = json.loads(result.text)['access_token']

    print("...... authorization request successfull access token: " + access_token)

    return access_token


# _Step_4
# Function to send message with two mandatory parameters, message and destination.
# When calling this function, this will use requests library to prepare an HTTP Call
# This HTTP Call will have an Authorization Header with a Bearer access_token
# that was retrieved from the getAccessToken function.

def sendSMS(message, destination, origin=default_origin, base_url=base_url, key=app_key, secret=app_secret,
                proxy=None):
    message_url = base_url + "/v2/sms"

    payload = {
        'message': message,
        'origin': origin,
        'destination': destination
    }

    headers = {
        'content-type': "application/json",
        'authorization': "Bearer " + getAccessToken(key, secret)
    }

    if 'global_proxy' in globals(): proxy = global_proxy

    print("..... sending message: " + message + ", to: " + destination)

    result = requests.post(
        url=message_url,
        data=json.dumps(payload),
        headers=headers,
        proxies=proxy
    )

    if result.status_code != 200:
        user_message = json.loads(result.text)['userMessage']
        print("..... failed to send message")
        print("..... Error: " + user_message)
        return user_message

    print("..... message sent Successfully")
    return result.text

# _Step_5
# Now we will write a program that receives 2 inputs
# First it will ask for the Message that you want to send
# Then it will ask for the phone number that you want to send the message to (Phone numbers have to be in E.164 format).
####################################################
#           Program Execution Start
####################################################


print("Starting message application")

text = input("What do you want to say(text message): ")
destination = input("Who do you want to send to(phone number): ")

print("sending: " + text)
print("to:      " + destination)

sendSMS(text, destination)
;_Step_2
[API]
;Insert your api url here
base_url=https://livelink.sapdigitalinterconnect.com/api
;Insert your generated app key here
app_key=appKey
;Insert your generated app secret here
app_secret=appSecret
;Insert your default origin here
default_origin=

[PROXY]
http=http://proxy:8080
https=http://proxy:8080