From 1e50cc89568c1bd590703c8e9749b03d952960a4 Mon Sep 17 00:00:00 2001 From: kkupreeva Date: Sun, 10 Apr 2022 18:15:11 +0200 Subject: [PATCH] add support for whatsapp --- docs/services.md | 25 ++++++++++ main.go | 2 + service/whatsapp/whatsapp.go | 89 ++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 service/whatsapp/whatsapp.go diff --git a/docs/services.md b/docs/services.md index 5d31b52..2cf3676 100644 --- a/docs/services.md +++ b/docs/services.md @@ -806,3 +806,28 @@ jobs: | TEXTMAGIC_TITLE | "" | | TEXTMAGIC_MESSAGE | "" | | TEXTMAGIC_RECEIVER | "" | + +## Whatsapp + +WhatsApp allows you to send messages to defined contacts. +Multiple contacts can be used separated by comma ','. + +#### Authentication +The authentication process requires you to scan the qr code, that is send through the channel, with the device you are using whatsapp on. +The session struct that is returned can be saved and used to restore the login without scanning the qr code again. +The qr code has a ttl of 20 seconds and the login function throws a timeout err if the time has passed or any other request fails. + +```bash +pingme whatsapp \ + --contact "01234567891,01234567892" \ + --msg "This is an whatsapp message from PingMe CLI" \ + --title "message title" +``` + +- **Variables** + +| Variables | Default Value | +| -------------------------- | :----------------: | +| WHATSAPP_CONTACT | "" | +| WHATSAPP_MESSAGE | "" | +| WHATSAPP_TITLE | "Server time" | \ No newline at end of file diff --git a/main.go b/main.go index 6efc419..851b8f8 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ import ( "github.com/kha7iq/pingme/service/rocketchat" "github.com/kha7iq/pingme/service/slack" "github.com/kha7iq/pingme/service/telegram" + "github.com/kha7iq/pingme/service/whatsapp" "github.com/urfave/cli/v2" ) @@ -58,6 +59,7 @@ email address, Line, Gotify and Wechat.` wechat.Send(), gotify.Send(), textmagic.Send(), + whatsapp.Send(), } err := app.Run(os.Args) diff --git a/service/whatsapp/whatsapp.go b/service/whatsapp/whatsapp.go new file mode 100644 index 0000000..63d74aa --- /dev/null +++ b/service/whatsapp/whatsapp.go @@ -0,0 +1,89 @@ +package whatsapp + +import ( + "context" + "log" + "strings" + + "github.com/kha7iq/pingme/service/helpers" + "github.com/nikoksr/notify" + "github.com/nikoksr/notify/service/whatsapp" + "github.com/urfave/cli/v2" +) + +// whatsapp struct holds data parsed via flags for whatsapp service. +type whatsApp struct { + Contact string + Message string + Title string +} + +// Send parse values from *cli.context and return *cli.Command. +// Values include whatsapp message, title, contacts. +// If multiple contacts are provided then the string is split with "," separator and +// each contact is added to receiver. +func Send() *cli.Command { + var whatsAppOpts whatsApp + return &cli.Command{ + Name: "whatsapp", + Usage: "Send message to whatsapp", + Description: `WhatsApp uses authentication using QR code on terminal and sends messages to defined contacts. +Multiple contacts can be used separated by comma ','. +All configuration options are also available via environment variables.`, + UsageText: "pingme whatsapp --contact 'contact1' --msg 'some message'", + Flags: []cli.Flag{ + &cli.StringFlag{ + Destination: &whatsAppOpts.Contact, + Name: "contact", + Required: true, + Aliases: []string{"c"}, + Usage: "WhatsApp contacts, if sending to multiple contacts separate with ','.", + EnvVars: []string{"WHATSAPP_CONTACT"}, + }, + &cli.StringFlag{ + Destination: &whatsAppOpts.Message, + Name: "msg", + Aliases: []string{"m"}, + Usage: "Message content.", + EnvVars: []string{"WHATSAPP_MESSAGE"}, + }, + &cli.StringFlag{ + Destination: &whatsAppOpts.Title, + Name: "title", + Value: helpers.TimeValue, + Usage: "Title of the message.", + EnvVars: []string{"WHATSAPP_TITLE"}, + }, + }, + Action: func(ctx *cli.Context) error { + whatsappSvc, err := whatsapp.New() + if err != nil { + return err + } + + err = whatsappSvc.LoginWithQRCode() + if err != nil { + return err + } + + contacts := strings.Split(whatsAppOpts.Contact, ",") + for _, v := range contacts { + if len(v) <= 0 { + return helpers.ErrChannel + } + + whatsappSvc.AddReceivers(v) + } + + notifier := notify.New() + notifier.UseServices(whatsappSvc) + + err = notifier.Send(context.Background(), whatsAppOpts.Title, whatsAppOpts.Message) + if err != nil { + return err + } + log.Println("Successfully sent!") + return nil + }, + } +}