core

package
v0.0.0-...-9f76edf Latest Latest
Warning

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

Go to latest
Published: May 6, 2022 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Package core provides core functionalities for mybot.

Index

Constants

View Source
const (
	TwitterEventType = "Twitter"
	SlackEventType   = "Slack"
)

Variables

This section is empty.

Functions

func CheckSlackError

func CheckSlackError(err error) bool

func CheckTwitterError

func CheckTwitterError(err error) bool

func TwitterStatusURL

func TwitterStatusURL(t anaconda.Tweet) string

Types

type ActionEvent

type ActionEvent struct {
	Action data.Action
	Data   interface{}
}

func NewActionEvent

func NewActionEvent(action data.Action, data interface{}) ActionEvent

func (ActionEvent) String

func (e ActionEvent) String() string

type Config

type Config interface {
	utils.Savable
	utils.Loadable
	utils.Deletable
	GetProperties() ConfigProperties
	GetTwitterScreenNames() []string
	GetTwitterTimelines() []TimelineConfig
	GetTwitterTimelinesByScreenName(screenName string) []TimelineConfig
	SetTwitterTimelines(timelines []TimelineConfig)
	AddTwitterTimeline(timeline TimelineConfig)
	GetTwitterFavorites() []FavoriteConfig
	SetTwitterFavorites(favorites []FavoriteConfig)
	AddTwitterFavorite(favorite FavoriteConfig)
	GetTwitterSearches() []SearchConfig
	SetTwitterSearches(searches []SearchConfig)
	AddTwitterSearch(search SearchConfig)
	GetPollingDuration() string
	SetPollingDuration(dur string)
	GetSlackMessages() []MessageConfig
	SetSlackMessages(msgs []MessageConfig)
	AddSlackMessage(msg MessageConfig)
	Validate() error
	ValidateWithAPI(api models.TwitterAPI) error
	Unmarshal(ext string, bytes []byte) error
	Marshal(ext string) ([]byte, error)
}

Config provides a set of funtions to access cofiguration of this application.

type ConfigProperties

type ConfigProperties struct {

	// Twitter is a configuration related to Twitter.
	Twitter TwitterConfig `json:"twitter" toml:"twitter" bson:"twitter" yaml:"twitter"`
	// Slack is a configuration related to Slack
	Slack SlackConfig `json:"slack" toml:"slack" bson:"slack" yaml:"slack"`
	// Duration is a duration for some periodic jobs such as fetching
	// users' favorites and searching by the specified condition.
	Duration string `json:"duration" toml:"duration" bson:"duration" yaml:"duration"`
	// contains filtered or unexported fields
}

ConfigProperties represents a collection of Config properties. All functions of this struct are thread-safe.

func (*ConfigProperties) AddSlackMessage

func (c *ConfigProperties) AddSlackMessage(msg MessageConfig)

func (*ConfigProperties) AddTwitterFavorite

func (c *ConfigProperties) AddTwitterFavorite(favorite FavoriteConfig)

func (*ConfigProperties) AddTwitterSearch

func (c *ConfigProperties) AddTwitterSearch(search SearchConfig)

func (*ConfigProperties) AddTwitterTimeline

func (c *ConfigProperties) AddTwitterTimeline(timeline TimelineConfig)

func (*ConfigProperties) GetPollingDuration

func (c *ConfigProperties) GetPollingDuration() string

func (*ConfigProperties) GetProperties

func (c *ConfigProperties) GetProperties() ConfigProperties

GetProperties returns a copy of ConfigProperties itself.

func (*ConfigProperties) GetSlackMessages

func (c *ConfigProperties) GetSlackMessages() []MessageConfig

GetSlackMessages returns a copy of Slack message configuration list.

func (*ConfigProperties) GetTwitterFavorites

func (c *ConfigProperties) GetTwitterFavorites() []FavoriteConfig

GetTwitterFavorites returns a copy of Twitter favorite configuration list.

func (*ConfigProperties) GetTwitterScreenNames

func (c *ConfigProperties) GetTwitterScreenNames() []string

GetTwitterScreenNames returns a list of all screen names in Twitter configuration.

func (*ConfigProperties) GetTwitterSearches

func (c *ConfigProperties) GetTwitterSearches() []SearchConfig

GetTwitterSearches returns a copy of Twitter search configuration list.

func (*ConfigProperties) GetTwitterTimelines

func (c *ConfigProperties) GetTwitterTimelines() []TimelineConfig

GetTwitterTimelines returns a copy of Twitter timeline configuration list.

func (*ConfigProperties) GetTwitterTimelinesByScreenName

func (c *ConfigProperties) GetTwitterTimelinesByScreenName(screenName string) []TimelineConfig

GetTwitterTimelinesByScreenName returns timeline configurations including a specified screen name.

func (*ConfigProperties) Marshal

func (c *ConfigProperties) Marshal(ext string) ([]byte, error)

Marshal returns a configuration content as a toml text. If error occurs while encoding, this returns an empty string. This return value is not same as the source file's content.

func (*ConfigProperties) SetPollingDuration

func (c *ConfigProperties) SetPollingDuration(dur string)

func (*ConfigProperties) SetSlackMessages

func (c *ConfigProperties) SetSlackMessages(msgs []MessageConfig)

func (*ConfigProperties) SetTwitterFavorites

func (c *ConfigProperties) SetTwitterFavorites(favorites []FavoriteConfig)

func (*ConfigProperties) SetTwitterSearches

func (c *ConfigProperties) SetTwitterSearches(searches []SearchConfig)

func (*ConfigProperties) SetTwitterTimelines

func (c *ConfigProperties) SetTwitterTimelines(timelines []TimelineConfig)

func (*ConfigProperties) Unmarshal

func (c *ConfigProperties) Unmarshal(ext string, bytes []byte) error

TODO: Make error message clearer

func (*ConfigProperties) Validate

func (c *ConfigProperties) Validate() error

Validate tries to validate the specified configuration. If invalid values are detected, this returns an error.

func (*ConfigProperties) ValidateWithAPI

func (c *ConfigProperties) ValidateWithAPI(api models.TwitterAPI) error

ValidateWithAPI validates ConfigProperties with external API access. This function is exclusive with Validate function.

type DBConfig

type DBConfig struct {
	ConfigProperties `yaml:",inline"`

	ID string `json:"id" toml:"id" bson:"id" yaml:"id"`
	// contains filtered or unexported fields
}

func NewDBConfig

func NewDBConfig(col models.MgoCollection, id string) (*DBConfig, error)

func (*DBConfig) Delete

func (c *DBConfig) Delete() error

func (*DBConfig) Load

func (c *DBConfig) Load() error

func (*DBConfig) Save

func (c *DBConfig) Save() error

type FavoriteConfig

type FavoriteConfig struct {
	Source                    `yaml:",inline"`
	models.SourceProperties   `yaml:",inline"`
	models.AccountProperties  `yaml:",inline"`
	models.FavoriteProperties `yaml:",inline"`
}

FavoriteConfig is a configuration for Twitter favorites

func NewFavoriteConfig

func NewFavoriteConfig() FavoriteConfig

NewFavoriteCnfig returns FavoriteConfig instance, which is empty but has a non-nil filter and action.

func (*FavoriteConfig) Validate

func (c *FavoriteConfig) Validate() error

type FileConfig

type FileConfig struct {
	ConfigProperties `yaml:",inline"`
	// source is a configuration file from which this was loaded. This is
	// needed to save the content to the same file.
	File string `json:"-" toml:"-" bson:"-" yaml:"-"`
}

FileConfig is a root of the all configurations of this applciation.

func NewFileConfig

func NewFileConfig(path string) (*FileConfig, error)

NewFileConfig takes the configuration file path and returns a configuration instance.

func NewTestFileConfig

func NewTestFileConfig(path string, t *testing.T) *FileConfig

func (*FileConfig) Delete

func (c *FileConfig) Delete() error

func (*FileConfig) Load

func (c *FileConfig) Load() error

Load loads the configuration from the source file. If the specified source file doesn't exist, this method does nothing and returns nil.

func (*FileConfig) Save

func (c *FileConfig) Save() error

Save saves the specified configuration to the source file.

type Filter

type Filter struct {
	models.FilterProperties `yaml:",inline"`
	Patterns                []string                 `json:"patterns,omitempty" toml:"patterns,omitempty" bson:"patterns,omitempty" yaml:"patterns,omitempty"`
	URLPatterns             []string                 `json:"url_patterns,omitempty" toml:"url_patterns,omitempty" bson:"url_patterns,omitempty" yaml:"url_patterns,omitempty"`
	Vision                  models.VisionCondition   `json:"vision,omitempty" toml:"vision,omitempty" bson:"vision,omitempty" yaml:"vision,omitempty"`
	Language                models.LanguageCondition `json:"language,omitempty" toml:"language,omitempty" bson:"language,omitempty" yaml:"language,omitempty"`
}

Filter is a configuration to filter out tweets

func NewFilter

func NewFilter() Filter

func (Filter) CheckSlackMsg

func (c Filter) CheckSlackMsg(
	ev *slack.MessageEvent,
	v VisionMatcher,
	l LanguageMatcher,
	cache data.Cache,
) (bool, error)

func (Filter) CheckTweet

func (c Filter) CheckTweet(
	t anaconda.Tweet,
	v VisionMatcher,
	l LanguageMatcher,
	cache data.Cache,
) (bool, error)

func (Filter) ShouldRepeat

func (c Filter) ShouldRepeat() bool

func (Filter) Validate

func (f Filter) Validate() error

type LanguageAPI

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

func (*LanguageAPI) Enabled

func (a *LanguageAPI) Enabled() bool

func (*LanguageAPI) MatchText

func (a *LanguageAPI) MatchText(
	text string,
	cond models.LanguageCondition,
) (string, bool, error)

type LanguageMatcher

type LanguageMatcher interface {
	MatchText(string, models.LanguageCondition) (string, bool, error)
	Enabled() bool
}

func NewLanguageMatcher

func NewLanguageMatcher(file string) (LanguageMatcher, error)

type MessageConfig

type MessageConfig struct {
	Source   `yaml:",inline"`
	Channels []string `json:"channels" toml:"channels" bson:"channels" yaml:"channels"`
}

func NewMessageConfig

func NewMessageConfig() MessageConfig

func (MessageConfig) Validate

func (c MessageConfig) Validate() error

type ReceivedEvent

type ReceivedEvent struct {
	Type string
	Name string
	Data interface{}
}

func NewReceivedEvent

func NewReceivedEvent(typ, name string, data interface{}) ReceivedEvent

func (ReceivedEvent) String

func (e ReceivedEvent) String() string

type SearchConfig

type SearchConfig struct {
	Source                  `yaml:",inline"`
	models.SourceProperties `yaml:",inline"`
	models.SearchProperties `yaml:",inline"`
}

SearchConfig is a configuration for Twitter searches

func NewSearchConfig

func NewSearchConfig() SearchConfig

NewSearchConfig returns SearchConfig instance, which is empty but has a non-nil filter and action.

func (*SearchConfig) Validate

func (c *SearchConfig) Validate() error

type SlackAPI

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

func NewSlackAPI

func NewSlackAPI(api models.SlackAPI, config Config, cache data.Cache) *SlackAPI

func NewSlackAPIWithAuth

func NewSlackAPIWithAuth(token string, config Config, cache data.Cache) *SlackAPI

func (*SlackAPI) AuthTest

func (a *SlackAPI) AuthTest() (*slack.AuthTestResponse, error)

func (*SlackAPI) Enabled

func (a *SlackAPI) Enabled() bool

func (*SlackAPI) Listen

func (a *SlackAPI) Listen(vis VisionMatcher, lang LanguageMatcher, twitterAPI *TwitterAPI) *SlackListener

func (*SlackAPI) PostMessage

func (a *SlackAPI) PostMessage(channel, text string, channelIsOpen bool, opts ...slack.MsgOption) error

TODO: Prevent infinite message loop

func (*SlackAPI) PostTweet

func (a *SlackAPI) PostTweet(channel string, tweet anaconda.Tweet) error

type SlackConfig

type SlackConfig struct {
	Messages []MessageConfig `json:"messages" toml:"messages" bson:"messages" yaml:"messages"`
}

func NewSlackConfig

func NewSlackConfig() SlackConfig

type SlackListener

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

func (*SlackListener) Start

func (l *SlackListener) Start(ctx context.Context, outChan chan<- interface{}) (err error)

type SlackMsg

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

type Source

type Source struct {
	// Name is a label to identify each source
	Name string `json:"name,omitempty" toml:"name,omitempty" bson:"name,omitempty" yaml:"name,omitempty"`
	// Filter filters out incoming data from sources.
	Filter Filter `json:"filter" toml:"filter" bson:"filter" yaml:"filter"`
	// Action defines actions for data passing through filters.
	Action data.Action `json:"action" toml:"action" bson:"action" yaml:"action"`
}

Source is a configuration for common data sources such as Twitter's timelines, favorites and searches. Sources should have filters and actions.

func NewSource

func NewSource() Source

func (*Source) String

func (c *Source) String() string

func (*Source) Validate

func (c *Source) Validate() error

type TimelineConfig

type TimelineConfig struct {
	Source                    `yaml:",inline"`
	models.SourceProperties   `yaml:",inline"`
	models.AccountProperties  `yaml:",inline"`
	models.TimelineProperties `yaml:",inline"`
}

TimelineConfig is a configuration for Twitter timelines

func NewTimelineConfig

func NewTimelineConfig() TimelineConfig

NewTimelineConfig returns TimelineConfig instance, which is empty but has a non-nil filter and action.

func (*TimelineConfig) Validate

func (c *TimelineConfig) Validate() error

type TweetChecker

type TweetChecker interface {
	CheckTweet(t anaconda.Tweet, v VisionMatcher, l LanguageMatcher, c data.Cache) (bool, error)
	ShouldRepeat() bool
}

TweetChecker function checks if the specified tweet is acceptable, which means it should be retweeted.

type TwitterAPI

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

TwitterAPI is a wrapper of anaconda.TwitterApi.

func NewTwitterAPI

func NewTwitterAPI(api models.TwitterAPI, config Config, cache data.Cache) *TwitterAPI

func NewTwitterAPIWithAuth

func NewTwitterAPIWithAuth(auth oauth.OAuthCreds, config Config, cache data.Cache) *TwitterAPI

NewTwitterAPIWithAuth takes a user's authentication, cache and configuration and returns TwitterAPI instance for that user

func (*TwitterAPI) BaseAPI

func (a *TwitterAPI) BaseAPI() models.TwitterAPI

func (*TwitterAPI) CheckUser

func (a *TwitterAPI) CheckUser(user string, allowSelf bool, users []string) (bool, error)

CheckUser cheks if user is matched for the given allowSelf and users arguments.

func (*TwitterAPI) Enabled

func (a *TwitterAPI) Enabled() bool

func (*TwitterAPI) GetFavorites

func (a *TwitterAPI) GetFavorites(vals url.Values) ([]anaconda.Tweet, error)

func (*TwitterAPI) GetSearch

func (a *TwitterAPI) GetSearch(query string, url url.Values) (anaconda.SearchResponse, error)

func (*TwitterAPI) GetSelf

func (a *TwitterAPI) GetSelf() (anaconda.User, error)

GetSelf gets the authenticated user's information and stores it as a cache, then returns it.

func (*TwitterAPI) GetUserSearch

func (a *TwitterAPI) GetUserSearch(searchTerm string, v url.Values) ([]anaconda.User, error)

func (*TwitterAPI) ListenMyself

func (a *TwitterAPI) ListenMyself(v url.Values) (*TwitterDMListener, error)

ListenMyself listens to the authenticated user by Twitter's User Streaming API and reacts with direct messages.

func (*TwitterAPI) ListenUsers

func (a *TwitterAPI) ListenUsers(
	v url.Values,
	vis VisionMatcher,
	lang LanguageMatcher,
	slack *SlackAPI,
	cache data.Cache,
) (*TwitterUserListener, error)

ListenUsers listens timelines of the friends

func (*TwitterAPI) PostSlackMsg

func (a *TwitterAPI) PostSlackMsg(text string, atts []slack.Attachment) (anaconda.Tweet, error)

func (*TwitterAPI) ProcessFavorites

func (a *TwitterAPI) ProcessFavorites(
	name string,
	v url.Values,
	c TweetChecker,
	vision VisionMatcher,
	lang LanguageMatcher,
	slack *SlackAPI,
	action data.Action,
) ([]anaconda.Tweet, []data.Action, error)

ProcessFavorites gets tweets from the specified user's favorite list and do action for tweets filtered by c.

func (*TwitterAPI) ProcessSearch

func (a *TwitterAPI) ProcessSearch(
	query string,
	v url.Values,
	c TweetChecker,
	vision VisionMatcher,
	lang LanguageMatcher,
	slack *SlackAPI,
	action data.Action,
) ([]anaconda.Tweet, []data.Action, error)

ProcessSearch gets tweets from search result by the specified query and do action for tweets filtered by c.

func (*TwitterAPI) VerifyCredentials

func (a *TwitterAPI) VerifyCredentials() (bool, error)

type TwitterConfig

type TwitterConfig struct {
	// no usage but keep this due to backward compatibility
	Debug     bool             `json:"debug" toml:"debug" bson:"debug" yaml:"debug"`
	Timelines []TimelineConfig `json:"timelines" toml:"timelines" bson:"timelines" yaml:"timelines"`
	Favorites []FavoriteConfig `json:"favorites" toml:"favorites" bson:"favorites" yaml:"favorites"`
	Searches  []SearchConfig   `json:"searches" toml:"searches" bson:"searches" yaml:"searches"`
}

TwitterConfig is a configuration related to Twitter.

func NewTwitterConfig

func NewTwitterConfig() TwitterConfig

func (TwitterConfig) GetScreenNames

func (tc TwitterConfig) GetScreenNames() []string

GetScreenNames returns all screen names in the TwitterConfig instance. This is useful to do something for all related users.

type TwitterDMListener

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

func (*TwitterDMListener) Listen

func (l *TwitterDMListener) Listen(ctx context.Context, outChan chan<- interface{}) error

func (*TwitterDMListener) Stop

func (l *TwitterDMListener) Stop()

type TwitterPostProcessor

type TwitterPostProcessor interface {
	Process(anaconda.Tweet, bool) error
}

type TwitterPostProcessorEach

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

func (*TwitterPostProcessorEach) Process

func (p *TwitterPostProcessorEach) Process(t anaconda.Tweet, match bool) error

type TwitterPostProcessorTop

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

func (*TwitterPostProcessorTop) Process

func (p *TwitterPostProcessorTop) Process(t anaconda.Tweet, match bool) error

type TwitterUserListener

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

func (*TwitterUserListener) Listen

func (l *TwitterUserListener) Listen(ctx context.Context, outChan chan<- interface{}) error

func (*TwitterUserListener) Stop

func (l *TwitterUserListener) Stop()

type VisionAPI

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

VisionAPI is a wrapper of vision.Service.

func (*VisionAPI) Enabled

func (a *VisionAPI) Enabled() bool

func (*VisionAPI) MatchImages

func (a *VisionAPI) MatchImages(
	urls []string,
	cond models.VisionCondition,
	imgCaches []models.ImageCacheData,
) ([]string, []bool, error)

MatchImages takes image URLs and a Vision condition and returns whether the specified images match or not.

type VisionMatcher

type VisionMatcher interface {
	MatchImages([]string, models.VisionCondition, []models.ImageCacheData) ([]string, []bool, error)
	Enabled() bool
}

func NewVisionMatcher

func NewVisionMatcher(file string) (VisionMatcher, error)

NewVisionMatcher takes a path of a user's google-cloud credential file and returns a VisionAPI instance for that user.

Jump to

Keyboard shortcuts

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