/
ActionTriggers_Example.py
312 lines (277 loc) · 9.31 KB
/
ActionTriggers_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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import cgrateshttpapi
import pprint
import sys
import json
global CGRateS_Obj
CGRateS_Obj = cgrateshttpapi.CGRateS('localhost', 2080)
Account = "Nick_Test_123"
# Add a Signup Bonus of $99 to the account with type *monetary expiring a month after it's added
Action_Signup_Bonus = {
"id": "0",
"method": "ApierV1.SetActions",
"params": [
{
"ActionsId": "Action_Add_Signup_Bonus",
"Actions": [
{
"Identifier": "*topup_reset",
"BalanceId": "Balance_Signup_Bonus",
"BalanceType": "*monetary",
"Units": 99,
"ExpiryTime": "*month",
"BalanceWeight": 1200,
"Weight": 90
},
{
"Identifier": "*cdrlog",
"BalanceId": "",
"BalanceUuid": "",
"BalanceType": "*monetary",
"Directions": "*out",
"Units": 0,
"ExpiryTime": "",
"Filter": "",
"TimingTags": "",
"DestinationIds": "",
"RatingSubject": "",
"Categories": "",
"SharedGroups": "",
"BalanceWeight": 0,
"ExtraParameters": "{\"Category\":\"^activation\",\"Destination\":\"Your sign up Bonus\"}",
"BalanceBlocker": "false",
"BalanceDisabled": "false",
"Weight": 80
},
{
"Identifier": "*http_post_async",
"ExtraParameters": "http://10.177.2.135/signup_bonus",
"ExpiryTime": "*unlimited",
"Weight": 70
},
{
"Identifier": "*log",
"Weight": 60
}
]}]}
pprint.pprint(CGRateS_Obj.SendData(Action_Signup_Bonus))
# Create ActionPlan using SetActionPlan to trigger the Action_Signup_Bonus ASAP
SetActionPlan_Signup_Bonus_JSON = {
"method": "ApierV1.SetActionPlan",
"params": [{
"Id": "ActionPlan_Signup_Bonus",
"ActionPlan": [{
"ActionsId": "Action_Add_Signup_Bonus",
"Years": "*any",
"Months": "*any",
"MonthDays": "*any",
"WeekDays": "*any",
"Time": "*asap",
"Weight": 10
}],
"Overwrite": True,
"ReloadScheduler": True
}]
}
pprint.pprint(CGRateS_Obj.SendData(SetActionPlan_Signup_Bonus_JSON))
# View the Actions we've got defined
Get_Actions_JSON = {
"method": "APIerSv2.GetActions",
"params": [
{
"Tenant": "cgrates.com"
}
],
}
pprint.pprint(CGRateS_Obj.SendData(Get_Actions_JSON))
# Create the Account object inside CGrateS
Create_Account_JSON = {
"method": "ApierV2.SetAccount",
"params": [
{
"Tenant": "cgrates.org",
"Account": str(Account),
"ActionPlanIds": ["ActionPlan_Signup_Bonus"],
"ActionPlansOverwrite": True,
"ReloadScheduler":True
}
]
}
print(CGRateS_Obj.SendData(Create_Account_JSON))
import time
#Give it a second to apply the ActionPlan
time.sleep(1)
# Get Account Info
pprint.pprint(CGRateS_Obj.SendData({'method': 'ApierV2.GetAccount', 'params': [
{"Tenant": "cgrates.org", "Account": str(Account)}]}))
# Action to add a Monthly charge of $6
Action_Monthly_Charge = {
"id": "0",
"method": "ApierV1.SetActions",
"params": [
{
"ActionsId": "Action_Monthly_Charge",
"Actions": [
{
'Identifier': '*debit',
'BalanceType': '*monetary',
'Units': 6,
'Id': 'Action_Monthly_Charge_Debit',
'Weight': 70},
{
"Identifier": "*log",
"Weight": 60,
'Id' : "Action_Monthly_Charge_Log"
},
{
"Identifier": "*cdrlog",
"BalanceId": "",
"BalanceUuid": "",
"BalanceType": "*monetary",
"Directions": "*out",
"Units": 0,
"ExpiryTime": "",
"Filter": "",
"TimingTags": "",
"DestinationIds": "",
"RatingSubject": "",
"Categories": "",
"SharedGroups": "",
"BalanceWeight": 0,
"ExtraParameters": "{\"Category\":\"^activation\",\"Destination\":\"Recurring Charge\"}",
"BalanceBlocker": "false",
"BalanceDisabled": "false",
"Weight": 80
},
]}]}
pprint.pprint(CGRateS_Obj.SendData(Action_Monthly_Charge))
# View the Actions we've got defined
Get_Actions_JSON = {
"method": "APIerSv2.GetActions",
"params": [
{
"Tenant": "cgrates.com",
"Id" : "Action_Monthly_Charge"
}
],
}
pprint.pprint(CGRateS_Obj.SendData(Get_Actions_JSON))
# # Create ActionPlan using SetActionPlan to trigger the Action_Monthly_Charge
SetActionPlan_Daily_Action_Monthly_Charge_JSON = {
"method": "ApierV1.SetActionPlan",
"params": [{
"Id": "ActionPlan_Monthly_Charge",
"ActionPlan": [{
"ActionsId": "Action_Monthly_Charge",
"Years": "*any",
"Months": "*any",
"MonthDays": "*any",
"WeekDays": "*any",
"Time": "*every_minute",
"Weight": 10
}],
"Overwrite": True,
"ReloadScheduler": True
}]
}
pprint.pprint(CGRateS_Obj.SendData(
SetActionPlan_Daily_Action_Monthly_Charge_JSON))
# Get the GetActionPlanIDs
Action_Plan_IDs = CGRateS_Obj.SendData(
{'method': 'ApierV2.GetActionPlanIDs', 'params': []})['result']
for result in Action_Plan_IDs:
print("Getting info for ActionPlan: " + str(result))
pprint.pprint(CGRateS_Obj.SendData(
{'method': 'ApierV2.GetActionPlan', 'params': [{"ID": str(result)}]})['result'])
# Create the Account object inside CGrateS & assign ActionPlan_Signup_Bonus and ActionPlan_Monthly_Charge
Create_Account_JSON = {
"method": "ApierV2.SetAccount",
"params": [
{
"Tenant": "cgrates.org",
"Account": str(Account),
"ActionPlanIds": ["ActionPlan_Signup_Bonus", "ActionPlan_Monthly_Charge"],
"ActionPlansOverwrite": True,
"ReloadScheduler":True
}
]
}
print(CGRateS_Obj.SendData(Create_Account_JSON))
# Get the GetActionPlanIDs
Action_Plan_IDs = CGRateS_Obj.SendData(
{'method': 'ApierV2.GetActionPlanIDs', 'params': []})['result']
for result in Action_Plan_IDs:
print("Getting info for ActionPlan: " + str(result))
pprint.pprint(CGRateS_Obj.SendData(
{'method': 'ApierV2.GetActionPlan', 'params': [{"ID": str(result)}]})['result'])
# Get Account Info
pprint.pprint(CGRateS_Obj.SendData({'method': 'ApierV2.GetAccount', 'params': [
{"Tenant": "cgrates.org", "Account": str(Account)}]}))
#Define a new Action to send an HTTP POST
Action_HTTP_Notify_95 = {
"id": "0",
"method": "ApierV1.SetActions",
"params": [
{
"ActionsId": "Action_HTTP_Notify_95",
"Actions": [
{
"Identifier": "*http_post_async",
"ExtraParameters": "http://10.177.2.135/95_remaining",
"ExpiryTime": "*unlimited",
"Weight": 700
},
{
"Identifier": "*log",
"Weight": 1200
}
]}]}
pprint.pprint(CGRateS_Obj.SendData(Action_HTTP_Notify_95))
#Define ActionTrigger
ActionTrigger_95_Remaining_JSON = {
"method": "APIerSv1.SetActionTrigger",
"params": [
{
"GroupID" : "ActionTrigger_95_Remaining",
"ActionTrigger":
{
"BalanceType": "*monetary",
"Balance" : {
'BalanceType': '*monetary',
'ID' : "*default",
'BalanceID' : "*default",
'Value' : 95,
},
"ThresholdType": "*min_balance",
"ThresholdValue": 95,
"Weight": 10,
"ActionsID" : "Action_HTTP_Notify_95",
},
"Overwrite": True
}
]
}
pprint.pprint(CGRateS_Obj.SendData(ActionTrigger_95_Remaining_JSON))
#Get ActionTriggers
Get_Action_Triggers_JSON = {
"method": "APIerSv1.GetActionTriggers",
"params": [
{"GroupID" : "ActionTrigger_95_Remaining"}
]
}
pprint.pprint(CGRateS_Obj.SendData(Get_Action_Triggers_JSON))
#Add ActionTrigger to Account
Add_ActionTrigger_to_Account_JSON = {
"method": "APIerSv1.AddAccountActionTriggers",
"params": [
{
"Tenant": "cgrates.org",
"Account": str(Account),
"ActionTriggerIDs": ["ActionTrigger_95_Remaining"],
"ActionTriggersOverwrite": True
}
]
}
pprint.pprint(CGRateS_Obj.SendData(Add_ActionTrigger_to_Account_JSON))
# Get Account Info
pprint.pprint(CGRateS_Obj.SendData({'method': 'ApierV2.GetAccount', 'params': [
{"Tenant": "cgrates.org", "Account": str(Account)}]}))