This commit is contained in:
Asim Aslam
2022-02-19 17:58:47 +00:00
parent 775d34ff88
commit ea95c4954d
3 changed files with 21 additions and 248 deletions

View File

@@ -2,13 +2,9 @@ package handler
import ( import (
"context" "context"
"net/url"
"github.com/enescakir/emoji" "github.com/enescakir/emoji"
"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/errors"
"github.com/micro/micro/v3/service/logger"
pb "github.com/micro/services/emoji/proto" pb "github.com/micro/services/emoji/proto"
) )
@@ -37,56 +33,3 @@ func (e *Emoji) Print(ctx context.Context, req *pb.PrintRequest, rsp *pb.PrintRe
return nil return nil
} }
func (e *Emoji) Send(ctx context.Context, req *pb.SendRequest, rsp *pb.SendResponse) error {
if len(req.From) == 0 {
return errors.BadRequest("emoji.send", "require from field")
}
if len(req.To) == 0 {
return errors.BadRequest("emoji.send", "require to field")
}
if len(req.Message) == 0 {
return errors.BadRequest("emoji.send", "message is blank")
}
v, err := config.Get("twilio.sid")
if err != nil {
logger.Error("Failed to get twilio.sid config")
return errors.InternalServerError("emoji.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("emoji.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("emoji.send", "failed to send message")
}
number := v.String("")
message := emoji.Parse(req.Message)
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("emoji.send", "failed to send message: %v", err.Error())
}
rsp.Success = true
return nil
}

View File

@@ -1,6 +1,6 @@
// Code generated by protoc-gen-go. DO NOT EDIT. // Code generated by protoc-gen-go. DO NOT EDIT.
// versions: // versions:
// protoc-gen-go v1.26.0 // protoc-gen-go v1.27.1
// protoc v3.15.6 // protoc v3.15.6
// source: proto/emoji.proto // source: proto/emoji.proto
@@ -312,121 +312,6 @@ func (x *PrintResponse) GetText() string {
return "" return ""
} }
// Send an emoji to anyone via SMS
type SendRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// who the message is from e.g Alice
From string `protobuf:"bytes,1,opt,name=from,proto3" json:"from,omitempty"`
// phone number to send to (including international dialing code)
To string `protobuf:"bytes,2,opt,name=to,proto3" json:"to,omitempty"`
// message to send including emoji aliases
Message string `protobuf:"bytes,3,opt,name=message,proto3" json:"message,omitempty"`
}
func (x *SendRequest) Reset() {
*x = SendRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_emoji_proto_msgTypes[6]
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_emoji_proto_msgTypes[6]
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_emoji_proto_rawDescGZIP(), []int{6}
}
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
// whether or not it succeeded
Success bool `protobuf:"varint,1,opt,name=success,proto3" json:"success,omitempty"`
}
func (x *SendResponse) Reset() {
*x = SendResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_emoji_proto_msgTypes[7]
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_emoji_proto_msgTypes[7]
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_emoji_proto_rawDescGZIP(), []int{7}
}
func (x *SendResponse) GetSuccess() bool {
if x != nil {
return x.Success
}
return false
}
var File_proto_emoji_proto protoreflect.FileDescriptor var File_proto_emoji_proto protoreflect.FileDescriptor
var file_proto_emoji_proto_rawDesc = []byte{ var file_proto_emoji_proto_rawDesc = []byte{
@@ -445,29 +330,19 @@ var file_proto_emoji_proto_rawDesc = []byte{
0x74, 0x65, 0x78, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x74, 0x65, 0x78, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74,
0x22, 0x23, 0x0a, 0x0d, 0x50, 0x72, 0x69, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x22, 0x23, 0x0a, 0x0d, 0x50, 0x72, 0x69, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
0x04, 0x74, 0x65, 0x78, 0x74, 0x22, 0x4b, 0x0a, 0x0b, 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x71, 0x04, 0x74, 0x65, 0x78, 0x74, 0x32, 0xa3, 0x01, 0x0a, 0x05, 0x45, 0x6d, 0x6f, 0x6a, 0x69, 0x12,
0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x31, 0x0a, 0x04, 0x46, 0x69, 0x6e, 0x64, 0x12, 0x12, 0x2e, 0x65, 0x6d, 0x6f, 0x6a, 0x69, 0x2e,
0x28, 0x09, 0x52, 0x04, 0x66, 0x72, 0x6f, 0x6d, 0x12, 0x0e, 0x0a, 0x02, 0x74, 0x6f, 0x18, 0x02, 0x46, 0x69, 0x6e, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x65, 0x6d,
0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x74, 0x6f, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x6f, 0x6a, 0x69, 0x2e, 0x46, 0x69, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65,
0x61, 0x67, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x22, 0x00, 0x12, 0x31, 0x0a, 0x04, 0x46, 0x6c, 0x61, 0x67, 0x12, 0x12, 0x2e, 0x65, 0x6d, 0x6f,
0x67, 0x65, 0x22, 0x28, 0x0a, 0x0c, 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x6a, 0x69, 0x2e, 0x46, 0x6c, 0x61, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13,
0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x18, 0x01, 0x20, 0x2e, 0x65, 0x6d, 0x6f, 0x6a, 0x69, 0x2e, 0x46, 0x6c, 0x61, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f,
0x01, 0x28, 0x08, 0x52, 0x07, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x32, 0xd6, 0x01, 0x0a, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x34, 0x0a, 0x05, 0x50, 0x72, 0x69, 0x6e, 0x74, 0x12, 0x13,
0x05, 0x45, 0x6d, 0x6f, 0x6a, 0x69, 0x12, 0x31, 0x0a, 0x04, 0x46, 0x69, 0x6e, 0x64, 0x12, 0x12, 0x2e, 0x65, 0x6d, 0x6f, 0x6a, 0x69, 0x2e, 0x50, 0x72, 0x69, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x75,
0x2e, 0x65, 0x6d, 0x6f, 0x6a, 0x69, 0x2e, 0x46, 0x69, 0x6e, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x65, 0x6d, 0x6f, 0x6a, 0x69, 0x2e, 0x50, 0x72, 0x69, 0x6e,
0x73, 0x74, 0x1a, 0x13, 0x2e, 0x65, 0x6d, 0x6f, 0x6a, 0x69, 0x2e, 0x46, 0x69, 0x6e, 0x64, 0x52, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x0f, 0x5a, 0x0d, 0x2e,
0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x31, 0x0a, 0x04, 0x46, 0x6c, 0x61, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x65, 0x6d, 0x6f, 0x6a, 0x69, 0x62, 0x06, 0x70, 0x72,
0x67, 0x12, 0x12, 0x2e, 0x65, 0x6d, 0x6f, 0x6a, 0x69, 0x2e, 0x46, 0x6c, 0x61, 0x67, 0x52, 0x65, 0x6f, 0x74, 0x6f, 0x33,
0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13, 0x2e, 0x65, 0x6d, 0x6f, 0x6a, 0x69, 0x2e, 0x46, 0x6c,
0x61, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x34, 0x0a, 0x05,
0x50, 0x72, 0x69, 0x6e, 0x74, 0x12, 0x13, 0x2e, 0x65, 0x6d, 0x6f, 0x6a, 0x69, 0x2e, 0x50, 0x72,
0x69, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x65, 0x6d, 0x6f,
0x6a, 0x69, 0x2e, 0x50, 0x72, 0x69, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65,
0x22, 0x00, 0x12, 0x31, 0x0a, 0x04, 0x53, 0x65, 0x6e, 0x64, 0x12, 0x12, 0x2e, 0x65, 0x6d, 0x6f,
0x6a, 0x69, 0x2e, 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x13,
0x2e, 0x65, 0x6d, 0x6f, 0x6a, 0x69, 0x2e, 0x53, 0x65, 0x6e, 0x64, 0x52, 0x65, 0x73, 0x70, 0x6f,
0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x0f, 0x5a, 0x0d, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f,
0x3b, 0x65, 0x6d, 0x6f, 0x6a, 0x69, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (
@@ -482,7 +357,7 @@ func file_proto_emoji_proto_rawDescGZIP() []byte {
return file_proto_emoji_proto_rawDescData return file_proto_emoji_proto_rawDescData
} }
var file_proto_emoji_proto_msgTypes = make([]protoimpl.MessageInfo, 8) var file_proto_emoji_proto_msgTypes = make([]protoimpl.MessageInfo, 6)
var file_proto_emoji_proto_goTypes = []interface{}{ var file_proto_emoji_proto_goTypes = []interface{}{
(*FindRequest)(nil), // 0: emoji.FindRequest (*FindRequest)(nil), // 0: emoji.FindRequest
(*FindResponse)(nil), // 1: emoji.FindResponse (*FindResponse)(nil), // 1: emoji.FindResponse
@@ -490,20 +365,16 @@ var file_proto_emoji_proto_goTypes = []interface{}{
(*FlagResponse)(nil), // 3: emoji.FlagResponse (*FlagResponse)(nil), // 3: emoji.FlagResponse
(*PrintRequest)(nil), // 4: emoji.PrintRequest (*PrintRequest)(nil), // 4: emoji.PrintRequest
(*PrintResponse)(nil), // 5: emoji.PrintResponse (*PrintResponse)(nil), // 5: emoji.PrintResponse
(*SendRequest)(nil), // 6: emoji.SendRequest
(*SendResponse)(nil), // 7: emoji.SendResponse
} }
var file_proto_emoji_proto_depIdxs = []int32{ var file_proto_emoji_proto_depIdxs = []int32{
0, // 0: emoji.Emoji.Find:input_type -> emoji.FindRequest 0, // 0: emoji.Emoji.Find:input_type -> emoji.FindRequest
2, // 1: emoji.Emoji.Flag:input_type -> emoji.FlagRequest 2, // 1: emoji.Emoji.Flag:input_type -> emoji.FlagRequest
4, // 2: emoji.Emoji.Print:input_type -> emoji.PrintRequest 4, // 2: emoji.Emoji.Print:input_type -> emoji.PrintRequest
6, // 3: emoji.Emoji.Send:input_type -> emoji.SendRequest 1, // 3: emoji.Emoji.Find:output_type -> emoji.FindResponse
1, // 4: emoji.Emoji.Find:output_type -> emoji.FindResponse 3, // 4: emoji.Emoji.Flag:output_type -> emoji.FlagResponse
3, // 5: emoji.Emoji.Flag:output_type -> emoji.FlagResponse 5, // 5: emoji.Emoji.Print:output_type -> emoji.PrintResponse
5, // 6: emoji.Emoji.Print:output_type -> emoji.PrintResponse 3, // [3:6] is the sub-list for method output_type
7, // 7: emoji.Emoji.Send:output_type -> emoji.SendResponse 0, // [0:3] is the sub-list for method input_type
4, // [4:8] is the sub-list for method output_type
0, // [0:4] 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 type_name
0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name 0, // [0:0] is the sub-list for field type_name
@@ -587,30 +458,6 @@ func file_proto_emoji_proto_init() {
return nil return nil
} }
} }
file_proto_emoji_proto_msgTypes[6].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_emoji_proto_msgTypes[7].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{} type x struct{}
out := protoimpl.TypeBuilder{ out := protoimpl.TypeBuilder{
@@ -618,7 +465,7 @@ func file_proto_emoji_proto_init() {
GoPackagePath: reflect.TypeOf(x{}).PkgPath(), GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_proto_emoji_proto_rawDesc, RawDescriptor: file_proto_emoji_proto_rawDesc,
NumEnums: 0, NumEnums: 0,
NumMessages: 8, NumMessages: 6,
NumExtensions: 0, NumExtensions: 0,
NumServices: 1, NumServices: 1,
}, },

View File

@@ -45,7 +45,6 @@ type EmojiService interface {
Find(ctx context.Context, in *FindRequest, opts ...client.CallOption) (*FindResponse, error) Find(ctx context.Context, in *FindRequest, opts ...client.CallOption) (*FindResponse, error)
Flag(ctx context.Context, in *FlagRequest, opts ...client.CallOption) (*FlagResponse, error) Flag(ctx context.Context, in *FlagRequest, opts ...client.CallOption) (*FlagResponse, error)
Print(ctx context.Context, in *PrintRequest, opts ...client.CallOption) (*PrintResponse, error) Print(ctx context.Context, in *PrintRequest, opts ...client.CallOption) (*PrintResponse, error)
Send(ctx context.Context, in *SendRequest, opts ...client.CallOption) (*SendResponse, error)
} }
type emojiService struct { type emojiService struct {
@@ -90,23 +89,12 @@ func (c *emojiService) Print(ctx context.Context, in *PrintRequest, opts ...clie
return out, nil return out, nil
} }
func (c *emojiService) Send(ctx context.Context, in *SendRequest, opts ...client.CallOption) (*SendResponse, error) {
req := c.c.NewRequest(c.name, "Emoji.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 Emoji service // Server API for Emoji service
type EmojiHandler interface { type EmojiHandler interface {
Find(context.Context, *FindRequest, *FindResponse) error Find(context.Context, *FindRequest, *FindResponse) error
Flag(context.Context, *FlagRequest, *FlagResponse) error Flag(context.Context, *FlagRequest, *FlagResponse) error
Print(context.Context, *PrintRequest, *PrintResponse) error Print(context.Context, *PrintRequest, *PrintResponse) error
Send(context.Context, *SendRequest, *SendResponse) error
} }
func RegisterEmojiHandler(s server.Server, hdlr EmojiHandler, opts ...server.HandlerOption) error { func RegisterEmojiHandler(s server.Server, hdlr EmojiHandler, opts ...server.HandlerOption) error {
@@ -114,7 +102,6 @@ func RegisterEmojiHandler(s server.Server, hdlr EmojiHandler, opts ...server.Han
Find(ctx context.Context, in *FindRequest, out *FindResponse) error Find(ctx context.Context, in *FindRequest, out *FindResponse) error
Flag(ctx context.Context, in *FlagRequest, out *FlagResponse) error Flag(ctx context.Context, in *FlagRequest, out *FlagResponse) error
Print(ctx context.Context, in *PrintRequest, out *PrintResponse) error Print(ctx context.Context, in *PrintRequest, out *PrintResponse) error
Send(ctx context.Context, in *SendRequest, out *SendResponse) error
} }
type Emoji struct { type Emoji struct {
emoji emoji
@@ -138,7 +125,3 @@ func (h *emojiHandler) Flag(ctx context.Context, in *FlagRequest, out *FlagRespo
func (h *emojiHandler) Print(ctx context.Context, in *PrintRequest, out *PrintResponse) error { func (h *emojiHandler) Print(ctx context.Context, in *PrintRequest, out *PrintResponse) error {
return h.EmojiHandler.Print(ctx, in, out) return h.EmojiHandler.Print(ctx, in, out)
} }
func (h *emojiHandler) Send(ctx context.Context, in *SendRequest, out *SendResponse) error {
return h.EmojiHandler.Send(ctx, in, out)
}