Skip to content

Commit

Permalink
adding build files and basic working send text message
Browse files Browse the repository at this point in the history
  • Loading branch information
abansal88 committed Mar 29, 2020
1 parent dcc0170 commit 9341007
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 22 deletions.
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
GOCMD=go
GOBUILD=$(GOCMD) build
GOFMT=$(GOCMD) fmt
GOLIST=$(GOCMD) list
GOVET=$(GOCMD) vet

.PHONY: all
all: fmt vet lint build

.PHONY: build
build:
$(GOBUILD) -o whatsapp-cli -v

.PHONY: fmt
fmt:
$(GOFMT) ./...

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

.PHONY: vet
vet:
$(GOVET) ./...

.PHONY: clean
clean:
rm whatsapp-cli
33 changes: 33 additions & 0 deletions executors/message_manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
package executors

import (
"fmt"
"time"

"github.com/Rhymen/go-whatsapp"
)

// SendText sends the provided text message to the receipient.
func SendText(text, reciever string) error {
wac, err := whatsapp.NewConn(5 * time.Second)
if err != nil {
return fmt.Errorf("error creating connection: %v\n", err)
}

err = LoginWithConnection(wac)
if err != nil {
return fmt.Errorf("error logging in: %v\n", err)
}

<-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)
}

fmt.Printf("successfully sent, messaageID : %v\n", msgID)
return nil
}
9 changes: 9 additions & 0 deletions executors/session_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ func Login() error {
if err != nil {
return fmt.Errorf("error while logging in %v\n", err)
}
return LoginWithConnection(wac)
}

// LoginWithConnection logs in the user using a provided connection. It ries to see if a session already exists. If not, tries to create a
// new one using qr scanned on the terminal.
func LoginWithConnection(wac *whatsapp.Conn) error {
//load saved session
session, err := readSession()
if err == nil {
Expand All @@ -25,7 +31,9 @@ func Login() error {
if err != nil {
return fmt.Errorf("restoring failed: %v\n", err)
}
fmt.Println("already logged in, using the same session")
} else {
fmt.Println("no saved login info found, initiating new login")
//no saved session -> regular login
qr := make(chan string)
go func() {
Expand All @@ -43,6 +51,7 @@ func Login() error {
if err != nil {
return fmt.Errorf("error saving session: %v\n", err)
}
fmt.Println("login successful")
return nil
}

Expand Down
38 changes: 19 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
package main

import (
"fmt"
"flag"
"fmt"
"os"

"github.com/abansal4032/go-whatsapp-cli/executors"
"github.com/abansal4032/go-whatsapp-cli/utils"
)

func commandUsage(err error) {
func commandUsage(err error, f *flag.FlagSet) {
fmt.Println("correct command usage is described below:")
f.PrintDefaults()
}

func errorHandler(err error) {
// TODO : graceful error handling here
if err != nil {
fmt.Println("correct command usage is described below:")
flag.PrintDefaults()
fmt.Printf("flow errored out : %v\n", err.Error())
}
}

func main() {
sendTextCmd := flag.NewFlagSet("sendText", flag.ExitOnError)
var err error
defer func() {
commandUsage(err)
errorHandler(err)
}()

switch os.Args[1] {

case "login":
if err = executors.Login(); err != nil {
fmt.Println(err)
return
}

case "logout":
if err = executors.Logout(); err != nil {
fmt.Println(err)
return
}

case "sendText":
//var args *utils.SendArgs
_, err = utils.ParseSendArgs(sendTextCmd, os.Args[2:])
var args *utils.SendArgs
args, err = utils.ParseSendArgs(sendTextCmd, os.Args[2:])
if err != nil {
fmt.Println(err)
commandUsage(err, sendTextCmd)
return
}
/*if err = executors.SendText(args); err != nil {
fmt.Println(err)
if err = executors.SendText(args.GetContent(), args.GetReciever()); err != nil {
return
}*/
}

default:
fmt.Println("wrong command provided. please see below for the list of permitted actions")
flag.PrintDefaults()
// TODO : add a list of all permitted actions
err = fmt.Errorf("wrong command provided. please see below for the list of permitted actions")
return

}
}
}
24 changes: 21 additions & 3 deletions utils/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,39 @@ import (
"fmt"
)

// SendArgs holds the information for sending a message.
type SendArgs struct {
to string
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.")
text := f.String("text", "", "the content of the message.")
if err := f.Parse(args); err != nil {
return nil, err
}
// checks for mandatory fields
if *to == "" {
return nil, fmt.Errorf("wrong arguments provided")
return nil, fmt.Errorf("to field empty")
}
if *text == "" {
return nil, fmt.Errorf("text field empty")
}
sendArgs := &SendArgs{
to: *to,
to: *to,
text: *text,
}
return sendArgs, nil
}

// GetReciever gets the reciever of the message.
func (s *SendArgs) GetReciever() string {
return s.to
}

// GetContent gets the content of the message.
func (s *SendArgs) GetContent() string {
return s.text
}

0 comments on commit 9341007

Please sign in to comment.