pkg

package
v1.0.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2020 License: MIT Imports: 20 Imported by: 1

Documentation

Overview

Package pkg contains types and methods for interacting with the barometer.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainsEmpty

func ContainsEmpty(ss ...string) bool

ContainsEmpty checks if an array of strings contains an empty string.

Example
containsEmpty := []string{"A", "B", ""}
if ContainsEmpty(containsEmpty...) {
	fmt.Println("contains an empty string")
}
Output:

contains an empty string

func FetchTimestamp

func FetchTimestamp(timestamp, area string) (time.Time, error)

FetchTimestamp obtains the timestamp value from the request and location.

Example
ts := strconv.FormatInt(time.Now().Unix(), 10)
area := "Asia/Manila" // IANA-compliant timezone name
timestamp, err := FetchTimestamp(ts, area)
if err != nil {
	log.Fatal(err)
}
fmt.Printf("time is: %v", timestamp)
Output:

func ParseMessage

func ParseMessage(s string) (int, string, error)

ParseMessage extracts the barometer measure and notes from a given text.

Example
message := "4 Had awesome dinner!"
measure, notes, err := ParseMessage(message)
if err != nil {
	log.Fatalf("cannot parse message, err: %v", err)
}
fmt.Printf("Your message: %s (%d)", notes, measure)
Output:

Your message: Had awesome dinner! (4)

func VerifyWebhook

func VerifyWebhook(form url.Values, token string) error

VerifyWebhook checks if the submitted request matches the token provided by Slack

Example
// Example token obtained from Slack
token := "M4KY3LOVPIhE9E2zIMAz0QUE"

// Example query sent by the slash command
q := "token=M4KY3LOVPIhE9E2zIMAz0QUE&text=4 hello&user_id=UA1DXYCL2"
v, err := url.ParseQuery(q)
if err != nil {
	panic(err)
}

if err := VerifyWebhook(v, token); err != nil {
	// Webhook didn't match, throw an error
	log.Fatal(err)
}
Output:

Types

type Attachment

type Attachment struct {
	Color     string `json:"color"`
	Title     string `json:"title"`
	TitleLink string `json:"title_link"`
	Text      string `json:"text"`
	ImageURL  string `json:"image_url"`
}

Attachment defines the message output after running the slash command.

type Configuration

type Configuration struct {
	Table string `json:"TABLE"`       // Database URL
	Token string `json:"SLACK_TOKEN"` // Slack token provided by the app for verification
	Area  string `json:"AREA"`        // IANA-compliant area

	// This defines the API keys for accessing the Twitter API
	// and get messages from the tiny-care bots
	TwitterConsumerKey    string `json:"TWITTER_CONSUMER_KEY"`
	TwitterConsumerSecret string `json:"TWITTER_CONSUMER_SECRET"`
	TwitterAccessKey      string `json:"TWITTER_ACCESS_KEY"`
	TwitterAccessSecret   string `json:"TWITTER_ACCESS_SECRET"`
}

Configuration contains all important settings for running the command.

func ReadConfiguration

func ReadConfiguration(path string) (*Configuration, error)

ReadConfiguration reads the configuration file and returns an instance of a Configuration.

Example
// Read config from a file
config, err := ReadConfiguration("path/to/config.json")
if err != nil {
	log.Fatal(err)
}

fmt.Printf("%v", config)
Output:

func (*Configuration) WriteConfiguration

func (cfg *Configuration) WriteConfiguration(path string) error

WriteConfiguration creates a configuration file at a given output path.

Example
// Create a sample configuration
config := &Configuration{
	Table: "bq://my-project.my-dataset.my-table",
	Token: "M4KY3LOVPIhE9E2zIMAz0QUE",
	Area:  "Asia/Manila",
}

err := config.WriteConfiguration("path/to/config.json")
if err != nil {
	log.Fatal(err)
}
Output:

type DBInserter

type DBInserter interface {
	InsertDB(item LogItem) error // Insert a log into the Database
}

DBInserter is an interface for storing barometer logs.

func NewDBInserter

func NewDBInserter(dburl string) (DBInserter, error)

NewDBInserter creates a DBInserter based on the detected scheme of the URL.

type LogItem

type LogItem struct {
	Timestamp     time.Time
	UserID        string
	Measure       int
	Notes         string
	TwitterClient *twitter.Client
}

LogItem is the user log for the barometer. This also serves as the schema for the database.

func (*LogItem) Insert

func (i *LogItem) Insert(db DBInserter) error

Insert puts the item entry into the specified database.

func (*LogItem) Reply

func (i *LogItem) Reply() (*Message, error)

Reply prepares the Slack message as a response to a slash command.

func (*LogItem) Save

func (i *LogItem) Save() (map[string]bigquery.Value, string, error)

Save allows us to implement BigQuery's ValueSaver interface.

type Message

type Message struct {
	ResponseType string       `json:"response_type"`
	Text         string       `json:"text"`
	Attachments  []Attachment `json:"attachments"`
}

Message is the Slack message event. see https://api.slack.com/docs/message-formatting for more information.

func UpdateLog

func UpdateLog(userID, text string, timestamp time.Time, db DBInserter, twitterClient *twitter.Client, debug bool) (*Message, error)

UpdateLog accepts the userID and the text, parses the timestamp, and stores it into the database. If debug is true, then log is not inserted into the database. This option is useful for testing.

Example
// Prepare inputs for updating the log
userID := "W012A3CDE"
text := "4 Had dinner with friends today!"
message, err := UpdateLog(userID, text, time.Now(), nil, nil, true) // Run in debug-mode
if err != nil {
	log.Fatalf("cannot update log, err: %v", err)
}
fmt.Println(message.Text)
Output:

Gotcha, I logged your mood: 4 (Had dinner with friends today!)

type Server

type Server struct {
	Port   int
	Router *httprouter.Router
	Config *Configuration

	// If true, then message will not insert into the database. Useful for testing.
	Debug bool
	// contains filtered or unexported fields
}

Server handles all requests coming from the Slack client.

func (*Server) Routes

func (s *Server) Routes()

Routes contain all handler functions that respond to GET or POST requests.

func (*Server) Start

func (s *Server) Start() error

Start command starts a server on a specific port.

Jump to

Keyboard shortcuts

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