action

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2018 License: MIT Imports: 8 Imported by: 0

README

Slack Interactive Actions

Work with Slack's interactive messages, documented here.

Usage

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/botopolis/bot"
	"github.com/botopolis/slack/action"
	oslack "github.com/nlopes/slack"
)

type ExamplePlugin struct{}

func (p ExamplePlugin) Load(r *bot.Robot) {
	fmt.Println("Loaded")

	var actions action.Plugin
	if ok := r.Plugin(&actions); !ok {
		r.Logger.Error("Example plugin requires slack/action.Plugin")
		return
	}

	r.Hear(bot.Regexp("trigger"), func(r bot.Responder) error {
		return r.Send(bot.Message{
			Params: oslack.PostMessageParameters{
				Attachments: []oslack.Attachment{{
					Text:       "Trigger example",
					CallbackID: "example",
					Actions: []oslack.AttachmentAction{{
						Name:  "check",
						Type:  "button",
						Text:  "Do it",
						Value: "true",
					}, {
						Name:  "check",
						Type:  "button",
						Text:  "Nah",
						Style: "danger",
						Value: "false",
					}},
				}},
			},
		})
	})

	// handle example callback ID with a function
	actions.Add("example", func(a oslack.AttachmentActionCallback) {
		if len(a.Actions) < 0 {
			return
		}
		if a.Actions[0].Value == "true" {
			// do the thing
		}
	})
}

func main() {
	bot.New(
		ExampleChat{},
		action.New("/interaction", "signing secret!"),
		ExamplePlugin{},
	).Run()
}
Output:

Loaded

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Plugin

type Plugin struct {

	// Path at which our webhook sits.
	Path string
	// Signing secret to verify message comes from slack.
	SigningSecret string
	// contains filtered or unexported fields
}

Plugin conforms to the botopolis/bot.Plugin interface

func New

func New(path, signingSecret string) *Plugin

New returns a new plugin taking arguments for path and token

func (Plugin) Add

func (r Plugin) Add(callbackID string, fn func(slack.AttachmentActionCallback))

Add registers a callback for the given callbackID

func (Plugin) Load

func (p Plugin) Load(r *bot.Robot)

Load installs the webhook

func (Plugin) Run

func (r Plugin) Run(cb slack.AttachmentActionCallback)

Run runs the callback for the slack action

Jump to

Keyboard shortcuts

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