keybaser

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2019 License: MIT Imports: 10 Imported by: 1

README

Keybaser CircleCI GoDoc codecov Go Report Card GolangCI

Built on top of the Keybase API github.com/keybase/go-keybase-chat-bot with the idea to simplify the Real-Time Messaging feature to easily create Keybase Bots, likely github.com/shomali11/slacker

Features

Features are almost same as github.com/shomali11/slacker

  • Easy definitions of commands and their input
  • Available bot initialization, errors and default handlers
  • Simple parsing of String, Integer, Float and Boolean parameters
  • Contains support for context.Context *Built-in help command
  • supports authorization
  • bot responds to mentions and direct messages
  • handlers run concurrently via goroutine
  • Full access to the Keybase API github.com/keybase/go-keybase-chat-bot

Dependencies

Requirements

Install

go get github.com/sawadashota/keybaser

Examples

Example 1

Defining a simple command

examples/simple

package main

import (
	"context"
	"log"

	"github.com/keybase/go-keybase-chat-bot/kbchat"
	"github.com/sawadashota/keybaser"
)

func main() {
	client, err := kbchat.Start(kbchat.RunOptions{
		Oneshot: &kbchat.OneshotOptions{
			Username: "<Bot's Username>",
			PaperKey: "<Bot's PaperKey>",
		},
	})
	if err != nil {
		log.Fatal(err)
	}

	bot, err := keybaser.New(client)
	if err != nil {
		log.Fatal(err)
	}

	definition := &keybaser.CommandDefinition{
		Handler: func(request keybaser.Request, response keybaser.ResponseWriter) {
			response.Reply("pong")
		},
	}

	bot.Command("ping", definition)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	if err := bot.Listen(ctx); err != nil {
		log.Fatal(err)
	}
}

Example 2

Defining a command with parameter, description and example

examples/parameter

package main

import (
	"context"
	"log"

	"github.com/keybase/go-keybase-chat-bot/kbchat"
	"github.com/sawadashota/keybaser"
)

func main() {
	client, err := kbchat.Start(kbchat.RunOptions{
		Oneshot: &kbchat.OneshotOptions{
			Username: "<Bot's Username>",
			PaperKey: "<Bot's PaperKey>",
		},
	})
	if err != nil {
		log.Fatal(err)
	}

	bot, err := keybaser.New(client)
	if err != nil {
		log.Fatal(err)
	}

	definition := &keybaser.CommandDefinition{
		Description: "Greet to you",
		Example:     "greet alice",
		Handler: func(request keybaser.Request, response keybaser.ResponseWriter) {
			name := request.Param("name")
			response.Reply("Hello " + name)
		},
	}

	bot.Command("greet <name>", definition)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	if err := bot.Listen(ctx); err != nil {
		log.Fatal(err)
	}
}

Example 3

Sample of running on Docker.

Keybaser doesn't work Go's binary alone because github.com/keybase/go-keybase-chat-bot requires keybase app. So this sample is using keybaseio/client as execution image. The image's Dockerfile is here

examples/docker

# Build Chat Bot app
FROM golang:1.13-buster as builder

WORKDIR /app

COPY . .

RUN go mod download && \
    go mod verify && \
    CGO_ENABLED=0 GO111MODULE=on GOOS=linux GOARCH=amd64 \
        go build \
        -o app \
        main.go

# Copy chat bot app binary to executor image
FROM keybaseio/client:nightly-slim

COPY --from=builder /app/app /usr/bin/app

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BotCommand

type BotCommand interface {
	Usage() string
	Definition() *CommandDefinition

	Match(text string) (*proper.Properties, bool)
	Execute(request Request, response ResponseWriter)
}

BotCommand interface

func NewBotCommand

func NewBotCommand(usage string, definition *CommandDefinition) BotCommand

NewBotCommand creates a new bot command object

type ClientOption

type ClientOption func(*Keybaser)

ClientOption is option client initialize

func SetMiddlewareFunc

func SetMiddlewareFunc(middlewareFunc func(next HandlerFunc) HandlerFunc) ClientOption

SetMiddlewareFunc is option for set custom middleware

func SetResponseConstructor

func SetResponseConstructor(constructor ResponseConstructor) ClientOption

SetResponseConstructor is option for set custom response constructor

func SetSubscriber

func SetSubscriber(subscriber Subscriber) ClientOption

SetSubscriber is option for set custom subscriber

func WithLogger

func WithLogger(logger logrus.FieldLogger) ClientOption

WithLogger is option for set custom logger

type CommandDefinition

type CommandDefinition struct {
	Description       string
	Example           string
	AuthorizationFunc func(request Request) bool
	Handler           func(request Request, response ResponseWriter)
}

CommandDefinition structure contains definition of the bot command

type HandlerFunc

type HandlerFunc func(ctx context.Context, message *kbchat.SubscriptionMessage)

HandlerFunc .

type KeybaseChatAPIClient

type KeybaseChatAPIClient interface {
	ListenForNewTextMessages() (kbchat.NewSubscription, error)
	SendMessage(channel chat1.ChatChannel, body string, args ...interface{}) (kbchat.SendResponse, error)
	GetUsername() string
}

KeybaseChatAPIClient .

type Keybaser

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

Keybaser contains the Keybase API

func New

func New(kc KeybaseChatAPIClient, opts ...ClientOption) (*Keybaser, error)

New creates a new client using the Keybase API

func (*Keybaser) Client

func (k *Keybaser) Client() KeybaseChatAPIClient

Client returns the internal keybase chat API

func (*Keybaser) Command

func (k *Keybaser) Command(usage string, definition *CommandDefinition)

Command define a new command and append it to the list of existing commands

func (*Keybaser) Listen

func (k *Keybaser) Listen(ctx context.Context) error

Listen receives events from Keybase and each is handled as needed

type NewSubscription

type NewSubscription interface {
	Read() (kbchat.SubscriptionMessage, error)
}

NewSubscription .

type Request

type Request interface {
	Param(key string) string
	StringParam(key string, defaultValue string) string
	BooleanParam(key string, defaultValue bool) bool
	IntegerParam(key string, defaultValue int) int
	FloatParam(key string, defaultValue float64) float64
	Context() context.Context
	Message() *kbchat.SubscriptionMessage
	Properties() *proper.Properties
}

Request interface that contains the Event received and parameters

func NewRequest

func NewRequest(ctx context.Context, message *kbchat.SubscriptionMessage, properties *proper.Properties) Request

NewRequest creates a new Request structure

type ResponseConstructor

type ResponseConstructor func(channel chat1.ChatChannel, client KeybaseChatAPIClient) ResponseWriter

ResponseConstructor .

type ResponseWriter

type ResponseWriter interface {
	Reply(text string)
	ReportError(err error)
	Client() KeybaseChatAPIClient
}

A ResponseWriter interface is used to respond to an event

func NewResponse

func NewResponse(channel chat1.ChatChannel, client KeybaseChatAPIClient) ResponseWriter

NewResponse creates a new response structure

type Subscriber

type Subscriber interface {
	Subscribe() (NewSubscription, error)
}

Subscriber .

Directories

Path Synopsis
examples
parameter command
simple command
docker module

Jump to

Keyboard shortcuts

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