nowpaste

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 20 Imported by: 0

README

nowpaste

Documentation Latest GitHub release Github Actions test License

nowpaste is a http server for posting messages to slack

Install

Binary packages

Releases

as AWS Lambda function

The build binary runs as a bootstrap for AWS Lambda functions. Rename the downloaded binary to bootstarap and create a zip archive. Deploy it as a Lambda function!

If a secret named nowpaste/SLACK_TOKEN is managed in AWS Secrets Manager and the appropriate access rights have been granted to the IAM Role, the Slack token can be passed by specifying the environment variable as follows

$ export NOWPASTE_SSM_NAMES=/aws/reference/secretsmanager/nowpaste/SLACK_TOKEN

For more information: prese refer to lambda directory

slack token permissions

It is assumed that nowpaste is passed the bot token of the slack app. An example of a slack app manifest is shown below.

display_information:
  name: nowpaste
  description: http server for posting messages to slack
  background_color: "#0b50e3"
features:
  bot_user:
    display_name: nowpaste
    always_online: true
oauth_config:
  scopes:
    bot:
      - channels:join
      - chat:write
      - chat:write.public
      - chat:write.customize
      - files:write
settings:
  org_deploy_enabled: false
  socket_mode_enabled: false
  token_rotation_enabled: false

Usage

If you want to run locally, you can use the following options

nowpaste [options]
version: current
  -listen string
        http server run on (default ":8080")
  -log-level string
        log level (default "info")
  -path-prefix string
        endpoint path prefix (default "/")
  -slack-token string
        slack token

nowpaste runs http server on http://#{listen}/ If you are using a Lambda Function URL, it will be https://<url_id>.lambda-url.<region>.on.aws/.

The simplest use case would be as follows

payload.json

{
   "channel": "<slack_channel_id>",
   "icon_emoji": ":tada:",
   "username": "nowpaste",
   "text": "Hello world!!"
}
$ cat payload.json  | curl "https://<url_id>.lambda-url.<region>.on.aws/" \
    -X POST \
    -H "Content-type: application/json" \
    -d @-

Amazon SNS http endpoint

nowpaste can accept Amazon SNS notification messages. Add a SNS topic http(s) endpoint to https://<url_id>.lambda-url.<region>.on.aws/amazon-sns/{channel_id}

Query Parameters/Amazon SNS Message Attribuites for Message Formatting

nowpaste allows you to control the format of the message using either query parameters or Amazon SNS Message Attributes. You can switch the format of the message by adding the following parameters to the URL or setting them as SNS Message Attributes:

  • as_file: If this parameter is set, the message is posted as a file. Valid values are true or false. The default is false.

  • as_message: If this parameter is set, the message is posted as a regular text message. Valid values are true or false. The default is true.

For example, to post the message as a file, specify the URL as follows:

$ cat payload.json  | curl "https://<url_id>.lambda-url.<region>.on.aws/?as_file=true" \
    -X POST \
    -H "Content-type: application/json" \
    -d @-

This URL will post the message as a file.

$ aws sns publish --topic-arn <topic_arn> --message "Hello world!!" --message-attributes '{"as_file": {"DataType": "String", "StringValue": "true"}}'

This SNS message will post the message as a file.

If neither as_file nor as_message is specified, the message will be automatically posted as a file if it exceeds 4000 characters or 6 lines.

LICENSE

MIT License

Copyright (c) 2022 IKEDA Masashi

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Content

type Content struct {
	Channel       string             `json:"channel,omitempty"`
	IconEmoji     string             `json:"icon_emoji,omitempty"`
	IconURL       string             `json:"icon_url,omitempty"`
	Username      string             `json:"username"`
	Blocks        json.RawMessage    `json:"blocks,omitempty"`
	Text          string             `json:"text,omitempty"`
	EscapeText    bool               `json:"escape_text,omitempty"`
	CodeBlockText bool               `json:"code_block_text,omitempty"`
	Attachments   []slack.Attachment `json:"attachments,omitempty"`
	AsFile        bool               `json:"as_file,omitempty"`
	AsMessage     bool               `json:"as_message,omitempty"`
	Filename      string             `json:"filename,omitempty"`
}

func (*Content) IsRich added in v0.2.0

func (content *Content) IsRich() bool

func (*Content) Merge added in v0.5.0

func (content *Content) Merge(c *Content)

type HTTPNotification

type HTTPNotification struct {
	Type              string                      `json:"Type"`
	MessageId         string                      `json:"MessageId"`
	Token             string                      `json:"Token,omitempty"` // Only for subscribe and unsubscribe
	TopicArn          string                      `json:"TopicArn"`
	Subject           string                      `json:"Subject,omitempty"` // Only for Notification
	Message           string                      `json:"Message"`
	SubscribeURL      string                      `json:"SubscribeURL,omitempty"` // Only for subscribe and unsubscribe
	Timestamp         time.Time                   `json:"Timestamp"`
	SignatureVersion  string                      `json:"SignatureVersion"`
	Signature         string                      `json:"Signature"`
	SigningCertURL    string                      `json:"SigningCertURL"`
	UnsubscribeURL    string                      `json:"UnsubscribeURL,omitempty"` // Only for notifications
	MessageAttributes map[string]MessageAttribute `json:"MessageAttributes,omitempty"`
}

https://docs.aws.amazon.com/sns/latest/dg/json-formats.html

type MessageAttribute added in v0.5.0

type MessageAttribute struct {
	Type  string `json:"Type"`
	Value string `json:"Value"`
}

type NowPaste

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

func New

func New(slackToken string) *NowPaste

func (*NowPaste) CheckBasicAuth added in v0.3.0

func (nwp *NowPaste) CheckBasicAuth(req *http.Request) bool

func (*NowPaste) ServeHTTP

func (nwp *NowPaste) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*NowPaste) SetBasicAuth added in v0.3.0

func (nwp *NowPaste) SetBasicAuth(user string, pass string)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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