flowbot

package module
v0.0.0-...-0ac6ab7 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2019 License: MIT Imports: 8 Imported by: 0

README

Flowbot - A Flowdock robot written in Go

GoDoc Build Status

This is a library to create a Flowdock robot which runs in Golang.

Overview

It uses the following Flowdock APIs:

  • Stream API (to read content on a given flow),
  • Push API (to push chat message on the flow or a team inbox notification).

Installation

$ go get -u github.com/eko/flowbot

Run the robot

$ go run app.go
Connecting to Flowdock stream https://stream.flowdock.com/flows/[...]/[...]
Connected! Start reading stream...
-> Command found: flow uptime
-> Chat message correctly sent.

A robot example application

This sample application answers to the following commands:

  • flow uptime: Renders the server uptime,
  • flow image : Render the first image found on Google Image corresponding to
package main

import (
    "github.com/eko/flowbot"
    "fmt"
    "io/ioutil"
    "net/http"
    "os/exec"
    "regexp"
)

func main() {
    flowbot.FlowdockFlowToken = "<YOUR FLOW TOKEN API KEY>"
    flowbot.FlowdockStreamUrl = "https://stream.flowdock.com/flows/<YOUR-ORGANIZATION>/<YOUR-FLOW-NAME>"
    flowbot.FlowdockAuthUsername = "<YOUR EMAIL ADDRESS>"
    flowbot.FlowdockAuthPassword = "<YOUR PASSWORD>"

    flowbot.AddCommand("flow uptime", func (command flowbot.Command, entry flowbot.Entry) {
        output, err := exec.Command("uptime").Output()
        if err != nil { panic(err) }

        flowbot.SendChat(fmt.Sprintf("My uptime: %s", output))
    })

    flowbot.AddCommand("flow image (.*)", func (command flowbot.Command, entry flowbot.Entry) {
        query := command.Pattern.FindStringSubmatch(entry.Content)[1]

        response, err := http.Get(fmt.Sprintf("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=%s", query))
        if err != nil { panic(err) }

        body, _ := ioutil.ReadAll(response.Body)

        imagePattern := regexp.MustCompile(`(http[s]?://[a-zA-Z0-9\/\-\.\_\%]+.[gif|png|jpg|jpeg])`)
        image := imagePattern.FindStringSubmatch(string(body))[0]

        flowbot.SendChat(image)
    })

    flowbot.Stream()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	FlowdockRobotName string = "Flowbot"

	FlowdockChatUrl  string = "https://api.flowdock.com/v1/messages/chat/"
	FlowdockInboxUrl string = "https://api.flowdock.com/v1/messages/team_inbox/"

	FlowdockStreamUrl    string
	FlowdockFlowToken    string
	FlowdockAuthUsername string
	FlowdockAuthPassword string
)

Flowdock configuration values

Functions

func AddCommand

func AddCommand(pattern string, handler handler)

Adds a command

func SendChat

func SendChat(message string)

Send a chat message

func SendInbox

func SendInbox(source string, from_address string, subject string, content string)

Send a team inbox message to configured flowdock flow token

func SendThreadChat

func SendThreadChat(threadId string, message string)

Send a chat message to a thread

func Stream

func Stream()

Start streaming on Flowdock

Types

type Chat

type Chat struct {
	Content          string `json:"content"`
	ExternalUserName string `json:"external_user_name"`
	ThreadId         string `json:"thread_id,omitempty"`
}

A Flowdock chat message

type Command

type Command struct {
	Pattern *regexp.Regexp
	Handler handler
}

A command structure Streaming API will detect this command name on Flow and run the command handler

type Entry

type Entry struct {
	Event       string    `json:"event"`
	Tags        []string  `json:"tags"`
	Uuid        string    `json:"uuid"`
	Persist     bool      `json:"persist"`
	Id          int       `json:"id"`
	Flow        string    `json:"flow"`
	Content     string    `json:"content"`
	Sent        int       `json:"sent"`
	App         string    `json:"app"`
	CreatedAt   time.Time `json:"created_at"`
	Attachments []string  `json:"attachments"`
	User        string    `json:"user"`
	ThreadId    string    `json:"thread_id,omitempty"`
}

A Flowdock entry structure Each streamed response will be transformed into an event

Jump to

Keyboard shortcuts

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