add sms service

This commit is contained in:
Asim Aslam
2021-06-30 11:54:59 +01:00
parent 221ff019af
commit 3a6f7a9ee2
12 changed files with 522 additions and 0 deletions

2
sms/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
sms

3
sms/Dockerfile Normal file
View File

@@ -0,0 +1,3 @@
FROM alpine
ADD sms /sms
ENTRYPOINT [ "/sms" ]

28
sms/Makefile Normal file
View File

@@ -0,0 +1,28 @@
GOPATH:=$(shell go env GOPATH)
.PHONY: init
init:
go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro
go get github.com/micro/micro/v3/cmd/protoc-gen-openapi
.PHONY: api
api:
protoc --openapi_out=. --proto_path=. proto/sms.proto
.PHONY: proto
proto:
protoc --proto_path=. --micro_out=. --go_out=:. proto/sms.proto
.PHONY: build
build:
go build -o sms *.go
.PHONY: test
test:
go test -v ./... -cover
.PHONY: docker
docker:
docker build . -t sms:latest

23
sms/README.md Normal file
View File

@@ -0,0 +1,23 @@
# Sms Service
This is the Sms service
Generated with
```
micro new sms
```
## Usage
Generate the proto code
```
make proto
```
Run the service
```
micro run .
```

3
sms/generate.go Normal file
View File

@@ -0,0 +1,3 @@
package main
//go:generate make proto

67
sms/handler/sms.go Normal file
View File

@@ -0,0 +1,67 @@
package handler
import (
"context"
"net/url"
"github.com/kevinburke/twilio-go"
"github.com/micro/micro/v3/service/config"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger"
pb "github.com/micro/services/sms/proto"
)
type Sms struct{}
func (e *Sms) Send(ctx context.Context, req *pb.SendRequest, rsp *pb.SendResponse) error {
if len(req.From) == 0 {
return errors.BadRequest("sms.send", "require from field")
}
if len(req.To) == 0 {
return errors.BadRequest("sms.send", "require to field")
}
if len(req.Message) == 0 {
return errors.BadRequest("sms.send", "message is blank")
}
v, err := config.Get("twilio.sid")
if err != nil {
logger.Error("Failed to get twilio.sid config")
return errors.InternalServerError("sms.send", "failed to send message")
}
sid := v.String("")
v, err = config.Get("twilio.token")
if err != nil {
logger.Error("Failed to get twilio.token config")
return errors.InternalServerError("sms.send", "failed to send message")
}
token := v.String("")
v, err = config.Get("twilio.number")
if err != nil {
logger.Error("Failed to get twilio.number config")
return errors.InternalServerError("sms.send", "failed to send message")
}
number := v.String("")
message := req.Message + " Sent from " + req.From
vals := url.Values{}
vals.Set("Body", message)
vals.Set("From", number)
vals.Set("To", req.To)
// non configurable and must match publicapi.json
vals.Set("MaxPrice", "0.01")
client := twilio.NewClient(sid, token, nil)
_, 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())
}
rsp.Status = "ok"
return nil
}

25
sms/main.go Normal file
View File

@@ -0,0 +1,25 @@
package main
import (
"github.com/micro/services/sms/handler"
pb "github.com/micro/services/sms/proto"
"github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/logger"
)
func main() {
// Create service
srv := service.New(
service.Name("sms"),
service.Version("latest"),
)
// Register handler
pb.RegisterSmsHandler(srv.Server(), new(handler.Sms))
// Run service
if err := srv.Run(); err != nil {
logger.Fatal(err)
}
}

1
sms/micro.mu Normal file
View File

@@ -0,0 +1 @@
service sms

243
sms/proto/sms.pb.go Normal file
View File

@@ -0,0 +1,243 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.26.0
// protoc v3.15.6
// source: proto/sms.proto
package sms
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
// Send an SMS. Include international dialing code in the number
type SendRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// who is the message from?
From string `protobuf:"bytes,1,opt,name=from,proto3" json:"from,omitempty"`
// number of the person it's to
To string `protobuf:"bytes,2,opt,name=to,proto3" json:"to,omitempty"`
// the message to send
Message string `protobuf:"bytes,3,opt,name=message,proto3" json:"message,omitempty"`
}
func (x *SendRequest) Reset() {
*x = SendRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_sms_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *SendRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*SendRequest) ProtoMessage() {}
func (x *SendRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_sms_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use SendRequest.ProtoReflect.Descriptor instead.
func (*SendRequest) Descriptor() ([]byte, []int) {
return file_proto_sms_proto_rawDescGZIP(), []int{0}
}
func (x *SendRequest) GetFrom() string {
if x != nil {
return x.From
}
return ""
}
func (x *SendRequest) GetTo() string {
if x != nil {
return x.To
}
return ""
}
func (x *SendRequest) GetMessage() string {
if x != nil {
return x.Message
}
return ""
}
type SendResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// will return "ok" if sent and "failed" if there was a problem
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"`
}
func (x *SendResponse) Reset() {
*x = SendResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_sms_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *SendResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*SendResponse) ProtoMessage() {}
func (x *SendResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_sms_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use SendResponse.ProtoReflect.Descriptor instead.
func (*SendResponse) Descriptor() ([]byte, []int) {
return file_proto_sms_proto_rawDescGZIP(), []int{1}
}
func (x *SendResponse) GetStatus() string {
if x != nil {
return x.Status
}
return ""
}
func (x *SendResponse) GetInfo() string {
if x != nil {
return x.Info
}
return ""
}
var File_proto_sms_proto protoreflect.FileDescriptor
var file_proto_sms_proto_rawDesc = []byte{
0x0a, 0x0f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x73, 0x6d, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x12, 0x03, 0x73, 0x6d, 0x73, 0x22, 0x4b, 0x0a, 0x0b, 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65,
0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x18, 0x01, 0x20,
0x01, 0x28, 0x09, 0x52, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x74, 0x6f, 0x18,
0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x74, 0x6f, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73,
0x73, 0x61, 0x67, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73,
0x61, 0x67, 0x65, 0x22, 0x3a, 0x0a, 0x0c, 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f,
0x6e, 0x73, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x18, 0x01, 0x20,
0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x69,
0x6e, 0x66, 0x6f, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x69, 0x6e, 0x66, 0x6f, 0x32,
0x34, 0x0a, 0x03, 0x53, 0x6d, 0x73, 0x12, 0x2d, 0x0a, 0x04, 0x53, 0x65, 0x6e, 0x64, 0x12, 0x10,
0x2e, 0x73, 0x6d, 0x73, 0x2e, 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
0x1a, 0x11, 0x2e, 0x73, 0x6d, 0x73, 0x2e, 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f,
0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x0d, 0x5a, 0x0b, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x3b, 0x73, 0x6d, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_proto_sms_proto_rawDescOnce sync.Once
file_proto_sms_proto_rawDescData = file_proto_sms_proto_rawDesc
)
func file_proto_sms_proto_rawDescGZIP() []byte {
file_proto_sms_proto_rawDescOnce.Do(func() {
file_proto_sms_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_sms_proto_rawDescData)
})
return file_proto_sms_proto_rawDescData
}
var file_proto_sms_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_proto_sms_proto_goTypes = []interface{}{
(*SendRequest)(nil), // 0: sms.SendRequest
(*SendResponse)(nil), // 1: sms.SendResponse
}
var file_proto_sms_proto_depIdxs = []int32{
0, // 0: sms.Sms.Send:input_type -> sms.SendRequest
1, // 1: sms.Sms.Send:output_type -> sms.SendResponse
1, // [1:2] is the sub-list for method output_type
0, // [0:1] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_proto_sms_proto_init() }
func file_proto_sms_proto_init() {
if File_proto_sms_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_proto_sms_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SendRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_sms_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*SendResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_proto_sms_proto_rawDesc,
NumEnums: 0,
NumMessages: 2,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_proto_sms_proto_goTypes,
DependencyIndexes: file_proto_sms_proto_depIdxs,
MessageInfos: file_proto_sms_proto_msgTypes,
}.Build()
File_proto_sms_proto = out.File
file_proto_sms_proto_rawDesc = nil
file_proto_sms_proto_goTypes = nil
file_proto_sms_proto_depIdxs = nil
}

93
sms/proto/sms.pb.micro.go Normal file
View File

@@ -0,0 +1,93 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: proto/sms.proto
package sms
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
import (
context "context"
api "github.com/micro/micro/v3/service/api"
client "github.com/micro/micro/v3/service/client"
server "github.com/micro/micro/v3/service/server"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
// Reference imports to suppress errors if they are not otherwise used.
var _ api.Endpoint
var _ context.Context
var _ client.Option
var _ server.Option
// Api Endpoints for Sms service
func NewSmsEndpoints() []*api.Endpoint {
return []*api.Endpoint{}
}
// Client API for Sms service
type SmsService interface {
Send(ctx context.Context, in *SendRequest, opts ...client.CallOption) (*SendResponse, error)
}
type smsService struct {
c client.Client
name string
}
func NewSmsService(name string, c client.Client) SmsService {
return &smsService{
c: c,
name: name,
}
}
func (c *smsService) Send(ctx context.Context, in *SendRequest, opts ...client.CallOption) (*SendResponse, error) {
req := c.c.NewRequest(c.name, "Sms.Send", in)
out := new(SendResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Sms service
type SmsHandler interface {
Send(context.Context, *SendRequest, *SendResponse) error
}
func RegisterSmsHandler(s server.Server, hdlr SmsHandler, opts ...server.HandlerOption) error {
type sms interface {
Send(ctx context.Context, in *SendRequest, out *SendResponse) error
}
type Sms struct {
sms
}
h := &smsHandler{hdlr}
return s.Handle(s.NewHandler(&Sms{h}, opts...))
}
type smsHandler struct {
SmsHandler
}
func (h *smsHandler) Send(ctx context.Context, in *SendRequest, out *SendResponse) error {
return h.SmsHandler.Send(ctx, in, out)
}

26
sms/proto/sms.proto Normal file
View File

@@ -0,0 +1,26 @@
syntax = "proto3";
package sms;
option go_package = "./proto;sms";
service Sms {
rpc Send(SendRequest) returns (SendResponse) {}
}
// Send an SMS. Include international dialing code in the number
message SendRequest {
// who is the message from?
string from = 1;
// number of the person it's to
string to = 2;
// the message to send
string message = 3;
}
message SendResponse {
// will return "ok" if sent and "failed" if there was a problem
string status = 1;
// any additional info
string info = 2;
}

8
sms/publicapi.json Normal file
View File

@@ -0,0 +1,8 @@
{
"name": "sms",
"icon": "📟",
"category": "communication",
"pricing": {
"Sms.Send": 10000
}
}