bot

package
v1.11.6 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var PermanentBanDuration = time.Hour * 24 * 400

PermanentBanDuration defines duration of permanent ban: If user is restricted for more than 366 days or less than 30 seconds from the current time, they are considered to be restricted forever.

Functions

func DisplayName

func DisplayName(msg Message) string

DisplayName returns user's display name or username or id

Types

type Detector added in v0.2.0

type Detector interface {
	Check(request spamcheck.Request) (spam bool, cr []spamcheck.Response)
	LoadSamples(exclReader io.Reader, spamReaders, hamReaders []io.Reader) (tgspam.LoadResult, error)
	LoadStopWords(readers ...io.Reader) (tgspam.LoadResult, error)
	UpdateSpam(msg string) error
	UpdateHam(msg string) error
	AddApprovedUser(user approved.UserInfo) error
	RemoveApprovedUser(id string) error
	ApprovedUsers() (res []approved.UserInfo)
	IsApprovedUser(userID string) bool
}

Detector is a spam detector interface

type Entity

type Entity struct {
	Type   string
	Offset int
	Length int
	URL    string `json:",omitempty"` // For “text_link” only, url that will be opened after user taps on the text
	User   *User  `json:",omitempty"` // For “text_mention” only, the mentioned user
}

Entity represents one special entity in a text message. For example, hashtags, usernames, URLs, etc.

type Image

type Image struct {
	// FileID corresponds to Telegram file_id
	FileID   string
	Width    int
	Height   int
	Caption  string    `json:",omitempty"`
	Entities *[]Entity `json:",omitempty"`
}

Image represents image

type Message

type Message struct {
	ID         int
	From       User
	SenderChat SenderChat `json:"sender_chat,omitempty"`
	ChatID     int64
	Sent       time.Time
	HTML       string    `json:",omitempty"`
	Text       string    `json:",omitempty"`
	Entities   *[]Entity `json:",omitempty"`
	Image      *Image    `json:",omitempty"`
	ReplyTo    struct {
		From       User
		Text       string `json:",omitempty"`
		Sent       time.Time
		SenderChat SenderChat `json:"sender_chat,omitempty"`
	} `json:",omitempty"`
}

Message is primary record to pass data from/to bots

type Response

type Response struct {
	Text          string
	Send          bool                 // status
	BanInterval   time.Duration        // bots banning user set the interval
	User          User                 // user to ban
	ChannelID     int64                // channel to ban, if set then User and BanInterval are ignored
	ReplyTo       int                  // message to reply to, if 0 then no reply but common message
	DeleteReplyTo bool                 // delete message what bot replays to
	CheckResults  []spamcheck.Response // check results for the message
}

Response describes bot's reaction on particular message

type SampleUpdater added in v0.2.0

type SampleUpdater struct {
	// contains filtered or unexported fields
}

SampleUpdater represents a file that can be read and appended to. this is a helper for dynamic reloading of samples used by SpamFilter

func NewSampleUpdater added in v0.2.0

func NewSampleUpdater(fileName string) *SampleUpdater

NewSampleUpdater creates a new SampleUpdater

func (*SampleUpdater) Append added in v0.2.0

func (s *SampleUpdater) Append(msg string) error

Append a message to the file, preventing duplicates

func (*SampleUpdater) Reader added in v0.2.0

func (s *SampleUpdater) Reader() (io.ReadCloser, error)

Reader returns a reader for the file, caller must close it

type SenderChat

type SenderChat struct {
	// ID is a unique identifier for this chat
	ID int64 `json:"id"`
	// the field below used only for logging purposes
	// UserName for private chats, supergroups and channels if available, optional
	UserName string `json:"username,omitempty"`
}

SenderChat is the sender of the message, sent on behalf of a chat. The channel itself for channel messages. The supergroup itself for messages from anonymous group administrators. The linked channel for messages automatically forwarded to the discussion group

type SpamConfig added in v0.2.0

type SpamConfig struct {

	// samples file names need to be watched for changes and reload.
	SpamSamplesFile    string
	HamSamplesFile     string
	StopWordsFile      string
	ExcludedTokensFile string
	SpamDynamicFile    string
	HamDynamicFile     string

	SpamMsg    string
	SpamDryMsg string

	WatchDelay time.Duration

	Dry bool
}

SpamConfig is a full set of parameters for spam bot

type SpamFilter

type SpamFilter struct {
	Detector
	// contains filtered or unexported fields
}

SpamFilter bot checks if a user is a spammer using lib.Detector Reloads spam samples, stop words and excluded tokens on file change.

func NewSpamFilter

func NewSpamFilter(ctx context.Context, detector Detector, params SpamConfig) *SpamFilter

NewSpamFilter creates new spam filter

func (*SpamFilter) AddApprovedUser added in v1.6.0

func (s *SpamFilter) AddApprovedUser(id int64, name string) error

AddApprovedUser adds users to the list of approved users, to both the detector and the storage

func (*SpamFilter) DynamicSamples added in v1.5.0

func (s *SpamFilter) DynamicSamples() (spam, ham []string, err error)

DynamicSamples returns dynamic spam and ham samples. both are optional

func (*SpamFilter) IsApprovedUser added in v1.6.0

func (s *SpamFilter) IsApprovedUser(userID int64) bool

IsApprovedUser checks if user is in the list of approved users

func (*SpamFilter) OnMessage

func (s *SpamFilter) OnMessage(msg Message) (response Response)

OnMessage checks if user already approved and if not checks if user is a spammer

func (*SpamFilter) ReloadSamples added in v0.2.0

func (s *SpamFilter) ReloadSamples() (err error)

ReloadSamples reloads samples and stop-words

func (*SpamFilter) RemoveApprovedUser added in v1.6.0

func (s *SpamFilter) RemoveApprovedUser(id int64) error

RemoveApprovedUser removes users from the list of approved users in both the detector and the storage

func (*SpamFilter) RemoveDynamicHamSample added in v1.5.0

func (s *SpamFilter) RemoveDynamicHamSample(sample string) (int, error)

RemoveDynamicHamSample removes a sample from the ham dynamic samples file and reloads samples after this

func (*SpamFilter) RemoveDynamicSpamSample added in v1.5.0

func (s *SpamFilter) RemoveDynamicSpamSample(sample string) (int, error)

RemoveDynamicSpamSample removes a sample from the spam dynamic samples file and reloads samples after this

func (*SpamFilter) UpdateHam added in v0.2.0

func (s *SpamFilter) UpdateHam(msg string) error

UpdateHam appends a message to the ham samples file and updates the classifier

func (*SpamFilter) UpdateSpam added in v0.2.0

func (s *SpamFilter) UpdateSpam(msg string) error

UpdateSpam appends a message to the spam samples file and updates the classifier

type User

type User struct {
	ID          int64  `json:"id"`
	Username    string `json:"user_name,omitempty"`
	DisplayName string `json:"display_name,omitempty"`
}

User defines user info of the Message

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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