slack

package
v0.0.0-...-888b30f Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2021 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBotMessageForTesting

func GetBotMessageForTesting() string

GetBotMessageForTesting returns a mock bot message (JSON).

func GetTokenFromTeamId

func GetTokenFromTeamId(a *App, teamID string) string

func IsBotMessage

func IsBotMessage(event EventMessage) bool

IsBotMessage returns true if event is created by a bot.

func ParseEvent

func ParseEvent(data []byte) (interface{}, error)

ParseEvent will try parsing slack events and return the first matching struct

Possible event types - EventChallenge

It will return map[string]interface{} if no type is matched

func SendMessage

func SendMessage(postMessage ChatPostMessage)

SendMessage sends to slack

Documentation: https://api.slack.com/methods/chat.postMessage

func Verify

func Verify(slackSigningToken []byte, timestamp, body, receivedMAC string) bool

Verify verifies the request is coming from Slack

Read https://api.slack.com/docs/verifying-requests-from-slack

func VerifyRequest

func VerifyRequest(req *http.Request, slackSigningToken []byte) bool

VerifyRequest is a wrapper around `Verify`

Types

type App

type App struct {
	SigningToken        string
	BotOAuthAccessToken string
	SlackClientId       string
	SlackClientSecret   string
	HttpClient          HttpClient
	// if it's true, the request verification does not happen
	TestMode    bool
	MongoClient *mongo.Client
}

App manages Global Application State

func GetMockApp

func GetMockApp(httpClient HttpClient) App

func New

func New(slackSigningToken, botOAuthAccessToken, slackClientId, slackClientSecret string, httpClient HttpClient, mongoClient *mongo.Client) App

New creates a slack API application

func (*App) CommandHandler

func (app *App) CommandHandler(w http.ResponseWriter, r *http.Request)

CommandHandler handles slack slash command

ENDPOINT /api/slack/command

Example Usage /time 2018-12-31 21:40 PST => 2019-01-01 14:40 KST

func (*App) EventHandler

func (app *App) EventHandler(w http.ResponseWriter, r *http.Request)

EventHandler responds to the Slack Event

When slack is first connected, it sends "Challenge" we need to return back the challenge code right away to be connected

func (*App) GetOauthCollection

func (app *App) GetOauthCollection() *mongo.Collection

func (*App) OauthHandler

func (app *App) OauthHandler(writer http.ResponseWriter, request *http.Request)

type BotProfile

type BotProfile struct {
	ID      string `json:"id"`
	Deleted bool   `json:"deleted"`
	Name    string `json:"name"`
	Updated int    `json:"updated"`
	AppID   string `json:"app_id"`
	Icons   struct {
		Image36 string `json:"image_36"`
		Image48 string `json:"image_48"`
		Image72 string `json:"image_72"`
	} `json:"icons"`
	TeamID string `json:"team_id"`
}

type ChatPostMessage

type ChatPostMessage struct {
	Token   string `json:"token"`
	Channel string `json:"channel"`
	Text    string `json:"text"`

	Attachments []interface{} `json:"attchments,omitempty"`
	ThreadTs    string        `json:"thread_ts,omitempty"`
	Mrkdown     bool          `json:"mrkdown,omitempty"`
	AsUser      bool          `json:"as_user,omitempty"`
}

ChatPostMessage is a struct sent to Slack

type EventChallenge

type EventChallenge struct {
	Token     string `json:"token"`
	Challenge string `json:"challenge"`
	Type      string `json:"type"`
}

EventChallenge is the first event sent when registering the app

The app should return challenge right away

type EventMessage

type EventMessage struct {
	Token       string           `json:"token"`
	TeamID      string           `json:"team_id"`
	APIAppID    string           `json:"api_app_id"`
	Event       EventMessageType `json:"event"`
	Type        string           `json:"type"`
	EventID     string           `json:"event_id"`
	EventTime   int              `json:"event_time"`
	AuthedUsers []string         `json:"authed_users"`
}

EventMessage is a struct sent by Slack

type EventMessageType

type EventMessageType struct {
	ClientMsgID string `json:"client_msg_id"`
	Type        string `json:"type"`

	Text string `json:"text"`
	User string `json:"user"`
	Ts   string `json:"ts"`
	Team string `json:"team"`

	// Only filled when the message is from a bot.
	BotID      string      `json:"bot_id"`
	BotProfile *BotProfile `json:"bot_profile,omitempty"`

	// (optional) if this field exists, then it's a thread reply
	ThreadTs string `json:"thread_ts,omitempty"`

	// (optional) if this field exists, then it's a thread reply
	ParentUserID string `json:"parent_user_id,omitempty"`
	Channel      string `json:"channel"`
	EventTs      string `json:"event_ts"`
	ChannelType  string `json:"channel_type"`
}

EventMessageType holds the information of message type

type HttpClient

type HttpClient interface {
	Do(*http.Request) (*http.Response, error)
}

type HttpClientImpl

type HttpClientImpl struct {
	Client *http.Client
}

func (*HttpClientImpl) Do

func (c *HttpClientImpl) Do(r *http.Request) (*http.Response, error)

type Response

type Response struct {
	Text         string       `json:"text"`
	ResponseType ResponseType `json:"response_type,omitempty"`
}

Response is a Slack Response struct

type ResponseType

type ResponseType string

ResponseType is a type of slack message type

const (
	// InChannel = everyone in the channel can view the message
	InChannel ResponseType = "in_channel"
	// Ephemeral = only the person who triggered the command can view the message
	Ephemeral ResponseType = "ephemeral"
)

Jump to

Keyboard shortcuts

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