Documentation ¶
Index ¶
- Constants
- Variables
- func FormatBranch(branch string) string
- func FormatName(name string) string
- func FormatRepo(repo string) string
- func FormatSHA(sha string) string
- func FormatSize(size int) string
- func FormatURL(url string) string
- func ShortenURL(longURL string) (string, error)
- type Bot
- func (bot *Bot) CreateHook()
- func (bot *Bot) GetCommand(msg string) string
- func (bot *Bot) HandlePullHook(event hookserve.Event)
- func (bot *Bot) HandlePushHook(event hookserve.Event)
- func (bot *Bot) JoinRoom(room string)
- func (bot *Bot) ListenForHooks()
- func (bot *Bot) LoadCommands()
- func (bot *Bot) LogIn(challstr Message)
- func (bot *Bot) MainLoop()
- func (bot *Bot) ParseMessage(msg Message)
- func (bot *Bot) ParseRawMessage(rawMsg string) []Message
- func (bot *Bot) QueueMessage(text, room string)
- func (bot *Bot) Receive()
- func (bot *Bot) RunCommand(msg Message)
- func (bot *Bot) Send()
- func (bot *Bot) SendMessage(msg string)
- func (bot *Bot) Start()
- type Config
- type Message
- type ShortURLError
Constants ¶
const ( GitIOBase = "http://git.io/" GitIOCreate = "http://git.io/create" // template for push messages // [repo] user pushed number new commits? to branch: URL PushTemplate = "[%s] %s pushed %s new commit%s to %s: %s" // template for commit messages // repo/branch SHA user: commit message CommitTemplate = "%s/%s %s %s: %s" // template for pull request messages // [repo] user action pull request #number: message (upstream...base) URL PullReqTemplate = "[%s] %s %s pull request #%s: %s (%s...%s) %s" )
const (
BufferSize = 4096
)
const (
GitHubBaseURL = "https://github.com/"
)
Variables ¶
var ( LoginUrl = "https://play.pokemonshowdown.com/action.php" IdRegex = regexp.MustCompile("[^a-z0-9]+") )
var ( GitSHARegex = regexp.MustCompile("^[a-f0-9]{7,40}$") GitLineRegex = regexp.MustCompile("(#L?)?([0-9]+)") )
var (
ErrShortenURL = errors.New("could not shorten the URL")
)
var PingTicker *time.Ticker
Functions ¶
func FormatBranch ¶
FormatBranch formats a branch for !htmlbox using #9C009C.
func FormatName ¶
FormatName formats a name for !htmlbox using #7F7F7F. Note that it uses a different colour to the IRC version due to PS' background colour.
func FormatRepo ¶
FormatRepo formats a repo name for !htmlbox using #FF00FF.
func FormatSize ¶
FormatSize formats an event size for !htmlbox using <strong>.
func ShortenURL ¶
Utility to shorten a URL using http://git.io/ Returns an empty string and ErrShortenURL if something goes wrong, otherwise returns the shortened URL and nil
Types ¶
type Bot ¶
type Bot struct {
// contains filtered or unexported fields
}
func CreateBot ¶
Creates and returns a bot using the given configuration, loading the commands in commands.go
func (*Bot) CreateHook ¶
func (bot *Bot) CreateHook()
Generates the GitHub webhook receiver and starts a goroutine to deal with received events
func (*Bot) GetCommand ¶
Gets the command name from a message, if there is one. If not, returns the empty string.
func (*Bot) HandlePullHook ¶
Sends messages to all relevant rooms updating them when a pull_request event is received. Still in beta
func (*Bot) HandlePushHook ¶
Sends messages to all relevant rooms updating them when a push event is received. Tells how many commits were pushed, and gives a description of each individual commit, as given in the commit message
func (*Bot) JoinRoom ¶
Causes the bot to join the given room and record when it joined in bot.config.Rooms
func (*Bot) ListenForHooks ¶
func (bot *Bot) ListenForHooks()
Listens for GitHub webhook events and delegates them to handlers, such as `HandlePushHook`. Currently only push and pull_request events are supported
func (*Bot) LoadCommands ¶
func (bot *Bot) LoadCommands()
Loads the commands that are specified within the function. A command can then be called using `bot.commands["name"](msg)`.
Messages will have arguments of the following form:
{user, args, command name[, timestamp]}
func (*Bot) LogIn ¶
Log in to PS! under the given name and password. See PS! documentation if you want to understand exactly what is required for login.
func (*Bot) MainLoop ¶
func (bot *Bot) MainLoop()
Begins the main loop of the bot, which keeps it running indefinitely (or until it crashes, since I haven't given it any form of crash handling yet.)
func (*Bot) ParseMessage ¶
Parses a non-raw message and determines what action to take in reponse. Currently most messages are ignored.
func (*Bot) ParseRawMessage ¶
Takes a single raw message from PS! and breaks it up into individual messages to respond to, returning a slice of Messages to be dealt with by the main parser.
func (*Bot) QueueMessage ¶
Adds a message for the given room to the outgoing queue. If the message is a PM, the room should be of the form "user:name", and the message will automatically get sent as a PM, so there is no need to add "/pm user, " to the front.
func (*Bot) Receive ¶
func (bot *Bot) Receive()
Receives messages from PS and queues them up to be handled. Use as a goroutine or otherwise it will loop infinitely.
func (*Bot) RunCommand ¶
Checks if the given command exists and executes the function it refers to if it does. Otherwise ignores the command.
func (*Bot) Send ¶
func (bot *Bot) Send()
Reads messages from the out queue and sends them to PS, one each 0.5s or so to avoid the chat queue at the PS end filling up and blocking more messages
func (*Bot) SendMessage ¶
Sends a queued message through the websocket connection
type Config ¶
type Config struct { /**** General config ****/ // The nickname to use on PS!. Limited to 16 characters Nick string // The password associated with the given nick. Blank if // the nick is unregistered Pass string // The server to connect to. PS! main's server is sim.smogon.com Server string // The port the given server uses. Default should be 8000 Port string // The websocket URL to connect to. Generate automatically from // the given settings using Config.GenerateURL URL *url.URL // The character that indicates that the message received is for the bot // to respond to. TODO: add validation for command char CommandChar string // The rooms the bot is in. Initially loaded from the config file, and // updated whenever the bot joins a room. Rooms map[string]int64 /**** Git config ****/ // Whether or not the bot should listen for webhooks EnableHooks bool // The port that the bot should listen on for incoming GitHub webhooks HookPort int // The secret given during the creation of the webhook. Must match the // secret on GitHub HookSecret string // A list of rooms to update when a webhook is received HookRooms []string // Aliases for .git, of the form alias: user/repo GitAliases map[string]string }
func GetConfig ¶
func GetConfig() Config
Reads the bot's config from file and converts it to a Config object for use by a Bot.
func (*Config) GenerateURL ¶
func (conf *Config) GenerateURL()
Generates a websocket URL to use for connecting, based on the given parameters. The websocket URL has the following format:
ws://server:port/showdown/websocket
type Message ¶
type Message struct {
// contains filtered or unexported fields
}
A Message struct is a simpler way of dealing with the message data from the messages PS! sends, as it contains all the information that is needed in a much easier to access form than a raw message from PS!. They can be created by passing a raw message into `NewMessage`, along with the room the message was received in.
func NewMessage ¶
Creates a Message object for the given message with all required information for parsing it and responding later.
type ShortURLError ¶
Error encountered when a URL can't be shortened using a URL shortener such as git.io or goo.gl
func (*ShortURLError) Error ¶
func (e *ShortURLError) Error() string