gitter

package
v4.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package gitter provides a sarah.Adapter implementation for Gitter integration.

Index

Constants

View Source
const (
	// GITTER is a dedicated sarah.BotType for Gitter integration.
	GITTER sarah.BotType = "gitter"
)
View Source
const (
	// RestAPIEndpoint defines base url of Gitter REST API.
	RestAPIEndpoint = "https://api.gitter.im/"
)
View Source
const (
	// StreamingAPIEndpointFormat defines the basic URL format of Gitter streaming API.
	StreamingAPIEndpointFormat = "https://stream.gitter.im/%s/rooms/%s/chatMessages"
)
View Source
const (
	// TimeFormat defines the Gitter-styled timestamp format.
	// https://golang.org/pkg/time/#Time.Format
	TimeFormat = "2006-01-02T15:04:05.999Z"
)

Variables

View Source
var (
	// ErrEmptyPayload is an error that represents an empty payload.
	ErrEmptyPayload = errors.New("empty payload was given")
)

Functions

func NewResponse

func NewResponse(content string, options ...RespOption) (*sarah.CommandResponse, error)

NewResponse creates *sarah.CommandResponse with the given arguments.

Types

type APIClient

type APIClient interface {
	// Rooms fetch the list of rooms the token's owner belongs.
	Rooms(context.Context) (*Rooms, error)

	// PostMessage sends message to a given Room.
	PostMessage(context.Context, *Room, string) (*Message, error)
}

APIClient is an interface that a Rest API client must satisfy. This is mainly defined to ease tests.

type Adapter

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

Adapter is a sarah.Adapter implementation for Gitter. This holds REST/Streaming API clients' instances.

func NewAdapter

func NewAdapter(config *Config, options ...AdapterOption) (*Adapter, error)

NewAdapter creates and returns a new Adapter instance.

func (*Adapter) BotType

func (adapter *Adapter) BotType() sarah.BotType

BotType returns a designated BotType for Gitter integration.

func (*Adapter) Run

func (adapter *Adapter) Run(ctx context.Context, enqueueInput func(sarah.Input) error, notifyErr func(error))

Run fetches all belonging Room information and connects to them. New goroutines are activated for each Room to connect, and the interactions run in a concurrent manner.

func (*Adapter) SendMessage

func (adapter *Adapter) SendMessage(ctx context.Context, output sarah.Output)

SendMessage lets sarah.Bot send a message to Gitter.

type AdapterOption

type AdapterOption func(adapter *Adapter)

AdapterOption defines a function's signature that Adapter's functional options must satisfy.

type Config

type Config struct {
	// Token declares the API token to integrate with Gitter.
	Token string `json:"token" yaml:"token"`

	// RetryPolicy declares how a retrial for an API call should behave.
	RetryPolicy *retry.Policy `json:"retry_policy" yaml:"retry_policy"`
}

Config contains some configuration variables for Gitter Adapter.

func NewConfig

func NewConfig() *Config

NewConfig creates and returns a new Config instance with default settings. Token is empty at this point as there can not be a default value. Use json.Unmarshal, yaml.Unmarshal, or manual manipulation to populate the blank value or override those default values.

type Connection

type Connection interface {
	MessageReceiver
	io.Closer
}

Connection defines an interface that satisfies both MessageReceiver and io.Closer.

type Issue

type Issue struct {
	Number uint `json:"number"`
}

Issue represents issue number mentioned in a message.

type MalformedPayloadError

type MalformedPayloadError struct {
	// Err tells the error reason.
	Err string
}

MalformedPayloadError represents an error that a given JSON payload is not properly formatted. e.g. required fields are not given, or payload is not a valid JSON string.

func NewMalformedPayloadError

func NewMalformedPayloadError(str string) *MalformedPayloadError

NewMalformedPayloadError creates a new MalformedPayloadError instance with the given error message.

func (*MalformedPayloadError) Error

func (e *MalformedPayloadError) Error() string

Error returns its error message.

type Mention

type Mention struct {
	ScreenName string `json:"screenName"`
	UserID     string `json:"userId"`
}

Mention represents a mention in the message.

type Message

type Message struct {
	ID            string    `json:"id"`
	Text          string    `json:"text"`
	HTML          string    `json:"html"`
	SendTimeStamp TimeStamp `json:"sent"`
	EditTimeStamp TimeStamp `json:"editedAt"`
	FromUser      User      `json:"fromUser"`
	Unread        bool      `json:"unread"`
	ReadBy        uint      `json:"readBy"`
	URLs          []string  `json:"urls"`
	Mentions      []Mention `json:"mentions"`
	Issues        []Issue   `json:"issues"`
	Meta          []Meta    `json:"meta"` // Reserved, but not in use
	Version       uint      `json:"v"`
}

Message represents Gitter's message resource. https://developer.gitter.im/docs/messages-resource

type MessageReceiver

type MessageReceiver interface {
	// Receive reads a new incoming message and return this as RoomMessage.
	// This blocks till a new message comes.
	Receive() (*RoomMessage, error)
}

MessageReceiver defines an interface that receives RoomMessage over Streaming API.

type Meta

type Meta struct {
}

Meta is reserved, but is not used so far. https://developer.gitter.im/docs/messages-resource

type PostingMessage

type PostingMessage struct {
	Text string `json:"text"`
}

PostingMessage represents the sending message. This can be marshaled and be sent as a JSON-styled payload.

type RespOption

type RespOption func(*respOptions)

RespOption defines a function's signature that NewResponse's functional option must satisfy.

func RespWithNext

func RespWithNext(fnc sarah.ContextualFunc) RespOption

RespWithNext sets a given fnc as part of the response's *sarah.UserContext. The next input from the same user will be passed to this fnc. sarah.UserContextStorage must be configured or otherwise, the function will be ignored.

func RespWithNextSerializable

func RespWithNextSerializable(arg *sarah.SerializableArgument) RespOption

RespWithNextSerializable sets the given arg as part of the response's *sarah.UserContext. The next input from the same user will be passed to the function defined in the arg. sarah.UserContextStorage must be configured or otherwise, the function will be ignored.

type RestAPIClient

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

RestAPIClient utilizes Gitter REST API.

func NewRestAPIClient

func NewRestAPIClient(token string) *RestAPIClient

NewRestAPIClient creates and returns a new API client instance. The version is fixed to v1.

func NewVersionSpecificRestAPIClient

func NewVersionSpecificRestAPIClient(token string, apiVersion string) *RestAPIClient

NewVersionSpecificRestAPIClient creates a new API client instance with the given API version.

func (*RestAPIClient) Get

func (client *RestAPIClient) Get(ctx context.Context, resourceFragments []string, intf interface{}) error

Get sends an HTTP GET request with the given path and parameters.

func (*RestAPIClient) Post

func (client *RestAPIClient) Post(ctx context.Context, resourceFragments []string, sendingPayload interface{}, responsePayload interface{}) error

Post sends an HTTP POST request to Gitter with the given parameters.

func (*RestAPIClient) PostMessage

func (client *RestAPIClient) PostMessage(ctx context.Context, room *Room, text string) (*Message, error)

PostMessage sends a message to Gitter.

func (*RestAPIClient) Rooms

func (client *RestAPIClient) Rooms(ctx context.Context) (*Rooms, error)

Rooms fetches belonging rooms' information.

type Room

type Room struct {
	ID             string    `json:"id"`
	Name           string    `json:"name"`
	Topic          string    `json:"topic"`
	URI            string    `json:"uri"`
	OneToOne       bool      `json:"oneToOne"`
	Users          []*User   `json:"users"`
	UnreadItems    uint      `json:"unreadItems"`
	Mentions       uint      `json:"mentions"`
	LastAccessTime TimeStamp `json:"lastAccessTime"`
	Favourite      uint      `json:"favourite"`
	Lurk           bool      `json:"lurk"`
	URL            string    `json:"url"`        // path
	GitHubType     string    `json:"githubType"` // TODO type
	Tags           []string  `json:"tags"`
	Version        uint      `json:"v"`
}

Room represents Gitter's room resource. https://developer.gitter.im/docs/rooms-resource

type RoomMessage

type RoomMessage struct {
	// Room represents where the message was sent.
	Room *Room

	// ReceivedMessage represents the received message.
	ReceivedMessage *Message
}

RoomMessage is a sarah.Input implementation that represents a received message.

func NewRoomMessage

func NewRoomMessage(room *Room, message *Message) *RoomMessage

NewRoomMessage creates and returns a new RoomMessage instance.

func (*RoomMessage) Message

func (message *RoomMessage) Message() string

Message returns the received text.

func (*RoomMessage) ReplyTo

func (message *RoomMessage) ReplyTo() sarah.OutputDestination

ReplyTo returns the Room the message was sent.

func (*RoomMessage) SenderKey

func (message *RoomMessage) SenderKey() string

SenderKey returns the message sender's id.

func (*RoomMessage) SentAt

func (message *RoomMessage) SentAt() time.Time

SentAt returns when the message is sent.

type Rooms

type Rooms []*Room

Rooms represents a group of Room instances.

type RoomsFetcher

type RoomsFetcher interface {
	// Rooms fetch the list of rooms the token's owner belongs.
	Rooms(context.Context) (*Rooms, error)
}

RoomsFetcher defines an interface that fetches Gitter rooms.

type StreamConnector

type StreamConnector interface {
	// Connect connects to the given Gitter room and returns the Connection.
	Connect(context.Context, *Room) (Connection, error)
}

StreamConnector defines an interface that connects to the given Gitter room.

type StreamingAPIClient

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

StreamingAPIClient utilizes Gitter streaming API.

func NewStreamingAPIClient

func NewStreamingAPIClient(token string) *StreamingAPIClient

NewStreamingAPIClient creates and returns a new Streaming API client instance. The API version is fixed to v1.

func NewVersionSpecificStreamingAPIClient

func NewVersionSpecificStreamingAPIClient(apiVersion string, token string) *StreamingAPIClient

NewVersionSpecificStreamingAPIClient creates and returns a new Streaming API client instance.

func (*StreamingAPIClient) Connect

func (client *StreamingAPIClient) Connect(ctx context.Context, room *Room) (Connection, error)

Connect initiates a connection to the Streaming API server and returns an established Connection.

type StreamingClient

type StreamingClient interface {
	Connect(context.Context, *Room) (Connection, error)
}

StreamingClient is an interface that an HTTP Streaming client must satisfy. This is mainly defined to ease tests.

type TimeStamp

type TimeStamp struct {
	// Time is the time.Time representation of the timestamp.
	Time time.Time

	// OriginalValue is the original timestamp value given by Gitter.
	OriginalValue string // e.g. "2014-03-24T15:41:18.991Z"
}

TimeStamp represents the timestamp when its belonging event occurred.

func (*TimeStamp) MarshalText

func (timeStamp *TimeStamp) MarshalText() ([]byte, error)

MarshalText marshals TimeStamp to a Gitter-styled one.

func (*TimeStamp) String

func (timeStamp *TimeStamp) String() string

String returns the original Gitter-styled timestamp value.

func (*TimeStamp) UnmarshalText

func (timeStamp *TimeStamp) UnmarshalText(b []byte) error

UnmarshalText unmarshals the given Gitter-styled timestamp to TimeStamp.

type User

type User struct {
	ID              string `json:"id"`
	UserName        string `json:"username"`
	DisplayName     string `json:"displayName"`
	URL             string `json:"url"` // path
	AvatarURLSmall  string `json:"avatarUrlSmall"`
	AvatarURLMedium string `json:"avatarUrlMedium"`
}

User represents Gitter's user resource. https://developer.gitter.im/docs/user-resource

Jump to

Keyboard shortcuts

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