Skip to content

sawadashota/keybaser

Repository files navigation

Keybaser GitHub Actions GoDoc codecov Go Report Card GolangCI License: MIT

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