/
smsconnector.go
executable file
·109 lines (99 loc) · 4.53 KB
/
smsconnector.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
package channels
import (
"github.com/alknopfler/notificationapp/db"
"github.com/alknopfler/notificationapp/logger"
"regexp"
"github.com/alknopfler/notificationapp/config"
"errors"
"github.com/alknopfler/notificationapp/templates"
"github.com/aws/aws-sdk-go/aws"
"time"
"strconv"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
)
//EventForSMS : SMS implementation for SMS
type EventForSMS struct {
TriggeredEvent db.Event
}
//ParseTemplate : Parsing Template for SMS
func (event EventForSMS) ParseTemplate() ([]db.Message, error) {
var messages []db.Message
channelSupported := CheckChannel(event.TriggeredEvent, db.SMS)
if !channelSupported {
logger.Print("ERROR",config.SrvName,config.Channel,"1","417","Dropping event ['" + event.TriggeredEvent.AccountID+ "']. SMS channel not supported.")
return messages, errors.New("SMS channel not supported")
}
logger.Print("INFO",config.SrvName,config.Channel,"1","200","Channel supported...")
numOfRecipient := len(event.TriggeredEvent.Recipient)
if numOfRecipient <= 0 {
logger.Print("ERROR",config.SrvName,config.Channel,"1","405","Dropping event ['" + event.TriggeredEvent.AccountID+ "']. No recipient found.")
return messages, errors.New("no recipients found")
}
smsContent, err := templates.ParseTemplateForMessage(event.TriggeredEvent, db.SMS, event.TriggeredEvent.Lang)
if err!=nil{
logger.Print("ERROR",config.SrvName,config.Channel,"1","500","Error parsing the template . ",err)
return messages, errors.New("Error parsing the template")
}
//parse each sms separately because it may vary by recipient
for _, em := range event.TriggeredEvent.Recipient {
if validatePhone(em) {
logger.Print("INFO",config.SrvName,config.Channel,"1","200","SMS: "+ em+" format is OK...creating the message")
dateCreated := time.Now()
message := db.Message{}
message.Recipient = em
message.Subject = event.TriggeredEvent.Subject
message.Channel = "SMS"
message.DateCreated = dateCreated
message.TemplateId = event.TriggeredEvent.TemplateType +"-SMS-"+ event.TriggeredEvent.Lang
message.Content = smsContent
message.CustomData = event.TriggeredEvent.CustomData
message.MessageID = strconv.Itoa(dateCreated.Nanosecond()) + em + event.TriggeredEvent.AccountID
messages = append(messages, message)
logger.Print("INFO",config.SrvName,config.Channel,"1","200","Message created and added to the list of message to send to recipient")
}
}
return messages, nil
}
//SendMessage : Message Sending for SMS
func (event EventForSMS) SendMessage(message db.Message) db.MessageResponse {
sess,err := session.NewSession()
if err!= nil{
logger.Print("ERROR",config.SrvName,config.Channel,"1","500","Error creating the SNS session",err)
return db.MessageResponse{Status: config.FAILED, Response: err.Error(), TimeOfResponse: time.Now()}
}
logger.Print("INFO",config.SrvName,config.Channel,"1","200","SNS session created successfully")
svc := sns.New(sess)
logger.Print("INFO",config.SrvName,config.Channel,"1","200","SNS client created successfully")
if message.Content == "" || message.Recipient == "" {
logger.Print("ERROR",config.SrvName,config.Channel,"1","400","Sending Failed. Message body or recipient empty")
return db.MessageResponse{Status: config.FAILED, Response: "MESSAGE or RECIPIENT EMPTY. Message: "+message.Content+ " - Recipient: "+message.Recipient, TimeOfResponse: time.Now()}
}
logger.Print("INFO",config.SrvName,config.Channel,"1","200","SendMessage ready to delivery message")
smsResponse := db.MessageResponse{}
params := &sns.PublishInput{
Message: aws.String(message.Content),
PhoneNumber: aws.String(message.Recipient),
}
var sender = make(map[string]*string)
a := config.SENDER
sender["DefaultSenderID"]=&a
svc.SetSMSAttributes(&sns.SetSMSAttributesInput{Attributes: sender})
resp, err := svc.Publish(params)
if err != nil {
smsResponse.Response = err.Error()
smsResponse.Status = config.FAILED
smsResponse.TimeOfResponse = time.Now()
logger.Print("ERROR",config.SrvName,config.Channel,"1","500","Error sending SMS to " + message.Recipient + " with error: "+ err.Error(), err)
} else {
smsResponse.Response = "SENT: "+resp.String()
smsResponse.Status = config.SUCCESS
smsResponse.TimeOfResponse = time.Now()
logger.Print("INFO",config.SrvName,config.Channel,"1","200","SMS sent to ['" + message.Recipient + "'] with messageID: "+resp.String())
}
return smsResponse
}
func validatePhone(phone string) bool {
re := regexp.MustCompile(`^[\+|00]\d+$`) //regex to verify +3412345667 format
return re.MatchString(phone)
}