ifttt

package module
v0.0.0-...-ced2318 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2018 License: Apache-2.0 Imports: 14 Imported by: 0

README

IFTTT

Documentation Build Status Go Report Card Coverage Status

Golang wrapper for the IFTTT Service API. Build your own IFTTT service as easily as registering an HTTP handler now!

This package is still under startup and expect breaking API changes without prior notifications

Examples

IFTTT-Netease

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrorPanicDuringProcess is returned to the server when a panic occurred while handling the request
	ErrorPanicDuringProcess = errors.New("Internal Error")
)

Functions

This section is empty.

Types

type Action

type Action interface {
	// Options is called during an action dynamic option request
	// Use req.FieldSlug for the field requested by this action.
	// If your action does not contain dynamic options, return nil, nil here.
	Options(req *Request) (*DynamicOption, error)
	// Handle is called then an applet triggered this action.
	// This is where you should check the validity of the request and handle the action.
	// If the request was unauthorized, return ifttt.ErrorInvalidToken
	// If the request cannot be performed due to a temporary error and you want IFTTT to retry this action later, set skip to false and return the error.
	// If there is a problem with the request that you cannot handle(eg: conflicting parameters), set skip to true and return the error, IFTTT will notify the user with the description of your error and give up.
	Handle(r *ActionHandleRequest, req *Request) (res *ActionResult, skip bool, err error)
}

Action is the interface which every registered action should implement.

type ActionHandleRequest

type ActionHandleRequest struct {
	// ActionFields are the parameters set in an applet
	ActionFields map[string]string
	// User contains the metadata of the IFTTT user (eg: timezone)
	User map[string]string
}

ActionHandleRequest describes a request to handle an action https://platform.ifttt.com/docs/api_reference#actions

type ActionResult

type ActionResult struct {
	// ID a string value which uniquely identifies the resource created or modified by the action.
	ID string
	// URL optional parameter, URL to the resource created or modified by the action.
	URL string
}

ActionResult returns the result of an activity https://platform.ifttt.com/docs/api_reference#actions

type AuthError

type AuthError struct {
	Message string
}

AuthError is returned as an error when the request failed to satisfy authentication requirements An optional Message can be defined to return to the user as an explanation of the error, defaults to "Token invalid"

func (AuthError) Error

func (c AuthError) Error() string

type DynamicOption

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

DynamicOption describes the result of a dynamic field options request. To created a nested OptionList, add the inner options as a *DynamicOption and pass it to the outer DynamicOption by (*DynamicOption).AddCategory

func (*DynamicOption) AddCategory

func (c *DynamicOption) AddCategory(name string, value *DynamicOption)

AddCategory adds a category item to the DynamicOption Note that the values inside the category could be selected but not the category itself.

func (*DynamicOption) AddString

func (c *DynamicOption) AddString(name string, value string)

AddString adds an option value labeled by name to the DynamicOption

type Notification

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

Notification is a wrapper for trigger realtime notifications Add userids and/or trigger identies to this struct and send this through ifttt.Service.Notify to inform IFTTT that these triggers/trigggers owned by these users has an update and IFTTT should poll for updates now. https://platform.ifttt.com/docs/api_reference#realtime-api

func (*Notification) AddTrigger

func (c *Notification) AddTrigger(triggerIdent string) error

AddTrigger adds a trigger identified by trigger identity to the notification

func (*Notification) AddUser

func (c *Notification) AddUser(userid string) error

AddUser adds a userid to this notification

type Request

type Request struct {
	// Authenticated whether the request was carrying an OAuth token or not
	Authenticated bool
	// UserAccessToken if Authenticated is true, this is the token provided by IFTTT, is not, this is set to the claimed Service Key from IFTTT
	UserAccessToken string
	// RequestUUID is a unique string which identifies requests for debugging purposes
	RequestUUID string
	// Slug the slug name of the action/trigger, you generally dont need to check this as requests are automatically routed by the package to their endpoints.
	Slug string
	// FieldSlug the field slug requested by the request, empty string in non field-related requests
	FieldSlug string
	// TriggerIdentity the identification of the trigger which initiated this request, empty string if not available
	TriggerIdentity string
	// DecodedBody the decoded JSON body of the request, you can extract data manually if it was not exposed by this package
	DecodedBody *gabs.Container
	// Type the type of the request, you generally dont need to check this as requests are automatically routed by the package to their endpoints.
	Type RequestType
	// RawRequest the raw HTTP request from IFTTT
	RawRequest *http.Request
	// ServiceRef reference to the service struct
	ServiceRef *Service
}

Request represents a parsed request from IFTTT

type RequestType

type RequestType int

RequestType enumerates IFTTT request types

const (
	// Unknown unknown request types
	Unknown RequestType = iota
	// TriggerFetch polls triggers for update information
	// https://platform.ifttt.com/docs/api_reference#triggers
	TriggerFetch
	// TriggerDeleteNotify notifies the trigger service that a trigger identifies by trigger identity has been removed and the server can stop making notifications regarding this trigger
	// https://platform.ifttt.com/docs/api_reference#trigger-identity
	TriggerDeleteNotify
	// TriggerDynamicOptions requests dynamic field options from a trigger
	// https://platform.ifttt.com/docs/api_reference#trigger-field-dynamic-options
	TriggerDynamicOptions
	// TriggerDynamicValidation requests the validation of a single field of a trigger
	// https://platform.ifttt.com/docs/api_reference#trigger-field-dynamic-validation
	TriggerDynamicValidation
	// TriggerContextualValidation requests the validation of a combination of the trigger fields, you need to contact IFTTT to have this enabled
	// https://platform.ifttt.com/docs/api_reference#trigger-field-contextual-validation
	TriggerContextualValidation
	// ActionTrigger triggers an action
	// https://platform.ifttt.com/docs/api_reference#actions
	ActionTrigger
	// ActionDynamicOptions requests dynamic field options from an action
	// https://platform.ifttt.com/docs/api_reference#action-fields
	ActionDynamicOptions
	// ServiceStatus requests for the availability of the service
	// https://platform.ifttt.com/docs/api_reference#service-status
	ServiceStatus
	// UserInfoRequest requests for the information of a user which will be displayed to the user on his service page
	// https://platform.ifttt.com/docs/api_reference#user-information
	UserInfoRequest
)

type Service

type Service struct {

	// IFTTT service key used to identify your service
	// get it from you dashboard
	ServiceKey string
	// Healthy should return whether the service is functioning normally
	// Defaults to true
	Healthy func() bool
	// UserInfo should return user info identified by req.UserAccessToken
	// if your service does not require authentication, passing nil should be OK
	UserInfo func(req *Request) (*UserInfo, error)
	// contains filtered or unexported fields
}

Service described the IFTTT service and handles requests from IFTTT

func (*Service) EnableDebug

func (c *Service) EnableDebug()

EnableDebug enabled debug output of this service

func (*Service) Notify

func (c *Service) Notify(evt Notification) error

Notify implements the IFTTT realtime API and sends notifications to the IFTTT realtime notification endpoint

func (*Service) RegisterAction

func (c *Service) RegisterAction(slug string, handler Action)

RegisterAction registers an action handler which implements Action

func (*Service) RegisterTrigger

func (c *Service) RegisterTrigger(slug string, handler Trigger)

RegisterTrigger registers a trigger handler which implements Trigger

func (Service) ServeHTTP

func (c Service) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler and handles http requests

type Trigger

type Trigger interface {
	// Poll askes the trigger for event updates
	// Implementations should return recent events regarding this trigger.
	// It does not matter if you return events which is already reported before, as long as the ID was kept the same.
	Poll(req *TriggerPollRequest, r *Request) (TriggerEventCollection, error)
	// Options is called during a trigger dynamic option request
	// Use req.FieldSlug for the field requested by this request.
	// If your trigger does not contain dynamic options, return nil, nil here.
	Options(req *Request) (*DynamicOption, error)
	// ValidateField is called during a trigger single-field validation request
	// Use req.FieldSlug for the field requested by this request.
	// If the field value is unacceptable, return a non-nil error which contains a friendly message describing the problem
	ValidateField(fieldslug string, value string, req *Request) error
	// ValidateContext is called during a trigger field context validation request
	// You need to contact IFTTT first to have this feature enabled
	// If you do not use this feature, return nil, nil here.
	ValidateContext(values map[string]string, req *Request) (map[string]error, error)
	// RemoveIdentity is called to notify the the trigger identified by triggerid has been deleted and the endpoint can stop tracking updates to this trigger
	// This is for performance reasons, especially when you are using real-time functions.
	// You can safely ignore this by returning nil directly.
	RemoveIdentity(triggerid string) error
	// RealTie should return whether this service supports the IFTTT real-time API.
	// If set to true, IFTTT will poll a lot less often on this trigger and rely on your active notification instead.
	// If you does not know what is IFTTT real-time API, return false here.
	RealTime() bool
}

Trigger is the interface which every registered trigger should implement.

type TriggerEvent

type TriggerEvent struct {
	// Ingredients contains values of the ingredients of this event
	Ingredients map[string]string
	// Meta contains metadata of this event
	Meta TriggerEventMeta
}

TriggerEvent represents a single event returned during a trigger poll the trigger will be triggered if ALL of the following conditions were met: (1) The trigger has NOT been fired by an event with identical IDs (2) The event happened later than the time the trigger was created

type TriggerEventCollection

type TriggerEventCollection []TriggerEvent

TriggerEventCollection a slice of TriggerEvent, the events returned to a trigger poll

func (TriggerEventCollection) Len

func (c TriggerEventCollection) Len() int

Len implements sort.Interface

func (TriggerEventCollection) Less

func (c TriggerEventCollection) Less(i, j int) bool

Less implements sort.Interface

func (TriggerEventCollection) Swap

func (c TriggerEventCollection) Swap(i, j int)

Swap implements sort.Interface

type TriggerEventMeta

type TriggerEventMeta struct {
	// ID should be a unique string identifier to an event, events with different IDs would be considered different events
	ID string
	// Time should be the creation or modification time of the resource that triggers the trigger
	// This field don't have to be constant, but the trigger will not be triggered if the time returned was earlier than the time the trigger was created
	Time time.Time
}

TriggerEventMeta represents the metadata of the event

type TriggerPollRequest

type TriggerPollRequest struct {
	// TriggerIdentity the identification string of the trigger
	TriggerIdentity string
	// TriggerFields the values of the trigger fields
	TriggerFields map[string]string
	// Limit max number of events requested, you can return a little more but extra events will be ignored
	Limit int
	// User contains the metadata of the IFTTT user (eg: timezone)
	User map[string]string
}

TriggerPollRequest represents the request from IFTTT for events regarding this trigger

type UserInfo

type UserInfo struct {
	Name string
	ID   string
	URL  string
}

UserInfo represents the user info returned to the user info request

Jump to

Keyboard shortcuts

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