diff --git a/go.mod b/go.mod index 57fdd71..f1537b5 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/Masterminds/semver/v3 v3.1.1 github.com/PuerkitoBio/goquery v1.6.1 github.com/SlyMarbo/rss v1.0.1 - github.com/ajanicij/goduckgo v0.0.0-20150205231829-61f9a275272f // indirect github.com/cdipaolo/goml v0.0.0-20190412180403-e1f51f713598 // indirect github.com/cdipaolo/sentiment v0.0.0-20200617002423-c697f64e7f10 github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect diff --git a/go.sum b/go.sum index 8b428a4..95a996d 100644 --- a/go.sum +++ b/go.sum @@ -37,8 +37,6 @@ github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWX github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= github.com/SlyMarbo/rss v1.0.1 h1:fiaIU5UhcXauVOniHOIocWG7uj8Ej6pHNarMGPJilzA= github.com/SlyMarbo/rss v1.0.1/go.mod h1:JNF+T33oj4m5WLCQXpBTCgO+SxRbYVgdiiimHNgzcbA= -github.com/ajanicij/goduckgo v0.0.0-20150205231829-61f9a275272f h1:FkZIeRvGe4AcF/K4UKsuvyTvRYhMCGwjpoIByl6t61I= -github.com/ajanicij/goduckgo v0.0.0-20150205231829-61f9a275272f/go.mod h1:KeRS5OFtXgX0i1YVwrl00Rm70FdNSJlzpvj3p+1qjx8= github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= github.com/akamai/AkamaiOPEN-edgegrid-golang v0.9.0/go.mod h1:zpDJeKyp9ScW4NNrbdr+Eyxvry3ilGPewKoXw3XGN1k= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= diff --git a/sms/handler/sms.go b/sms/handler/sms.go index 0de687a..3770b9f 100644 --- a/sms/handler/sms.go +++ b/sms/handler/sms.go @@ -2,18 +2,33 @@ package handler import ( "context" + "encoding/json" + "fmt" "net/url" + "regexp" "strings" + "time" "github.com/kevinburke/twilio-go" "github.com/micro/micro/v3/service/auth" "github.com/micro/micro/v3/service/config" "github.com/micro/micro/v3/service/errors" "github.com/micro/micro/v3/service/logger" + "github.com/micro/micro/v3/service/store" "github.com/micro/services/pkg/tenant" pb "github.com/micro/services/sms/proto" ) +const ( + prefixUserID = "byUserID" + prefixTwilioID = "byTwilioID" +) + +type Sent struct { + UserID string + TwilioMsgID string +} + type Sms struct{} func (e *Sms) Send(ctx context.Context, req *pb.SendRequest, rsp *pb.SendResponse) error { @@ -23,15 +38,19 @@ func (e *Sms) Send(ctx context.Context, req *pb.SendRequest, rsp *pb.SendRespons if len(req.To) == 0 { return errors.BadRequest("sms.send", "require to field") } + reg, _ := regexp.Compile("^\\+[0-9]+$") + if !reg.MatchString(req.To) { + return errors.BadRequest("sms.send", "invalid to field format") + } if len(req.Message) == 0 { return errors.BadRequest("sms.send", "message is blank") } + tnt, _ := tenant.FromContext(ctx) // crudely ban any sender in the banned list aka no impersonating frm := strings.ToLower(req.From) for _, sender := range BanFrom { if strings.Contains(frm, strings.ToLower(sender)) { - tnt, _ := tenant.FromContext(ctx) acc, _ := auth.AccountFromContext(ctx) logger.Error("Request to send from %v blocked by account: %v tenant: %v", req.From, acc, tnt) @@ -74,12 +93,32 @@ func (e *Sms) Send(ctx context.Context, req *pb.SendRequest, rsp *pb.SendRespons vals.Set("MaxPrice", "0.01") client := twilio.NewClient(sid, token, nil) - _, err = client.Messages.Create(ctx, vals) + twMsg, err := client.Messages.Create(ctx, vals) if err != nil { logger.Errorf("Failed to send message: %v", err) return errors.InternalServerError("sms.send", "failed to send message: %v", err.Error()) } + sent := Sent{ + UserID: tnt, + TwilioMsgID: twMsg.Sid, + } + + b, _ := json.Marshal(&sent) + // log the association so we can correlate who sent what + if err := store.Write(&store.Record{ + Key: fmt.Sprintf("%s/%s/%s/%s", prefixUserID, sent.UserID, time.Now().Format("20060102"), sent.TwilioMsgID), + Value: b, + }); err != nil { + logger.Errorf("Failed to store association %+v %s", sent, err) + } + if err := store.Write(&store.Record{ + Key: fmt.Sprintf("%s/%s", prefixTwilioID, sent.TwilioMsgID), + Value: b, + }); err != nil { + logger.Errorf("Failed to store association %+v %s", sent, err) + } + rsp.Status = "ok" return nil diff --git a/sms/proto/sms.pb.go b/sms/proto/sms.pb.go index 2cbf7bd..d11923d 100644 --- a/sms/proto/sms.pb.go +++ b/sms/proto/sms.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.26.0 -// protoc v3.15.6 +// protoc v3.15.5 // source: proto/sms.proto package sms @@ -20,17 +20,17 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) -// Send an SMS. Include international dialing code in the number +// Send an SMS. type SendRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - // who is the message from? + // who is the message from? The message will be suffixed with "Sent from " From string `protobuf:"bytes,1,opt,name=from,proto3" json:"from,omitempty"` - // number of the person it's to + // the destination phone number including the international dialling code (e.g. +44) To string `protobuf:"bytes,2,opt,name=to,proto3" json:"to,omitempty"` - // the message to send + // the main body of the message to send Message string `protobuf:"bytes,3,opt,name=message,proto3" json:"message,omitempty"` } @@ -92,7 +92,7 @@ type SendResponse struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - // will return "ok" if sent and "failed" if there was a problem + // will return "ok" if successful Status string `protobuf:"bytes,1,opt,name=status,proto3" json:"status,omitempty"` // any additional info Info string `protobuf:"bytes,2,opt,name=info,proto3" json:"info,omitempty"` diff --git a/sms/proto/sms.proto b/sms/proto/sms.proto index 8020cbf..01535d3 100644 --- a/sms/proto/sms.proto +++ b/sms/proto/sms.proto @@ -8,18 +8,18 @@ service Sms { rpc Send(SendRequest) returns (SendResponse) {} } -// Send an SMS. Include international dialing code in the number +// Send an SMS. message SendRequest { - // who is the message from? + // who is the message from? The message will be suffixed with "Sent from " string from = 1; - // number of the person it's to + // the destination phone number including the international dialling code (e.g. +44) string to = 2; - // the message to send + // the main body of the message to send string message = 3; } message SendResponse { - // will return "ok" if sent + // will return "ok" if successful string status = 1; // any additional info string info = 2;