Documentation ¶
Overview ¶
Example ¶
package main import ( "context" "os" "time" qrterminal "github.com/mdp/qrterminal/v3" "go.uber.org/zap" "moul.io/u" "berty.tech/berty/v2/go/pkg/bertybot" ) func main() { logger, _ := zap.NewDevelopment() ctx, cancel := context.WithCancel(context.Background()) defer cancel() // init bot bot, _ := bertybot.New( bertybot.WithLogger(logger.Named("botlib")), // configure a logger bertybot.WithDisplayName("example bot"), // bot name bertybot.WithInsecureMessengerGRPCAddr("127.0.0.1:9091"), // connect to running berty messenger daemon bertybot.WithSkipReplay(), // skip old events, only consume fresh ones bertybot.WithSkipAcknowledge(), // skip acknowledge events bertybot.WithSkipMyself(), // skip my own interactions bertybot.WithRecipe(bertybot.DebugEventRecipe(logger.Named("debug"))), // debug events bertybot.WithRecipe(bertybot.DelayResponseRecipe(time.Second)), // add a delay before sending replies bertybot.WithRecipe(bertybot.AutoAcceptIncomingContactRequestRecipe()), // accept incoming contact requests bertybot.WithRecipe(bertybot.WelcomeMessageRecipe("welcome to example bot")), // send welcome message to new contacts and new conversations bertybot.WithRecipe(bertybot.EchoRecipe("you said: ")), // reply to messages with the same message bertybot.WithHandler(bertybot.UserMessageHandler, func(ctx bertybot.Context) { // custom handler ctx.ReplyString("hello world!") }), ) // display link and qr code logger.Info("retrieve instance Berty ID", zap.String("pk", bot.PublicKey()), zap.String("link", bot.BertyIDURL())) qrterminal.GenerateHalfBlock(bot.BertyIDURL(), qrterminal.L, os.Stdout) // signal handling go func() { u.WaitForCtrlC() cancel() }() // start bot bot.Start(ctx) }
Output:
Index ¶
- type Bot
- type CommandFn
- type Context
- type Handler
- type HandlerType
- type NewOption
- func WithCommand(name, description string, handler CommandFn) NewOption
- func WithDisplayName(name string) NewOption
- func WithHandler(typ HandlerType, handler Handler) NewOption
- func WithInsecureMessengerGRPCAddr(addr string) NewOption
- func WithLogger(logger *zap.Logger) NewOption
- func WithMessengerClient(client bertymessenger.MessengerServiceClient) NewOption
- func WithMessengerGRPCConn(cc *grpc.ClientConn) NewOption
- func WithRecipe(recipe Recipe) NewOption
- func WithSkipAcknowledge() NewOption
- func WithSkipMyself() NewOption
- func WithSkipReplay() NewOption
- type Recipe
- func AutoAcceptIncomingContactRequestRecipe() Recipe
- func AutoAcceptIncomingGroupInviteRecipe() Recipe
- func DebugEventRecipe(logger *zap.Logger) Recipe
- func DelayResponseRecipe(duration time.Duration) Recipe
- func EchoRecipe(prefix string) Recipe
- func SendErrorToClientRecipe() Recipe
- func WelcomeMessageRecipe(text string) Recipe
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bot ¶
type Bot struct {
// contains filtered or unexported fields
}
func (*Bot) BertyIDURL ¶
BertyIDURL returns the shareable Berty ID in the form of `https://berty.tech/id#xxx`.
type Context ¶
type Context struct { // common HandlerType HandlerType EventPayload proto.Message `json:"-"` // content of the payload is already available in the parsed payloads EventType bertymessenger.StreamEvent_Type Context context.Context Client bertymessenger.MessengerServiceClient Logger *zap.Logger IsReplay bool // whether the event is a replayed or a fresh event IsMe bool // whether the bot is the author IsAck bool // whether the event is an ack // parsed payloads, depending on the context Contact *bertymessenger.Contact `json:"Contact,omitempty"` Conversation *bertymessenger.Conversation `json:"Conversation,omitempty"` Interaction *bertymessenger.Interaction `json:"Interaction,omitempty"` Member *bertymessenger.Member `json:"Member,omitempty"` Account *bertymessenger.Account `json:"Account,omitempty"` Device *bertymessenger.Device `json:"Device,omitempty"` ConversationPK string `json:"ConversationPK,omitempty"` UserMessage string `json:"UserMessage,omitempty"` CommandArgs []string // contains filtered or unexported fields }
Context is the main argument passed to handlers.
func (*Context) ReplyString ¶
ReplyString sends a text message on the conversation related to the context. The conversation can be 1-1 or multi-member.
type HandlerType ¶
type HandlerType uint
const ( UnknownHandler HandlerType = iota ErrorHandler PreAnythingHandler PostAnythingHandler EndOfReplayHandler ContactUpdatedHandler InteractionUpdatedHandler ConversationUpdatedHandler AccountUpdatedHandler MemberUpdatedHandler DeviceUpdatedHandler NotificationHandler IncomingContactRequestHandler AcceptedContactHandler UserMessageHandler NewConversationHandler CommandHandler CommandNotFoundHandler )
type NewOption ¶
NewOption can be passed to the `New` function to configure the bot.
func WithCommand ¶
WithCommand registers a new command that can be called with the '/' prefix. If name was already used, the preview command is replaced by the new one.
func WithDisplayName ¶
WithDisplayName sets the name of the bot, by default, the name will be named "My Berty Bot".
func WithHandler ¶
func WithHandler(typ HandlerType, handler Handler) NewOption
WithHandler append a new Handler for the specified HandlerType.
func WithInsecureMessengerGRPCAddr ¶
WithInsecureMessengerGRPCAddr tries to open a new gRPC connection against the passed TCP address. It uses grpc.WithInsecure as dialer option and won't check any certificate.
func WithLogger ¶
WithLogger passes a configured zap Logger.
func WithMessengerClient ¶
func WithMessengerClient(client bertymessenger.MessengerServiceClient) NewOption
WithMessengerClient passes an already initialized messenger client.
func WithMessengerGRPCConn ¶
func WithMessengerGRPCConn(cc *grpc.ClientConn) NewOption
WithMessengerGRPCConn configures a new Messenger client from an already initialized gRPC connection.
func WithSkipAcknowledge ¶
func WithSkipAcknowledge() NewOption
WithSkipAcknowledge disables sending Acknowledge events.
func WithSkipMyself ¶
func WithSkipMyself() NewOption
WithSkipMyself disables sending events sent by myself.
func WithSkipReplay ¶
func WithSkipReplay() NewOption
WithSkipReplay will ignore all events that were already processed before starting to listen to Messenger events.
type Recipe ¶
type Recipe map[HandlerType][]Handler
Recipe is a set of handlers that performs common behaviors.
func AutoAcceptIncomingContactRequestRecipe ¶
func AutoAcceptIncomingContactRequestRecipe() Recipe
AutoAcceptIncomingContactRequestRecipe makes the bot "click" on the "accept" button automatically.
func AutoAcceptIncomingGroupInviteRecipe ¶
func AutoAcceptIncomingGroupInviteRecipe() Recipe
AutoAcceptIncomingContactRequestRecipe makes the bot "click" on the "accept" button automatically. NOT YET IMPLEMENTED.
func DebugEventRecipe ¶
DebugEventRecipe logs every event using zap.DebugLevel.
func DelayResponseRecipe ¶
DelayResponseRecipe will wait for the specified duration before handling an event.
func EchoRecipe ¶
EchoRecipe configures the bot to automatically reply any message with the same message. If a prefix is specified, the bot will prefix its response. If a prefix is specified, the bot will ignore incoming messages with that prefix (i.e., a conversation with multiple echo bots). The bot will skip incoming commands (messages starting with a '/').
func SendErrorToClientRecipe ¶
func SendErrorToClientRecipe() Recipe
SendErrorToClientRecipe will send internal errors to the related context (a contact or a conversation). NOT YET IMPLEMENTED.
func WelcomeMessageRecipe ¶
WelcomeMessageRecipe automatically sends a text message to new contacts.