Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(service): add support for whatsapp #63

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions docs/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" |
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -58,6 +59,7 @@ email address, Line, Gotify and Wechat.`
wechat.Send(),
gotify.Send(),
textmagic.Send(),
whatsapp.Send(),
}

err := app.Run(os.Args)
Expand Down
89 changes: 89 additions & 0 deletions service/whatsapp/whatsapp.go
Original file line number Diff line number Diff line change
@@ -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
},
}
}