googlechat

package
v0.41.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2023 License: MIT Imports: 7 Imported by: 0

README

Google Chat

go.dev reference

Prerequisites

In order to integrate notify with a Google Chat Application, an "Application Default Credentials" file must be supplied.

For more information on Google Application credential JSON files see: https://cloud.google.com/docs/authentication/application-default-credentials

a example service account key JSON file has been provided in this directory example_credentials.json which takes the following shape:

{
  "type": "service_account",
  "project_id": "",
  "private_key_id": "",
  "private_key": "",
  "client_email": "",
  "client_id": "",
  "auth_uri": "",
  "token_uri": "",
  "auth_provider_x509_cert_url": "",
  "client_x509_cert_url": ""
}

Usage:

package main

import (
	"context"
	"fmt"
	"log"
	"strings"

	"github.com/nikoksr/notify"
	"github.com/nikoksr/notify/service/googlechat"
	"google.golang.org/api/chat/v1"
	"google.golang.org/api/option"
)

func main() {
    ctx := context.Background()

    withCred := option.WithCredentialsFile("credentials.json")
    withSpacesScope := option.WithScopes("https://www.googleapis.com/auth/chat.spaces") 
    
    // In this example, we'll send a message to all spaces within the google workspace.
    // Start by using the google chat API to find the spaces within a workspace.

    listSvc, err := chat.NewService(ctx, withCred, withSpacesScope)
    spaces, err := listSvc.Spaces.List().Do()

    if err != nil {
        log.Fatalf("svc.Spaces.List().Do() failed: %s", err.Error())
    }

    // With the the list of spaces, loop over each space creating a receivers slice
    // of all the space.Name's.     
    receivers := make([]string, 0)

    for _, space := range spaces.Spaces {
         fmt.Printf("space %s\n", space.DisplayName)

         // The googlechat service handles prepending "spaces/" to the name.
         // Make sure the space.Name does not prepend "spaces/".
         name := strings.Replace(space.Name, "spaces/", "", 1)

         receivers = append(receivers, name)
    }
    
    msgSvc, err := googlechat.New(withCred)

    // alternatively, if you would like to pass a context
    // use googlechat.NewWithContext(ctx, ...options)

    if err != nil {
        log.Fatalf("googlechat.New() failed: %s", err.Error())
    }

    msgSvc.AddReceivers(receivers...)

    notifier := notify.NewWithServices(msgSvc)

    fmt.Printf("sending message to %d spaces\n", len(receivers))
    err = notifier.Send(ctx, "subject", "message")
    if err != nil {
        log.Fatalf("notifier.Send() failed: %s", err.Error())
    }

    log.Println("notification sent")
}

Documentation

Overview

Package googlechat provides message notification integration sent to multiple spaces within a Google Chat Application.

Disclaimer: In order to integrate `notify` with a Google Chat Application, an "Application Default Credentials" file must be supplied.

For more information on Google Application credential JSON files see: https://cloud.google.com/docs/authentication/application-default-credentials

Usage:

		package main

		import (
		    "context"
		    "fmt"
		    "log"
		    "strings"

		    "github.com/nikoksr/notify"
		    "github.com/nikoksr/notify/service/googlechat"
		    "google.golang.org/api/chat/v1"
		    "google.golang.org/api/option"
		)

		func main() {
		    ctx := context.Background()

		    withCred := option.WithCredentialsFile("credentials.json")
		    withSpacesScope := option.WithScopes("https://www.googleapis.com/auth/chat.spaces")

	        // In this example, we'll send a message to all spaces within the google workspace
	        // Start by using the google chat API to find the spaces within a workspace.

		    listSvc, err := chat.NewService(ctx, withCred, withSpacesScope)
		    spaces, err := listSvc.Spaces.List().Do()

		    if err != nil {
		        log.Fatalf("svc.Spaces.List().Do() failed: %s", err.Error())
		    }

	        // With the the list of spaces, loop over each space creating a receivers slice
	        // of all the space.Name's.
	        receivers := make([]string, 0)

		    for _, space := range spaces.Spaces {
	            fmt.Printf("space %s\n", space.DisplayName)

	             // The googlechat service handles prepending "spaces/" to the name.
	             // Make sure the space.Name does not prepend "spaces/".
		        name := strings.Replace(space.Name, "spaces/", "", 1)

		        sps = append(sps, name)
		    }

		    msgSvc, err := googlechat.New(withCred)

            // alternatively, if you would like to pass a context
            // use googlechat.NewWithContext(ctx, ...options)

		    if err != nil {
		        log.Fatalf("googlechat.New() failed: %s", err.Error())
		    }

		    msgSvc.AddReceivers(receivers...)

		    notifier := notify.NewWithServices(msgSvc)

		    fmt.Printf("sending message to %d spaces\n", len(receivers))
		    err = notifier.Send(ctx, "subject", "message")
		    if err != nil {
		        log.Fatalf("notifier.Send() failed: %s", err.Error())
		    }

		    log.Println("notification sent")
		}

Package googlechat provides message notification integration sent to multiple spaces within a Google Chat Application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service encapsulates the google chat client along with internal state for storing chat spaces.

func New

func New(options ...option.ClientOption) (*Service, error)

New returns an instance of the google chat notification service

func NewWithContext

func NewWithContext(ctx context.Context, options ...option.ClientOption) (*Service, error)

NewWithContext returns an instance of the google chat notification service with the specified context. Utilize this constructor if the message requires the context to be set.

func (*Service) AddReceivers

func (s *Service) AddReceivers(spaces ...string)

AddReceivers takes a name of authorized spaces and appends them to the internal spaces slice. The Send method will send a given message to all those spaces.

func (*Service) Send

func (s *Service) Send(ctx context.Context, subject, message string) error

Send takes a message subject and a message body and sends them to all the spaces previously set.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL