Options approach for creating new CwtchBot #2

Open
decentral1se wants to merge 3 commits from decentral1se/cwtchbot:options-refactor into main
2 changed files with 23 additions and 19 deletions

View File

@ -1,7 +1,7 @@
# Cwtch Bot Framework # Cwtch Bot Framework
A specialized Cwtch Bot framework in Go that provides a more lightweight and tailored approach to building chat bots for Cwtch. A specialized Cwtch Bot framework in Go that provides a more lightweight and tailored approach to building chat bots for Cwtch.
For an introduction to building chatbots with the CwtchBot framework check out [the building an echobot tutorial](https://docs.cwtch.im/developing/building-a-cwtch-app/building-an-echobot). For an introduction to building chatbots with the CwtchBot framework check out [the building an echobot tutorial](https://docs.cwtch.im/developing/building-a-cwtch-app/building-an-echobot).
If you'd like to get involved please open an issue, or submit a pull request :) If you'd like to get involved please open an issue, or submit a pull request :)

38
bot.go
View File

@ -2,21 +2,22 @@ package bot
import ( import (
"crypto/rand" "crypto/rand"
"cwtch.im/cwtch/app"
"cwtch.im/cwtch/event"
"cwtch.im/cwtch/peer"
"cwtch.im/cwtch/protocol/connections"
"cwtch.im/cwtch/settings"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"git.openprivacy.ca/openprivacy/connectivity"
"git.openprivacy.ca/openprivacy/connectivity/tor"
"git.openprivacy.ca/openprivacy/log"
mrand "math/rand" mrand "math/rand"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"time" "time"
"cwtch.im/cwtch/app"
"cwtch.im/cwtch/event"
"cwtch.im/cwtch/peer"
"cwtch.im/cwtch/protocol/connections"
"cwtch.im/cwtch/settings"
"git.openprivacy.ca/openprivacy/connectivity"
"git.openprivacy.ca/openprivacy/connectivity/tor"
"git.openprivacy.ca/openprivacy/log"
) )
type CwtchBot struct { type CwtchBot struct {
@ -29,21 +30,24 @@ type CwtchBot struct {
experiments []string experiments []string
} }
func NewCwtchBot(userdir string, peername string) *CwtchBot { // CwtchBotOption modifies a new CwtchBot.
cb := new(CwtchBot) type CwtchBotOption func(c *CwtchBot)
cb.dir = userdir
cb.peername = peername // WithExperiments specifies the experiments option of a CwtchBot.
cb.engineHooks = connections.DefaultEngineHooks{} func WithExperiments(experiments []string) CwtchBotOption {
cb.experiments = nil return func(c *CwtchBot) {
return cb c.experiments = experiments
}
} }
func NewCwtchBotWithExperiments(userdir string, peername string, experiments []string) *CwtchBot { func NewCwtchBot(userdir, peername string, opts ...CwtchBotOption) *CwtchBot {
cb := new(CwtchBot) cb := new(CwtchBot)
cb.dir = userdir cb.dir = userdir
cb.peername = peername cb.peername = peername
cb.engineHooks = connections.DefaultEngineHooks{} cb.engineHooks = connections.DefaultEngineHooks{}
cb.experiments = experiments for _, optFunc := range opts {
optFunc(cb)
}
return cb return cb
} }