diff --git a/FlowrouteMessagingLib/Configuration.py b/FlowrouteMessagingLib/Configuration.py index e188840..39962d8 100644 --- a/FlowrouteMessagingLib/Configuration.py +++ b/FlowrouteMessagingLib/Configuration.py @@ -3,10 +3,10 @@ """ FlowrouteMessagingLib.Configuration - Copyright Flowroute, Inc. 2016 + Copyright Flowroute, Inc. 2021 """ class Configuration(object): # The base Uri for API calls - BASE_URI = "https://api.flowroute.com/v2" + BASE_URI = "https://api.flowroute.com/v2.1" diff --git a/FlowrouteMessagingLib/Controllers/APIController.py b/FlowrouteMessagingLib/Controllers/APIController.py index 643bbb1..ea3447d 100644 --- a/FlowrouteMessagingLib/Controllers/APIController.py +++ b/FlowrouteMessagingLib/Controllers/APIController.py @@ -5,6 +5,7 @@ Copyright Flowroute, Inc. 2016 """ import requests +from requests.auth import HTTPBasicAuth from FlowrouteMessagingLib.APIHelper import APIHelper from FlowrouteMessagingLib.Configuration import Configuration @@ -24,7 +25,7 @@ def __init__(self, username, password): self.__username = username self.__password = password - def create_message(self, message) -> dict: + def create_message(self, message): """Does a POST request to /messages. Send a message. @@ -49,21 +50,22 @@ def create_message(self, message) -> dict: query_builder += "/messages" # Validate and preprocess url - query_url = APIHelper.clean_url(query_builder) + _query_url = APIHelper.clean_url(query_builder) # Prepare headers - headers = { - "user-agent": "Flowroute Messaging SDK 1.0", - "content-type": "application/json; charset=utf-8", + _headers = { + 'accept': 'application/vnd.api+json', + 'content-type': 'application/vnd.api+json; charset=utf-8' } - # Prepare and invoke the API call request to fetch the response - response = requests.post( - url=query_url, - headers=headers, - data=APIHelper.json_serialize(message), - auth=(self.__username, self.__password)) + # Prepare and execute request + response = requests.post(_query_url, + headers=_headers, + auth=HTTPBasicAuth(self.__username, self.__password), + data=APIHelper.json_serialize(message)) + json_content = APIHelper.json_deserialize(response.content) + print("Resp: {}".format(response.json())) # Error handling using HTTP status codes if response.status_code == 401: @@ -78,7 +80,7 @@ def create_message(self, message) -> dict: return json_content - def get_message_lookup(self, record_id: str) -> dict: + def get_message_lookup(self, record_id): """Does a GET request to /messages/{record_id}. Lookup a Message by MDR. diff --git a/FlowrouteMessagingLib/Models/Message.py b/FlowrouteMessagingLib/Models/Message.py index 94a677f..a93498d 100644 --- a/FlowrouteMessagingLib/Models/Message.py +++ b/FlowrouteMessagingLib/Models/Message.py @@ -28,12 +28,16 @@ def __init__(self, **kwargs): self.to = None self.mfrom = None self.content = None + self.is_mms = False + self.media_urls = [] # Create a mapping from API property names to Model property names replace_names = { "to": "to", "from_": "mfrom", "content": "content", + "is_mms": "is_mms", + "media_urls": "media_urls" } # Parse all of the Key-Value arguments @@ -61,6 +65,8 @@ def resolve_names(self): "to": "to", "mfrom": "from", "content": "content", + "is_mms": "is_mms", + "media_urls": "media_urls" } retval = dict() diff --git a/demo_send.py b/demo_send.py index 09331d6..3e298d0 100644 --- a/demo_send.py +++ b/demo_send.py @@ -21,14 +21,14 @@ from FlowrouteMessagingLib.Models.Message import * # Set up your API credentials -# Please replace the variables in Configuration.php with your information. -username = os.getenv('ACCESS_KEY') -password = os.getenv('SECRET_KEY') -from_number = os.getenv('FROM_E164') -to_number = os.getenv('TO_E164') +# Please replace the variables with your information. +username = 'YOUR-ACCESS-KEY' +password = 'YOUR-SECRET-KEY' +from_number = 'YOUR-MESSAGE-ENABLED-FLOWROUTE-DID' +to_number = 'RECIPIENT-DID' # Print the demo script header. -print("Flowroute, Inc - Demo SMS Python script.\n") +print("Flowroute, Inc - Demo SMS / MMS Python script.\n") if username is None or password is None or from_number is None or to_number is None: print("To operate this script, please set the environment variables as follows:") print("'ACCESS_KEY'=Your account tech_prefix") @@ -42,15 +42,17 @@ pprint.pprint(controller) # Build your message. -message = Message(to=to_number, from_=from_number, content='Your cool new SMS message here!') +txtMessage = Message(to=to_number, from_=from_number, content='Your cool new SMS message here!', + media_urls=[], is_mms=False, dlr_callback=None) # Send your message. try: - response = controller.create_message(message) + print("Sending SMS") + response = controller.create_message(txtMessage) pprint.pprint(response) -except APIException as e: - print("Send Error - " + str(e.response_code) + '\n') - pprint.pprint(e.response_body['errors']) +except Exception as e: + print("Send Error - " + str(e) + '\n') + # pprint.pprint(e.response_body) exit(1) # can't continue from here # Get the MDR id from the response. @@ -70,3 +72,27 @@ print("MDR Retrieval Error - {} - {}".format(str(e.response_code), e.response_body)) pprint.pprint(e.response_body['errors']) exit(2) + +# Now send an MMS message +# -- Specify a publicly available image or media clip to send +image_url = 'https://developer.flowroute.com/theme/images/developers_logo_article_2x.png' +# -- If you have a server that can accept a POST message for a Delivery receipt, you can specify it here +dlr_page = None +# -- Specify the text message to go along with the media +text_content = 'A Flowroute Logo' +pictureMessage = Message(to=to_number, from_=from_number, + content=text_content, + media_urls=[image_url], + is_mms=True, # Required for MMS messages + dlr_callback=dlr_page) + +# Send your message. +try: + response = controller.create_message(pictureMessage) + pprint.pprint(response) +except APIException as e: + print("Send Error - " + str(e.response_code) + '\n') + pprint.pprint(e.response_body['errors']) + exit(1) # can't continue from here + +# Note: MMS messages do NOT have an MDR.