-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity.go
208 lines (176 loc) · 7.01 KB
/
activity.go
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
package twiliosms
import (
"strings"
"github.com/DipeshTest/allstarsshared/twilio"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/logger"
)
// MyActivity is a stub for your Activity implementation
type MyActivity struct {
metadata *activity.Metadata
}
var log = logger.GetLogger("activity-twiliosms")
// NewActivity creates a new activity
func NewActivity(metadata *activity.Metadata) activity.Activity {
return &MyActivity{metadata: metadata}
}
// Metadata implements activity.Activity.Metadata
func (a *MyActivity) Metadata() *activity.Metadata {
return a.metadata
}
const (
SENDSMS = "Send SMS"
RETRIVE_LIST = "Retrive Recipients"
)
// Eval implements activity.Activity.Eval
func (a *MyActivity) Eval(context activity.Context) (done bool, err error) {
accountSid := context.GetInput("accountSid").(string)
authToken := context.GetInput("authToken").(string)
urlStr := context.GetInput("urlString").(string)
method := context.GetInput("method").(string)
smsText := context.GetInput("messageBody").(string)
to := context.GetInput("to").(string)
from := context.GetInput("from").(string)
nums := strings.Split(to, ",")
if len(strings.TrimSpace(urlStr)) == 0 {
// fmt.Println(twilio.ResponseData{501, "Failed", "Send input numbers back", "SMS body is blank"})
context.SetOutput("statusCode", "504")
context.SetOutput("status", "Failed")
context.SetOutput("message", "Url String field is blank")
//context.SetOutput("failedNumbers", to)
//respond with this
} else if len(strings.TrimSpace(accountSid)) == 0 {
// fmt.Println(twilio.ResponseData{501, "Failed", "Send input numbers back", "SMS body is blank"})
context.SetOutput("statusCode", "505")
context.SetOutput("status", "Failed")
context.SetOutput("message", "Account SID field is blank")
//context.SetOutput("failedNumbers", to)
//respond with this
} else if len(strings.TrimSpace(authToken)) == 0 {
// fmt.Println(twilio.ResponseData{501, "Failed", "Send input numbers back", "SMS body is blank"})
context.SetOutput("statusCode", "506")
context.SetOutput("status", "Failed")
context.SetOutput("message", "Auth Token String field is blank")
//context.SetOutput("failedNumbers", to)
//respond with this
} else {
switch method {
case SENDSMS:
{
if len(strings.TrimSpace(smsText)) == 0 {
// fmt.Println(twilio.ResponseData{501, "Failed", "Send input numbers back", "SMS body is blank"})
context.SetOutput("statusCode", "501")
context.SetOutput("status", "Failed")
context.SetOutput("message", "SMS body is blank")
context.SetOutput("failedNumbers", to)
//respond with this
} else if len(strings.TrimSpace(to)) == 0 {
// fmt.Println(twilio.ResponseData{501, "Failed", "Send input numbers back", "SMS body is blank"})
context.SetOutput("statusCode", "502")
context.SetOutput("status", "Failed")
context.SetOutput("message", "To field is blank")
//context.SetOutput("failedNumbers", to)
//respond with this
} else if len(strings.TrimSpace(from)) == 0 {
// fmt.Println(twilio.ResponseData{501, "Failed", "Send input numbers back", "SMS body is blank"})
context.SetOutput("statusCode", "503")
context.SetOutput("status", "Failed")
context.SetOutput("message", "From field is blank")
//context.SetOutput("failedNumbers", to)
//respond with this
} else {
log.Debugf("The Flogo engine says [%s] to [%s]", accountSid+authToken+urlStr, smsText+to+from)
jobs := make(chan string)
results := make(chan twilio.ResponseData)
for i, _ := range nums {
twilioData := twilio.Twilio{accountSid, authToken, urlStr + accountSid + "/Messages.json", smsText, nums[i], from}
go twilio.SendSMS(twilioData, jobs, results)
//fmt.Println("Creating go routinge number", nums[i])
}
for j, _ := range nums {
jobs <- nums[j]
//fmt.Println("Sending number to routine", nums[j])
}
close(jobs)
respMap := make(map[int]string)
endProc := false
failedNumbers := ""
faildNumMessage := ""
for k, _ := range nums {
respdata := <-results
k = k + 1
if respdata.StatusCode == 404 || respdata.StatusCode == 401 || respdata.StatusCode == 901 {
// here we need to create a custom response and send out error code, use return statement
respMap[respdata.StatusCode] = respdata.Num + ":" + respdata.Message
//fmt.Println("Error from service" + respMap[respdata.StatusCode])
context.SetOutput("statusCode", "203")
context.SetOutput("status", "Failed")
if respdata.StatusCode == 901 {
context.SetOutput("message", respdata.ErrorData)
} else {
context.SetOutput("message", respdata.Message)
}
context.SetOutput("message", respdata.Message)
context.SetOutput("failedNumbers", to)
//return 3 param resp
endProc = true
break
} else {
respMap[respdata.StatusCode] = respdata.Num + ":" + respdata.Message
if !(respdata.StatusCode >= 200 && respdata.StatusCode < 300) {
failedNumbers = failedNumbers + respdata.Num + ":"
faildNumMessage = faildNumMessage + respdata.Message + ":\n"
}
// / fmt.Println("Response from service" + respMap[respdata.StatusCode])
}
// fmt.Println("Response from ", k, <-results)
}
//close(results)
respStat1001 := respMap[1001]
respStat400 := respMap[400]
respStat200 := respMap[200]
if !endProc {
if respStat200 == "" {
context.SetOutput("statusCode", "202")
context.SetOutput("status", "Failed")
context.SetOutput("message", faildNumMessage)
context.SetOutput("failedNumbers", failedNumbers)
} else if respStat1001 == "" && respStat400 == "" {
//fmt.Println(respMap[200])
context.SetOutput("statusCode", "200")
context.SetOutput("status", "Success")
context.SetOutput("message", "SMS sent successfully to :"+to)
// context.SetOutput("failedNumbers", to)
} else {
context.SetOutput("statusCode", "201")
context.SetOutput("status", "Partial-Success")
context.SetOutput("message", faildNumMessage)
context.SetOutput("failedNumbers", failedNumbers)
}
} else {
}
log.Debugf("The Flogo engine says [%s] to [%s]", context.GetOutput("statusCode"), err)
}
}
case RETRIVE_LIST:
{
twilioData := twilio.Twilio{accountSid, authToken, urlStr + accountSid + "/OutgoingCallerIds.json", "", "", ""}
respdata := twilio.RetrieveRecipientList(twilioData)
if !(respdata.StatusCode >= 200 && respdata.StatusCode < 300) {
context.SetOutput("statusCode", "203")
context.SetOutput("status", "Failed")
if respdata.StatusCode == 901 {
context.SetOutput("message", respdata.ErrorData)
} else {
context.SetOutput("message", respdata.Message)
}
} else {
context.SetOutput("statusCode", "200")
context.SetOutput("status", "Success")
context.SetOutput("message", respdata.Num)
}
}
}
}
return true, err
}