-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding session management with login and logout capabilities
- Loading branch information
abansal88
committed
Mar 29, 2020
1 parent
64d22c1
commit dcc0170
Showing
4 changed files
with
162 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package executors | ||
|
||
// SendText sends the provided text message to the receipient. | ||
func SendText(text, reciever string) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package executors | ||
|
||
import ( | ||
"encoding/gob" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
qrcodeTerminal "github.com/Baozisoftware/qrcode-terminal-go" | ||
"github.com/Rhymen/go-whatsapp" | ||
) | ||
|
||
// Login logs in the user. It ries to see if a session already exists. If not, tries to create a | ||
// new one using qr scanned on the terminal. | ||
func Login() error { | ||
wac, err := whatsapp.NewConn(5 * time.Second) | ||
if err != nil { | ||
return fmt.Errorf("error while logging in %v\n", err) | ||
} | ||
//load saved session | ||
session, err := readSession() | ||
if err == nil { | ||
//restore session | ||
session, err = wac.RestoreWithSession(session) | ||
if err != nil { | ||
return fmt.Errorf("restoring failed: %v\n", err) | ||
} | ||
} else { | ||
//no saved session -> regular login | ||
qr := make(chan string) | ||
go func() { | ||
terminal := qrcodeTerminal.New() | ||
terminal.Get(<-qr).Print() | ||
}() | ||
session, err = wac.Login(qr) | ||
if err != nil { | ||
return fmt.Errorf("error during login: %v\n", err) | ||
} | ||
} | ||
|
||
//save session | ||
err = writeSession(session) | ||
if err != nil { | ||
return fmt.Errorf("error saving session: %v\n", err) | ||
} | ||
return nil | ||
} | ||
|
||
// Logout logs out the user. | ||
func Logout() error { | ||
return removeSession() | ||
} | ||
|
||
func readSession() (whatsapp.Session, error) { | ||
session := whatsapp.Session{} | ||
file, err := os.Open(os.TempDir() + "/whatsappCLISession.gob") | ||
if err != nil { | ||
return session, err | ||
} | ||
defer file.Close() | ||
decoder := gob.NewDecoder(file) | ||
err = decoder.Decode(&session) | ||
if err != nil { | ||
return session, err | ||
} | ||
return session, nil | ||
} | ||
|
||
func writeSession(session whatsapp.Session) error { | ||
file, err := os.Create(os.TempDir() + "/whatsappCLISession.gob") | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
encoder := gob.NewEncoder(file) | ||
err = encoder.Encode(session) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func removeSession() error { | ||
return os.Remove(os.TempDir() + "/whatsappCLISession.gob") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package utils | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
type SendArgs struct { | ||
to string | ||
} | ||
|
||
func ParseSendArgs(f *flag.FlagSet, args []string) (*SendArgs, error) { | ||
to := f.String("to", "", "the receipient 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") | ||
} | ||
sendArgs := &SendArgs{ | ||
to: *to, | ||
} | ||
return sendArgs, nil | ||
} |