bot

package
v0.0.0-...-740805f Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2024 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MESSAGE_TYPE_CAST_ADD = "MESSAGE_TYPE_CAST_ADD"
)

Variables

View Source
var (
	// ErrAPINotSet is returned when the API is not set in the bot configuration.
	ErrAPINotSet = fmt.Errorf("api not set")
	// ErrParsingPoll is returned when there is an error parsing the poll during
	// the poll message handler.
	ErrParsingPoll = fmt.Errorf("error parsing poll")
	// ErrGettingUserData is returned when there is an error getting user data
	// during the poll message handler.
	ErrGettingUserData = fmt.Errorf("error getting user data")
	// ErrReplyingToCast is returned when there is an error replying to a cast
	// during the reply with poll URL function.
	ErrReplyingToCast = fmt.Errorf("error replying to cast")
	// ErrGettingParentCast is returned when there is an error getting the parent
	// cast during the mute notifications message handler.
	ErrGettingParentCast = fmt.Errorf("error getting parent cast")
)
View Source
var PollReplyTemplate = `` /* 216-byte string literal not displayed */

PollReplyTemplate is the template for the reply to a cast with a poll. It must be formatted with the poll URL.

Functions

This section is empty.

Types

type Bot

type Bot struct {
	UserData *farcasterapi.Userdata

	Messages chan *farcasterapi.APIMessage
	// contains filtered or unexported fields
}

Bot struct represents a bot that listens for new casts and sends them to a channel, it also has a cool down time to avoid spamming the API and a last cast timestamp to retrieve new casts from that point, ensuring no cast is missed or duplicated

func New

func New(config BotConfig) (*Bot, error)

New function creates a new bot with the given configuration, it returns an error if the API is not set in the configuration.

func (*Bot) PollMessageHandler

func (b *Bot) PollMessageHandler(ctx context.Context, msg *farcasterapi.APIMessage, maxDuration time.Duration) (*farcasterapi.Userdata, *poll.Poll, bool, error)

PollMessageHandler is a function that handles a new cast and checks if it is a poll, if it is, it parses the poll and returns the user data and the poll. If it is not a poll, it returns false. It returns an error if something goes wrong, including when message parsing fails.

func (*Bot) ReplyWithPollURL

func (b *Bot) ReplyWithPollURL(ctx context.Context, msg *farcasterapi.APIMessage, pollURL string) error

func (*Bot) Start

func (b *Bot) Start(ctx context.Context)

Start function starts the bot, it listens for new casts and sends them to the Messages channel. It does this in a goroutine to avoid blocking the main and every cool down time.

func (*Bot) Stop

func (b *Bot) Stop()

Stop function stops the bot and its goroutine, and closes the Messages channel.

type BotConfig

type BotConfig struct {
	API      farcasterapi.API
	CoolDown time.Duration
}

BotConfig is the configuration definition for the bot, it includes the API instance and the cool down time between casts (default is 10 seconds)

type CastAddBody

type CastAddBody struct {
	Text      string `json:"text"`
	ParentURL string `json:"parentUrl"`
}

CastAddBody represents the body of a cast add message.

type Message

type Message struct {
	Data    *MessageData `json:"data"`
	HexHash string       `json:"hash"`
}

Message represents a message from the API, it includes the data and the hash of the message.

func (*Message) Author

func (m *Message) Author() uint64

Author returns the author of the message.

func (*Message) Hash

func (m *Message) Hash() ([]byte, error)

Timestamp returns the timestamp of the message.

func (*Message) IsMention

func (m *Message) IsMention() bool

IsMention returns true if the message is a mention.

func (*Message) Mention

func (m *Message) Mention() string

Mention returns the text of a cast.

type MessageData

type MessageData struct {
	Type        string       `json:"type"`
	From        uint64       `json:"fid"`
	Timestamp   uint64       `json:"timestamp"`
	CastAddBody *CastAddBody `json:"castAddBody,omitempty"`
}

MessageData represents the data of a message.

Directories

Path Synopsis
poll package contains the logic to parse a poll message from a string and create a poll struct with the question, options and duration.
poll package contains the logic to parse a poll message from a string and create a poll struct with the question, options and duration.

Jump to

Keyboard shortcuts

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