Skip to content

Commit

Permalink
refactoring to support other message types easily
Browse files Browse the repository at this point in the history
  • Loading branch information
abansal88 committed Mar 30, 2020
1 parent fcb46dd commit f8151c7
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fmt:
$(GOFMT) ./...

.PHONY: lint
fmt:
lint:
$(GOLIST) ./... | grep -v /vendor/ | xargs -L1 golint

.PHONY: vet
Expand Down
46 changes: 36 additions & 10 deletions executors/message_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
16 changes: 16 additions & 0 deletions utils/keys.go
Original file line number Diff line number Diff line change
@@ -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"
16 changes: 8 additions & 8 deletions utils/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit f8151c7

Please sign in to comment.