mattermost

package
v0.41.2 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 6 Imported by: 0

README

Mattermost Usage

Ensure that you have already navigated to your GOPATH and installed the following packages:

  • go get -u github.com/nikoksr/notify

Steps for Mattermost Server

These are general and very high level instructions

  1. Create a new Mattermost server / Join existing Mattermost server
  2. Make sure your Username/loginID have the OAuth permission scope(s): create_post
  3. Copy the Channel ID of the channel you want to post a message to. You can grab the Channel ID in channel info. example: yfgstwuisnshydhd
  4. Now you should be good to use the code below

Sample Code

package main

import (
    "os"

    "github.com/nikoksr/notify"
    "github.com/nikoksr/notify/service/mattermost"
)

func main() {

    // Init notifier
    notifier := notify.New()
    ctx := context.Background()

    // Provide your Mattermost server url
    mattermostService := mattermost.New("https://myserver.cloud.mattermost.com")

    // Provide username as loginID and password to login into above server.
    // NOTE: This generates auth token which will get expired, invoking this method again
    // after expiry will generate new token and uses for further requests.
    err := mattermostService.LoginWithCredentials(ctx, "someone@gmail.com", "somepassword")
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    // Passing a Mattermost channel/chat id as receiver for our messages.
    // Where to send our messages.
    mattermostService.AddReceivers("CHANNEL_ID")

    // Tell our notifier to use the Mattermost service. You can repeat the above process
    // for as many services as you like and just tell the notifier to use them.
    notifier.UseServices(mattermostService)

    // Add presend and postsend hooks that you need to execute before every requests and after
    // every response respectively. Multiple presend and postsend are executed in the order defined here.
    // refer service/http for the more info.
    // PreSend hook
    mattermostService.PreSend(func(req *stdhttp.Request) error {
        log.Printf("Sending message to %s server", req.URL)
        return nil
    })
    // PostSend hook
    mattermostService.PostSend(func(req *stdhttp.Request, resp *stdhttp.Response) error {
        log.Printf("Message sent to %s server with status %d", req.URL, resp.StatusCode)
        return nil
    })

    // Send a message
    err = notifier.Send(
        ctx,
        "Hello from notify :wave:\n",
        "Message written in Go!",
    )
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

}

Documentation

Overview

Package mattermost provides message notification integration for mattermost.com.

Usage:

package main

import (
	"os"

	"github.com/heilmela/notify"
	"github.com/heilmela/notify/service/mattermost"
)

func main() {

	// Init notifier
	notifier := notify.New()
	ctx := context.Background()

	// Provide your Mattermost server url
	mattermostService := mattermost.New("https://myserver.cloud.mattermost.com")

	// Provide username as loginID and password to login into above server.
	// NOTE: This generates auth token which will get expired, invoking this method again
	// after expiry will generate new token and uses for further requests.
	err := mattermostService.LoginWithCredentials(ctx, "someone@gmail.com", "somepassword")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Passing a Mattermost channel/chat id as receiver for our messages.
	// Where to send our messages.
	mattermostService.AddReceivers("CHANNEL_ID")

	// Tell our notifier to use the Mattermost service. You can repeat the above process
	// for as many services as you like and just tell the notifier to use them.
	notifier.UseServices(mattermostService)

	// Add presend and postsend hooks that you need to execute before every requests and after
	// every response respectively. Multiple presend and postsend are executed in the order defined here.
	// refer service/http for the more info.
	// PreSend hook
	mattermostService.PreSend(func(req *stdhttp.Request) error {
		log.Printf("Sending message to %s server", req.URL)
		return nil
	})
	// PostSend hook
	mattermostService.PostSend(func(req *stdhttp.Request, resp *stdhttp.Response) error {
		log.Printf("Message sent to %s server with status %d", req.URL, resp.StatusCode)
		return nil
	})

	// Send a message
	err = notifier.Send(
		ctx,
		"Hello from notify :wave:",
		"Message written in Go!",
	)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

Package mattermost provides message notification integration for mattermost.com.

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 notify httpService client and contains mattermost channel ids.

func New

func New(url string) *Service

New returns a new instance of a Mattermost notification service.

func (*Service) AddReceivers

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

AddReceivers takes Mattermost channel IDs or Chat IDs and adds them to the internal channel ID list. The Send method will send a given message to all these channels.

func (*Service) LoginWithCredentials

func (s *Service) LoginWithCredentials(ctx context.Context, loginID, password string) error

LoginWithCredentials provides helper for authentication using Mattermost user/admin credentials.

func (*Service) PostSend

func (s *Service) PostSend(hook http.PostSendHookFn)

PostSend adds a post-send hook to the service. The hook will be executed after sending a request to a receiver.

func (*Service) PreSend

func (s *Service) PreSend(hook http.PreSendHookFn)

PreSend adds a pre-send hook to the service. The hook will be executed before sending a request to a receiver.

func (*Service) Send

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

Send takes a message subject and a message body and send them to added channel ids. you will need a 'create_post' permission for your username. refer https://api.mattermost.com/ for more info

Jump to

Keyboard shortcuts

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