Skip to content

Commit

Permalink
add google chat (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
parrasajad committed Jul 14, 2022
1 parent 5fc442a commit 9cd2c6a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
51 changes: 51 additions & 0 deletions pkg/providers/googlechat/googlechat.go
@@ -0,0 +1,51 @@
package googlechat

import (
"fmt"

"github.com/containrrr/shoutrrr"
"github.com/pkg/errors"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/notify/pkg/utils"
"go.uber.org/multierr"
)

type Provider struct {
GoogleChat []*Options `yaml:"googleChat,omitempty"`
}

type Options struct {
ID string `yaml:"id,omitempty"`
Space string `yaml:"space,omitempty"`
Key string `yaml:"key,omitempty"`
Token string `yaml:"token,omitempty"`
GoogleChatFormat string `yaml:"google_chat_format,omitempty"`
}

func New(options []*Options, ids []string) (*Provider, error) {
provider := &Provider{}

for _, o := range options {
if len(ids) == 0 || utils.Contains(ids, o.ID) {
provider.GoogleChat = append(provider.GoogleChat, o)
}
}

return provider, nil
}

func (p *Provider) Send(message, CliFormat string) error {
var GoogleChatErr error
for _, pr := range p.GoogleChat {
msg := utils.FormatMessage(message, utils.SelectFormat(CliFormat, pr.GoogleChatFormat))
url := fmt.Sprintf("googlechat://chat.googleapis.com/v1/spaces/%s/messages?key=%s&token=%s", pr.Space, pr.Key, pr.Token)
err := shoutrrr.Send(url, msg)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("failed to send googleChat notification for id: %s ", pr.ID))
GoogleChatErr = multierr.Append(GoogleChatErr, err)
continue
}
gologger.Verbose().Msgf("googleChat notification sent for id: %s", pr.ID)
}
return GoogleChatErr
}
24 changes: 17 additions & 7 deletions pkg/providers/providers.go
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/notify/pkg/providers/custom"
"github.com/projectdiscovery/notify/pkg/providers/discord"
"github.com/projectdiscovery/notify/pkg/providers/googlechat"
"github.com/projectdiscovery/notify/pkg/providers/pushover"
"github.com/projectdiscovery/notify/pkg/providers/slack"
"github.com/projectdiscovery/notify/pkg/providers/smtp"
Expand All @@ -18,13 +19,14 @@ import (

// ProviderOptions is configuration for notify providers
type ProviderOptions struct {
Slack []*slack.Options `yaml:"slack,omitempty"`
Discord []*discord.Options `yaml:"discord,omitempty"`
Pushover []*pushover.Options `yaml:"pushover,omitempty"`
SMTP []*smtp.Options `yaml:"smtp,omitempty"`
Teams []*teams.Options `yaml:"teams,omitempty"`
Telegram []*telegram.Options `yaml:"telegram,omitempty"`
Custom []*custom.Options `yaml:"custom,omitempty"`
Slack []*slack.Options `yaml:"slack,omitempty"`
Discord []*discord.Options `yaml:"discord,omitempty"`
Pushover []*pushover.Options `yaml:"pushover,omitempty"`
SMTP []*smtp.Options `yaml:"smtp,omitempty"`
Teams []*teams.Options `yaml:"teams,omitempty"`
Telegram []*telegram.Options `yaml:"telegram,omitempty"`
GoogleChat []*googlechat.Options `yaml:"googlechat,omitempty"`
Custom []*custom.Options `yaml:"custom,omitempty"`
}

// Provider is an interface implemented by providers
Expand Down Expand Up @@ -67,6 +69,14 @@ func New(providerOptions *ProviderOptions, options *types.Options) (*Client, err
}
client.providers = append(client.providers, provider)
}
if providerOptions.GoogleChat != nil && (len(options.Providers) == 0 || utils.Contains(options.Providers, "googlechat")) {

provider, err := googlechat.New(providerOptions.GoogleChat, options.IDs)
if err != nil {
return nil, errors.Wrap(err, "could not create googlechat provider client")
}
client.providers = append(client.providers, provider)
}
if providerOptions.SMTP != nil && (len(options.Providers) == 0 || utils.Contains(options.Providers, "smtp")) {

provider, err := smtp.New(providerOptions.SMTP, options.IDs)
Expand Down

0 comments on commit 9cd2c6a

Please sign in to comment.