diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/Makefile b/Makefile index 6bfb72d..553f35c 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ fmt: $(GOFMT) ./... .PHONY: lint -fmt: +lint: $(GOLIST) ./... | grep -v /vendor/ | xargs -L1 golint .PHONY: vet diff --git a/executors/message_manager.go b/executors/message_manager.go index dfec182..e7978b7 100644 --- a/executors/message_manager.go +++ b/executors/message_manager.go @@ -2,13 +2,47 @@ package executors import ( "fmt" + "github.com/abansal4032/go-whatsapp-cli/utils" "time" "github.com/Rhymen/go-whatsapp" ) -// SendText sends the provided text message to the receipient. -func SendText(text, reciever string) error { +// SendMessage sends the provided message to the recipient. +func SendMessage(args interface{}, msgType string) error { + msg, err := composeMessage(args, msgType) + if err != nil { + return fmt.Errorf("error while composing message: %v\n", err) + } + return sendMessage(msg) +} + +func composeMessage(msgMetadata interface{}, msgType string) (interface{}, error) { + switch msgType { + case utils.TEXTMESSAGEKEY: + sendTextArgs, ok := msgMetadata.(*utils.SendTextArgs) + if !ok { + return nil, fmt.Errorf("cannot read args for text message") + } + text := sendTextArgs.GetContent() + utils.MESSAGEFOOTER + receiver := sendTextArgs.GetReceiver() + msg := whatsapp.TextMessage{ + Info: whatsapp.MessageInfo{ + RemoteJid: receiver + utils.CONTACTSUFFIX, + }, + Text: text, + } + return msg, nil + case utils.IMAGEMESSAGEKEY: + case utils.VIDEOMESSAGEKEY: + default: + return nil, fmt.Errorf("unknown message type : %v\n", msgType) + } + // Gotta keep the compiler happy + return nil, fmt.Errorf("unknown message type : %v\n", msgType) +} + +func sendMessage(msg interface{}) error { wac, err := whatsapp.NewConn(5 * time.Second) if err != nil { return fmt.Errorf("error creating connection: %v\n", err) @@ -21,14 +55,6 @@ func SendText(text, reciever string) error { <-time.After(3 * time.Second) - text += "\n sent using github.com/abansal4032/go-whatsapp-cli" - msg := whatsapp.TextMessage{ - Info: whatsapp.MessageInfo{ - RemoteJid: reciever + "@s.whatsapp.net", - }, - Text: text, - } - msgID, err := wac.Send(msg) if err != nil { return fmt.Errorf("error sending message: %v", err) diff --git a/main.go b/main.go index 3abc473..716c862 100644 --- a/main.go +++ b/main.go @@ -41,13 +41,13 @@ func main() { } case "sendText": - var args *utils.SendArgs + var args *utils.SendTextArgs args, err = utils.ParseSendArgs(sendTextCmd, os.Args[2:]) if err != nil { commandUsage(err, sendTextCmd) return } - if err = executors.SendText(args.GetContent(), args.GetReciever()); err != nil { + if err = executors.SendMessage(args, utils.TEXTMESSAGEKEY); err != nil { return } diff --git a/utils/keys.go b/utils/keys.go new file mode 100644 index 0000000..1466262 --- /dev/null +++ b/utils/keys.go @@ -0,0 +1,16 @@ +package utils + +// TEXTMESSAGEKEY identifies a message of text type. +const TEXTMESSAGEKEY = "TextMessage" + +// IMAGEMESSAGEKEY identifies a message of image type. +const IMAGEMESSAGEKEY = "ImageMessage" + +// VIDEOMESSAGEKEY identifies a message of video type. +const VIDEOMESSAGEKEY = "VideoMessage" + +// MESSAGEFOOTER is the identifier added to every message to distinguish the messages sent using CLI. +const MESSAGEFOOTER = "\n sent using github.com/abansal4032/go-whatsapp-client" + +// CONTACTSUFFIX is suffix for the phone number to send the messages. +const CONTACTSUFFIX = "@s.whatsapp.net" diff --git a/utils/models.go b/utils/models.go index ba90f43..09cd0d6 100644 --- a/utils/models.go +++ b/utils/models.go @@ -5,15 +5,15 @@ import ( "fmt" ) -// SendArgs holds the information for sending a message. -type SendArgs struct { +// SendTextArgs holds the information for sending a message. +type SendTextArgs struct { to string text string } // ParseSendArgs parses the arguments for sending a message. -func ParseSendArgs(f *flag.FlagSet, args []string) (*SendArgs, error) { - to := f.String("to", "", "the receipient of the message.") +func ParseSendArgs(f *flag.FlagSet, args []string) (*SendTextArgs, error) { + to := f.String("to", "", "the recipient of the message.") text := f.String("text", "", "the content of the message.") if err := f.Parse(args); err != nil { return nil, err @@ -25,19 +25,19 @@ func ParseSendArgs(f *flag.FlagSet, args []string) (*SendArgs, error) { if *text == "" { return nil, fmt.Errorf("text field empty") } - sendArgs := &SendArgs{ + sendArgs := &SendTextArgs{ to: *to, text: *text, } return sendArgs, nil } -// GetReciever gets the reciever of the message. -func (s *SendArgs) GetReciever() string { +// GetReceiver gets the receiver of the message. +func (s *SendTextArgs) GetReceiver() string { return s.to } // GetContent gets the content of the message. -func (s *SendArgs) GetContent() string { +func (s *SendTextArgs) GetContent() string { return s.text }