lowbot

package module
v1.9.5 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2024 License: MIT Imports: 16 Imported by: 3

README ΒΆ

lowbot

πŸ€– LowBot is a Go project on GitHub that simplifies bot creation by automatically generating bot journeys from a YAML script. It offers extensive customization options, making it highly versatile and adaptable.

πŸ€” Why LowBot?

  • πŸ“š Simple and Self-Explanatory Scripting:

    LowBot follows a straightforward and intuitive YAML script format, making it easy to understand and modify for creating bot journeys.

  • 🧩 Extensibility for Custom Business Rules:

    LowBot offers extensive flexibility, allowing users to define their own custom actions and implement unique business rules tailored to their specific requirements.

  • πŸ“‘ Expandable Bot Channels:

    With LowBot, users can seamlessly integrate and implement their own channels, empowering them to connect the bot with various platforms and communication channels of their choice.

πŸ“¦ Install

go get github.com/chrissgon/lowbot

πŸš€ Quick Start

Start a bot with Telegram and local persist

import "github.com/chrissgon/lowbot"

flow, _ := lowbot.NewFlow("your_flow.yaml")
channel, _ := lowbot.NewTelegram("your_telegram_token")
persist, _ := lowbot.NewLocalPersist()

lowbot.StartBot(flow, channel, persist)

Start a bot with Discord and custom actions

import "github.com/chrissgon/lowbot"

myActions := lowbot.ActionsMap{
    "Custom": func(flow *lowbot.Flow, channel lowbot.IChannel) (bool, error) {
        // your rules
        wait := true
        return wait, nil
    },
}

lowbot.SetCustomActions(myActions)

flow, _ := lowbot.NewFlow("your_flow.yaml")
channel, _ := lowbot.NewDiscord()
persist, _ := lowbot.NewLocalPersist()

lowbot.StartBot(flow, channel, persist)

You can create your Channel or Persist by implementing their interfaces

import "github.com/chrissgon/lowbot"

func MyNewChannel () lowbot.IChannel {
  // implements Channel
}

func MyNewPersist () lowbot.Persist {
  // implements Persist
}

flow, _ := lowbot.NewFlow("your_flow.yaml")

// And you can pass in the StartBot
lowbot.StartBot(flow, MyNewChannel(), MyNewPersist())

πŸ“š Documentation

Read all documentation in docs folder.

🌎 Global Variables

When you run this project, you can adjusts the following environment variables.

Debug: Show debug => default: true

EnableLocalPersist: Enable local persist => default: true

πŸ”’ Environment Variables

When you run this project, you need set the following environment variables.

  • When you to use Telegram:
    TELEGRAM_TOKEN: Telegram bot token.
  • When you to use Discord:
    DISCORD_TOKEN: Discord bot token.

πŸ’ͺ🏻 Contribution

This project is open source and welcomes community contributions. Feel free to fork, implement improvements, and submit a pull request. Every contribution is valued and appreciated!

We hope that lowbot proves useful to you and enhances your manga reading experience. Feel free to explore the source code, provide feedback, and report any issues you encounter.

❀️ Authors

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

View Source
const (
	CHANNEL_TELEGRAM_NAME = "telegram"
	CHANNEL_DISCORD_NAME  = "discord"
	CONSUMER_JOURNEY_NAME = "journey"
	CONSUMER_CHATGPT_NAME = "chatgpt"
)

Variables ΒΆ

View Source
var (
	DEBUG                           = false
	ERR_UNKNOWN_TELEGRAM_TOKEN      = errors.New("unknown telegram token")
	ERR_UNKNOWN_DISCORD_TOKEN       = errors.New("unknown discord token")
	ERR_UNKNOWN_CHATGPT_TOKEN       = errors.New("unknown chatgpt token")
	ERR_CONNECT_CHATGPT             = errors.New("connect to chatgpt failed")
	ERR_UNDEFINED_CHATGPT_ASSISTANT = errors.New("undefined chatgpt assistant")
	ERR_UNKNOWN_ACTION              = errors.New("unknown action")
	ERR_NIL_FLOW                    = errors.New("nil flow")
	ERR_NIL_STEP                    = errors.New("nil step")
	ERR_UNKNOWN_DEFAULT_STEP        = errors.New("unknown step: default")
	ERR_UNKNOWN_INIT_STEP           = errors.New("unknown step: init")
	ERR_UNKNOWN_NEXT_STEP           = errors.New("unknown next step")
	ERR_PATTERN_NEXT_STEP           = errors.New("step pattern invalid")
)
View Source
var (
	FILETYPE_AUDIO_EXT = []string{".aac", ".mp3", ".oga", ".opus", ".wav", ".weba", ".cda"}
	FILETYPE_IMAGE_EXT = []string{".apng", ".avif", ".gif", ".jpg", ".jpeg", ".png", ".svg", ".webp"}
	FILETYPE_VIDEO_EXT = []string{".avi", ".mp4", ".mpeg", ".ogv", ".webm"}
)

Functions ΒΆ

func ActionButton ΒΆ

func ActionButton(flow *Flow, interaction *Interaction, channel IChannel) (bool, error)

func ActionFile ΒΆ added in v1.7.1

func ActionFile(flow *Flow, interaction *Interaction, channel IChannel) (bool, error)

func ActionInput ΒΆ

func ActionInput(flow *Flow, interaction *Interaction, channel IChannel) (bool, error)

func ActionText ΒΆ

func ActionText(flow *Flow, interaction *Interaction, channel IChannel) (bool, error)

func ActionWait ΒΆ added in v1.5.2

func ActionWait(flow *Flow, interaction *Interaction, channel IChannel) (bool, error)

func FormatTestError ΒΆ

func FormatTestError(expect, have any) string

func IsURL ΒΆ

func IsURL(str string) bool

func ParseTemplate ΒΆ

func ParseTemplate(texts []string) string

func RunAction ΒΆ

func RunAction(flow *Flow, interaction *Interaction, channel IChannel) (bool, error)

func SetCustomActions ΒΆ added in v1.1.0

func SetCustomActions(custom ActionsMap)

func StartConsumer ΒΆ added in v1.7.1

func StartConsumer(consumer IConsumer, channels []IChannel)

Types ΒΆ

type ActionsMap ΒΆ

type ActionsMap map[string]func(*Flow, *Interaction, IChannel) (bool, error)

type Channel ΒΆ

type Channel struct {
	ChannelID uuid.UUID
	Name      string
}

type ChatGPTAssistantConsumer ΒΆ added in v1.8.0

type ChatGPTAssistantConsumer struct {
	*Consumer
	// contains filtered or unexported fields
}

func (*ChatGPTAssistantConsumer) GetConsumer ΒΆ added in v1.9.5

func (consumer *ChatGPTAssistantConsumer) GetConsumer() *Consumer

func (*ChatGPTAssistantConsumer) Run ΒΆ added in v1.8.0

func (consumer *ChatGPTAssistantConsumer) Run(interaction *Interaction, channel IChannel) error

type ChatGPTConsumer ΒΆ added in v1.7.1

type ChatGPTConsumer struct {
	*Consumer
	// contains filtered or unexported fields
}

func (*ChatGPTConsumer) GetConsumer ΒΆ added in v1.9.5

func (consumer *ChatGPTConsumer) GetConsumer() *Consumer

func (*ChatGPTConsumer) Run ΒΆ added in v1.7.1

func (consumer *ChatGPTConsumer) Run(interaction *Interaction, channel IChannel) error

type Consumer ΒΆ added in v1.7.1

type Consumer struct {
	ConsumerID uuid.UUID
	Name       string
}

type DiscordChannel ΒΆ added in v1.7.1

type DiscordChannel struct {
	*Channel
	// contains filtered or unexported fields
}

func (*DiscordChannel) GetChannel ΒΆ added in v1.7.1

func (channel *DiscordChannel) GetChannel() *Channel

func (*DiscordChannel) Next ΒΆ added in v1.7.1

func (channel *DiscordChannel) Next(interaction chan *Interaction)

func (*DiscordChannel) RespondInteraction ΒΆ added in v1.7.1

func (channel *DiscordChannel) RespondInteraction(in *discordgo.Interaction)

func (*DiscordChannel) SendAudio ΒΆ added in v1.7.1

func (channel *DiscordChannel) SendAudio(interaction *Interaction) error

func (*DiscordChannel) SendButton ΒΆ added in v1.7.1

func (channel *DiscordChannel) SendButton(interaction *Interaction) error

func (*DiscordChannel) SendDocument ΒΆ added in v1.7.1

func (channel *DiscordChannel) SendDocument(interaction *Interaction) error

func (*DiscordChannel) SendFile ΒΆ added in v1.7.1

func (channel *DiscordChannel) SendFile(sessionID, text, path string) error

func (*DiscordChannel) SendImage ΒΆ added in v1.7.1

func (channel *DiscordChannel) SendImage(interaction *Interaction) error

func (*DiscordChannel) SendText ΒΆ added in v1.7.1

func (channel *DiscordChannel) SendText(interaction *Interaction) error

func (*DiscordChannel) SendVideo ΒΆ added in v1.7.1

func (channel *DiscordChannel) SendVideo(interaction *Interaction) error

type File ΒΆ added in v1.7.1

type File struct {
	FileID    uuid.UUID
	FileType  FileType
	Bytes     []byte
	Path      string
	Extension string
	Err       error
}

func (*File) GetFile ΒΆ added in v1.7.1

func (file *File) GetFile() *File

func (*File) IsAudio ΒΆ added in v1.7.1

func (file *File) IsAudio() bool

func (*File) IsDocument ΒΆ added in v1.7.1

func (file *File) IsDocument() bool

func (*File) IsImage ΒΆ added in v1.7.1

func (file *File) IsImage() bool

func (*File) IsVideo ΒΆ added in v1.7.1

func (file *File) IsVideo() bool

func (*File) Read ΒΆ added in v1.7.1

func (file *File) Read() error

func (*File) SetFilePath ΒΆ added in v1.7.1

func (file *File) SetFilePath(path string)

func (*File) SetFileType ΒΆ added in v1.7.1

func (file *File) SetFileType()

type FileType ΒΆ added in v1.7.1

type FileType string
const (
	FILETYPE_AUDIO    FileType = "FILETYPE_AUDIO"
	FILETYPE_DOCUMENT FileType = "FILETYPE_DOCUMENT"
	FILETYPE_IMAGE    FileType = "FILETYPE_IMAGE"
	FILETYPE_VIDEO    FileType = "FILETYPE_VIDEO"
)

type Flow ΒΆ

type Flow struct {
	FlowID  uuid.UUID
	Name    string `yaml:"name"`
	Steps   Steps  `yaml:"steps"`
	Current *Step
}

func NewFlow ΒΆ

func NewFlow(path string) (*Flow, error)

func (*Flow) Next ΒΆ

func (flow *Flow) Next(interaction *Interaction) error

func (*Flow) NoHasNext ΒΆ added in v1.4.0

func (flow *Flow) NoHasNext() bool

func (*Flow) Start ΒΆ

func (flow *Flow) Start() error

type FlowPersist ΒΆ added in v1.7.1

type FlowPersist interface {
	Set(any, *Flow) error
	Get(any) (*Flow, error)
}

func NewMemoryFlowPersist ΒΆ added in v1.7.1

func NewMemoryFlowPersist() (FlowPersist, error)

type IChannel ΒΆ added in v1.7.1

type IChannel interface {
	GetChannel() *Channel
	Next(chan *Interaction)
	SendAudio(*Interaction) error
	SendButton(*Interaction) error
	SendDocument(*Interaction) error
	SendImage(*Interaction) error
	SendText(*Interaction) error
	SendVideo(*Interaction) error
}

func NewDiscordChannel ΒΆ added in v1.7.1

func NewDiscordChannel(token string) (IChannel, error)

func NewTelegramChannel ΒΆ added in v1.7.1

func NewTelegramChannel(token string) (IChannel, error)

type IConsumer ΒΆ added in v1.7.1

type IConsumer interface {
	Run(*Interaction, IChannel) error
	GetConsumer() *Consumer
}

func NewChatGPTAssistantConsumer ΒΆ added in v1.8.0

func NewChatGPTAssistantConsumer(token string, assistantID string) (IConsumer, error)

func NewChatGPTConsumer ΒΆ added in v1.7.1

func NewChatGPTConsumer(token string, model string) (IConsumer, error)

func NewJourneyConsumer ΒΆ added in v1.7.1

func NewJourneyConsumer(flow *Flow, persist FlowPersist) IConsumer

type IFile ΒΆ added in v1.7.1

type IFile interface {
	GetFile() *File
	Read() error
	IsAudio() bool
	IsDocument() bool
	IsImage() bool
	IsVideo() bool
}

func NewFile ΒΆ added in v1.7.1

func NewFile(path string) IFile

type Interaction ΒΆ

type Interaction struct {
	Channel    *Channel
	Sender     *Who
	Replier    *Who
	Type       InteractionType
	Parameters InteractionParameters
	Custom     map[string]any
}

func NewInteractionMessageButton ΒΆ

func NewInteractionMessageButton(channel IChannel, sender *Who, buttons []string, text string) *Interaction

func NewInteractionMessageFile ΒΆ added in v1.7.1

func NewInteractionMessageFile(channel IChannel, sender *Who, path string, text string) *Interaction

func NewInteractionMessageText ΒΆ

func NewInteractionMessageText(channel IChannel, sender *Who, text string) *Interaction

func (*Interaction) SetReplier ΒΆ added in v1.7.1

func (interaction *Interaction) SetReplier(replier *Who) *Interaction

type InteractionParameters ΒΆ

type InteractionParameters struct {
	Buttons []string
	File    IFile
	Text    string
	Custom  map[string]any
}

type InteractionType ΒΆ

type InteractionType string
const (
	MESSAGE_BUTTON InteractionType = "MESSAGE_BUTTON"
	MESSAGE_FILE   InteractionType = "MESSAGE_FILE"
	MESSAGE_TEXT   InteractionType = "MESSAGE_TEXT"
	EVENT_TYPING   InteractionType = "EVENT_TYPING"
)

type JourneyConsumer ΒΆ added in v1.7.1

type JourneyConsumer struct {
	*Consumer
	Flow    *Flow
	Persist FlowPersist
}

func (*JourneyConsumer) GetConsumer ΒΆ added in v1.9.5

func (consumer *JourneyConsumer) GetConsumer() *Consumer

func (*JourneyConsumer) Run ΒΆ added in v1.7.1

func (consumer *JourneyConsumer) Run(interaction *Interaction, channel IChannel) error

type MemoryFlowPersist ΒΆ added in v1.7.1

type MemoryFlowPersist struct {
	Sessions map[any]*Flow
}

func (*MemoryFlowPersist) Get ΒΆ added in v1.7.1

func (memory *MemoryFlowPersist) Get(ID any) (*Flow, error)

func (*MemoryFlowPersist) Set ΒΆ added in v1.7.1

func (memory *MemoryFlowPersist) Set(ID any, flow *Flow) error

type Room ΒΆ added in v1.7.1

type Room struct {
	RoomID       uuid.UUID
	Interactions []*Interaction
}

type Step ΒΆ

type Step struct {
	Action     string            `yaml:"action"`
	Next       map[string]string `yaml:"next"`
	Parameters StepParameters    `yaml:"parameters"`
	Responses  []*Interaction
}

func (*Step) AddResponse ΒΆ added in v1.4.0

func (step *Step) AddResponse(interaction *Interaction)

func (*Step) GetLastResponse ΒΆ added in v1.4.2

func (step *Step) GetLastResponse() *Interaction

func (*Step) GetLastResponseText ΒΆ added in v1.4.2

func (step *Step) GetLastResponseText() string

type StepParameters ΒΆ

type StepParameters struct {
	Buttons []string       `yaml:"buttons"`
	Path    string         `yaml:"path"`
	Text    string         `yaml:"text"`
	Texts   []string       `yaml:"texts"`
	Custom  map[string]any `yaml:"custom"`
}

type Steps ΒΆ

type Steps map[string]*Step

type TelegramChannel ΒΆ added in v1.7.1

type TelegramChannel struct {
	*Channel
	// contains filtered or unexported fields
}

func (*TelegramChannel) GetChannel ΒΆ added in v1.7.1

func (channel *TelegramChannel) GetChannel() *Channel

func (*TelegramChannel) Next ΒΆ added in v1.7.1

func (channel *TelegramChannel) Next(interaction chan *Interaction)

func (*TelegramChannel) SendAudio ΒΆ added in v1.7.1

func (channel *TelegramChannel) SendAudio(interaction *Interaction) error

func (*TelegramChannel) SendButton ΒΆ added in v1.7.1

func (channel *TelegramChannel) SendButton(interaction *Interaction) error

func (*TelegramChannel) SendDocument ΒΆ added in v1.7.1

func (channel *TelegramChannel) SendDocument(interaction *Interaction) error

func (*TelegramChannel) SendImage ΒΆ added in v1.7.1

func (channel *TelegramChannel) SendImage(interaction *Interaction) error

func (*TelegramChannel) SendText ΒΆ added in v1.7.1

func (channel *TelegramChannel) SendText(interaction *Interaction) error

func (*TelegramChannel) SendVideo ΒΆ added in v1.7.1

func (channel *TelegramChannel) SendVideo(interaction *Interaction) error

type Who ΒΆ added in v1.7.1

type Who struct {
	WhoID  any
	Name   string
	Custom map[string]any
}

func NewWho ΒΆ added in v1.7.1

func NewWho(whoID any, name string) *Who

Directories ΒΆ

Path Synopsis
docs
examples/hello command

Jump to

Keyboard shortcuts

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