-
Notifications
You must be signed in to change notification settings - Fork 41
/
sendpulse-rest-api-example.py
256 lines (209 loc) · 8.27 KB
/
sendpulse-rest-api-example.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# -*-coding:utf8-*-
""" SendPulse REST API usage example
Documentation:
https://login.sendpulse.com/manual/rest-api/
https://sendpulse.com/api
"""
from pysendpulse.pysendpulse import PySendPulse
if __name__ == "__main__":
REST_API_ID = ''
REST_API_SECRET = ''
TOKEN_STORAGE = 'memcached'
MEMCACHED_HOST = '127.0.0.1:11211'
SPApiProxy = PySendPulse(REST_API_ID, REST_API_SECRET, TOKEN_STORAGE, memcached_host=MEMCACHED_HOST)
# Get list of tasks
SPApiProxy.push_get_tasks()
# Get list of websites
SPApiProxy.push_get_websites()
# Get amount of websites
SPApiProxy.push_count_websites()
# Get list of variables for website
SPApiProxy.push_get_variables(WEBSITE_ID)
# Get list of subscriptions for website
SPApiProxy.push_get_subscriptions(WEBSITE_ID)
# Get amount of subscriptions for website
SPApiProxy.push_count_subscriptions(WEBSITE_ID)
# Activate/Deactivate subscriber, state=1 - activate, state=2 - deactivate
SPApiProxy.push_set_subscription_state(SUBSCRIBER_ID, STATE)
# Create new push task
SPApiProxy.push_create('Hello!', WEBSITE_ID, 'This is my first push message', '10',
{'filter_lang': 'en', 'filter': '{"variable_name":"some","operator":"or","conditions":[{"condition":"likewith","value":"a"},{"condition":"notequal","value":"b"}]}'})
# Get balance in Japanese Yen
SPApiProxy.get_balance('JPY')
# Get Mailing Lists list example
SPApiProxy.get_list_of_addressbooks()
# Get Mailing Lists list with limit and offset example
SPApiProxy.get_list_of_addressbooks(offset=5, limit=2)
# Add emails with variables to addressbook
emails_for_add = [
{
'email': '[email protected]',
'variables': {
'name': 'test11',
'number': '11'
}
},
{'email': '[email protected]'},
{
'email': '[email protected]',
'variables': {
'firstname': 'test33',
'age': 33,
'date': '2015-09-30'
}
}
]
SPApiProxy.add_emails_to_addressbook(ADDRESSBOOK_ID, emails_for_add)
# Delete email from addressbook
emails_for_delete = ['[email protected]']
SPApiProxy.delete_emails_from_addressbook(ADDRESSBOOK_ID, emails_for_delete)
# Get a list of variables available on a mailing list
SPApiProxy.get_addressbook_variables(ADDRESSBOOK_ID)
# Changing a variable for an email contact
SPApiProxy.set_variables_for_email(ADDRESSBOOK_ID, '[email protected]', [{'name': 'foo', 'value': 'bar'}])
# Get campaigns statistic for list of emails
emails_list = ['[email protected]']
SPApiProxy.get_emails_stat_by_campaigns(emails_list)
# Add sender "FROM" email
SPApiProxy.add_sender('[email protected]', 'Jane Roe')
# Get list of senders
SPApiProxy.get_list_of_senders()
# Add emails to unsubscribe list
SPApiProxy.smtp_add_emails_to_unsubscribe([
{'email': 'test_1@domain_1.com', 'comment': 'comment_1'},
{'email': 'test_2@domain_2.com', 'comment': 'comment_2'}
])
# Create new email campaign with attaches
task_body = "<h1>Hello, John!</h1><p>This is the test task from https://sendpulse.com/api REST API!</p>"
SPApiProxy.add_campaign(from_email='[email protected]',
from_name='Jane Roe',
subject='Test campaign from REST API',
body=task_body,
addressbook_id=ADDRESSBOOK_ID,
campaign_name='Test campaign from REST API',
attachments={'attach1.txt': '12345\n', 'attach2.txt': '54321\n'})
# Send mail using SMTP
email = {
'subject': 'This is the test task from REST API',
'html': '<h1>Hello, John!</h1><p>This is the test task from https://sendpulse.com/api REST API!</p>',
'text': 'Hello, John!\nThis is the test task from https://sendpulse.com/api REST API!',
'from': {'name': 'John Doe', 'email': '[email protected]'},
'to': [
{'name': 'Jane Roe', 'email': '[email protected]'}
],
'bcc': [
{'name': 'Richard Roe', 'email': '[email protected]'}
]
}
SPApiProxy.smtp_send_mail(email)
# Send mail with template using SMTP
email = {
'subject': 'This is the test task from REST API',
'from': {'name': 'John Doe', 'email': '[email protected]'},
'to': [
{'name': 'Jane Roe', 'email': '[email protected]'}
],
"template": {
'id': '73606', # ID of the template uploaded in the service. Use this
# (https://sendpulse.com/integrations/api/bulk-email#template-list)
# method to get the template ID (use either real_id or id parameter from the reply)
'variables': {
'foo': 'value',
'bar': 'value'
}
},
}
SPApiProxy.smtp_send_mail_with_template(email)
# **************** SMS ***************
# Add phones to address book
phones_for_add = [
'11111111111',
'22222222222'
]
SPApiProxy.sms_add_phones(ADDRESSBOOK_ID, phones_for_add)
# Add phones to address book
phones_for_add = {
"11111111111":
[
[
{"name": "test1", "type": "date", "value": "2018-10-10 23:00:00"},
{"name": "test2", "type": "string", "value": "asdasd"},
{"name": "test3", "type": "number", "value": "123"}
]
],
"22222222222":
[
[
{"name": "test1", "type": "date", "value": "2018-10-10 23:00:00"},
{"name": "test2", "type": "string", "value": "czxczx"},
{"name": "test3", "type": "number", "value": "456"}
]
]
}
SPApiProxy.sms_add_phones_with_variables(ADDRESSBOOK_ID, phones_for_add)
# Update phones variables from the address book
phones_for_update = [
'11111111111'
]
variables = [
{
"name": "name", "type": "string", "value": "Michael"
}
]
SPApiProxy.sms_update_phones_variables(ADDRESSBOOK_ID, phones_for_update, variables)
# Get information about phone from the address book
SPApiProxy.sms_get_phone_info(ADDRESSBOOK_ID, '1111111111')
# Remove phones to address book
phones_for_remove = [
'11111111111',
'22222222222'
]
SPApiProxy.sms_delete_phones(ADDRESSBOOK_ID, phones_for_remove)
# Get phones from the blacklist
SPApiProxy.sms_get_blacklist()
# Add phones to blacklist
phones_for_add_to_blacklist = [
'111222227',
'222333337'
]
SPApiProxy.sms_add_phones_to_blacklist(phones_for_add_to_blacklist, 'test')
# Remove phones from blacklist
phones_for_remove = [
'11111111111',
'22222222222'
]
SPApiProxy.sms_delete_phones_from_blacklist(phones_for_remove)
# Get info by phones from the blacklist
phones = [
'11111111111',
'22222222222'
]
SPApiProxy.sms_get_phones_info_from_blacklist(phones)
# Create new sms campaign by addressbook_id
SPApiProxy.sms_add_campaign_by_addressbook_id(SENDER_NAME, ADDRESSBOOK_ID, 'test')
# Create new sms campaign by some phones
phones_for_send = [
'11111111111'
]
SPApiProxy.sms_add_campaign_by_phones(SENDER_NAME, phones_for_send, 'test')
# Get list of sms campaigns
date_from = '2018-04-10 23:00:00'
date_to = '2018-05-10 23:00:00'
SPApiProxy.sms_get_list_campaigns(date_from, date_to)
# Get information about sms campaign
SPApiProxy.sms_get_campaign_info(CAMPAIGN_ID)
# Cancel sms campaign
SPApiProxy.sms_cancel_campaign(CAMPAIGN_ID)
# Get cost sms campaign
SPApiProxy.sms_get_campaign_cost('sender', 'test', ADDRESSBOOK_ID)
# SPApiProxy.sms_get_campaign_cost('sender', 'test', None, ['111111111'])
# Remove sms campaign
SPApiProxy.sms_delete_campaign(CAMPAIGN_ID)
# **************** EVENT ***************
# Start event
params = {
"email": "[email protected]",
"phone": "+123456789",
"var_1": "var_1_value"
}
SPApiProxy.send_event('registration', params);