-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdemo_send.py
More file actions
98 lines (82 loc) · 3.24 KB
/
demo_send.py
File metadata and controls
98 lines (82 loc) · 3.24 KB
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
"""demo.py
flowroute-messaging-python is a Python SDK that provides methods
to send an outbound SMS from a Flowroute phone number and also
to retrieve a Message Detail Record (MDR). These methods use v2
(version 2) of the Flowroute API.
Copyright:
Flowroute, Inc. 2016
Prerequisites:
* Messaging enabled on both numbers.
"""
import os
import pprint
from time import sleep
import sys
from FlowrouteMessagingLib.Controllers.APIController import *
from FlowrouteMessagingLib.Models.Message import *
# Set up your API credentials
# 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 / 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")
print("'SECRET_KEY':Your API Secret Key")
print("'FROM_E164':Flowroute DID with leading +1")
print("'TO_E164':Destination DID with leading +1")
sys.exit(0)
# Create the Controller.
controller = APIController(username=username, password=password)
pprint.pprint(controller)
# Build your message.
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:
print("Sending SMS")
response = controller.create_message(txtMessage)
pprint.pprint(response)
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.
mdr_id = response['data']['id']
print("MDR ID: {}".format(mdr_id))
# Wait for message to register.
# Three seconds should be enough.
sleep(3)
# Retrieve the MDR record.
try:
# Example MDR: 'mdr1-b334f89df8de4f8fa7ce377e06090a2e'
mdr_record = controller.get_message_lookup(mdr_id)
pprint.pprint(mdr_record)
except APIException as e:
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.