-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathexample_send_subscriptions_outbound_message.py
More file actions
85 lines (67 loc) · 2.94 KB
/
example_send_subscriptions_outbound_message.py
File metadata and controls
85 lines (67 loc) · 2.94 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
# -*- coding: utf-8 -*-
import pdb
import argparse
import sys as sys
import logging as logging
import time as time
import oneapi as oneapi
import oneapi.models as models
import oneapi.dummyserver as dummyserver
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--server", help="Address of the server (default=https://oneapi.infobip.com)")
parser.add_argument("username", help="Login")
parser.add_argument("password", help="Password")
parser.add_argument("sender", help="From address")
parser.add_argument("-p", "--port", help="local port for delivery notification")
parser.add_argument("-d", "--data_format", help="Type of data used in request, can be url or json (default=url)")
parser.add_argument("-a", "--accept", help="Type of data used for response, can be url or json (default=url)")
args = parser.parse_args()
data_format = "url"
if args.data_format:
if (args.data_format == "json"):
data_format = "json"
port = 7090
if args.port:
port = int(args.port)
header = None
if 'accept' in locals():
if args.accept:
header = {"accept" : args.accept}
# example:initialize-sms-client
sms_client = oneapi.SmsClient(args.username, args.password, args.server)
# ----------------------------------------------------------------------------------------------------
# example:prepare-message-without-notify-url
sms = models.SMSRequest()
sms.sender_address = args.sender
sms.notify_url = 'http://{}:{}'.format('localhost', port)
sms.callback_data = 'Any string'
sms.filter_criteria = "Urgent"
# ----------------------------------------------------------------------------------------------------
# example:send-message
result = sms_client.subscribe_delivery_status(sms, header, data_format)
# store client correlator because we can later query for the delivery status with it:
resource_url = result.resource_url
# ----------------------------------------------------------------------------------------------------
if not result.is_success():
print 'Error sending message:', result.exception
sys.exit(1)
print 'Is success = ', result.is_success()
print 'Resource URL = ', result.resource_url
# Wait for 15 seconds for push-es
server = dummyserver.DummyWebServer(port)
server.start_wait_and_shutdown(15)
requests = server.get_requests()
if not requests:
print 'No requests received'
sys.exit(1)
for method, path, http_body in requests:
# example:on-delivery-notification
delivery_status = oneapi.SmsClient.unserialize_delivery_status(http_body)
# ----------------------------------------------------------------------------------------------------
print delivery_status
# Few seconds later we can delete subscription
time.sleep(10)
# example:query-for-delivery-status
sms_client.delete_delivery_status_subscription(resource_url)
# ----------------------------------------------------------------------------------------------------