cinnabot

package module
v0.0.0-...-0ed76fc Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2019 License: MIT Imports: 20 Imported by: 0

README

cinnabot Build Status

Telegram Bot for Cinnamon College

Set Up

go get ./...

Test

go test ./...

Suggestions

  1. Caching of PSI data for faster response
Features
  1. Have cooler phrases as commands instead of one word commands. Like ''.
  2. Fun error messages (e.g. Chuck Norris Jokes)

Documentation

Index

Constants

View Source
const (
	TR1     = "THEME ROOM 1"
	TR2     = "THEME ROOM 2"
	CTPH    = "CHUA THIAN POH HALL"
	AMPH    = "AMPHITHEATRE"
	CHATTER = "CHATTERBOX"
)

String constants

Variables

This section is empty.

Functions

func EventBetweenDays

func EventBetweenDays(start, end time.Time) func(Event) bool

EventBetweenDays returns an EventPredicate testing if an event falls between the start and end date, inclusive. It is meant to be called with start, end time.Time obtained from ParseDDMMYYDate, which by default has time = 12MN.

func FormatDate

func FormatDate(t time.Time) string

FormatDate formats a time.Time into date in a standardised format

func FormatTime

func FormatTime(t time.Time) string

FormatTime formats a time.Time into a time in a standardised format

func FormatTimeDate

func FormatTimeDate(t time.Time) string

FormatTimeDate formats a time.Time into a full time and date, in a standardised format

func ParseDDMMYYDate

func ParseDDMMYYDate(date string) (time.Time, error)

ParseDDMMYYDate parses user-inputted dd/mm/yy date into time.Time

func ParseJSONDate

func ParseJSONDate(date string) time.Time

ParseJSONDate parses date from format used in nususc.com's API into a time.Time object

func SpaceName

func SpaceName(facilityID int) string

SpaceName returns the (capitalised) name of each Space. Case 4 (Prototyping Studio) unused and omitted.

Types

type AreaMetadata

type AreaMetadata struct {
	Name string            `json:"name"`
	Loc  tgbotapi.Location `json:"label_location"`
}

type BusStop

type BusStop struct {
	BusStopNumber string `json:"no"`
	Latitude      string `json:"lat"`
	Longitude     string `json:"lng"`
	BusStopName   string `json:"name"`
}

Bus stop structs

type BusStopHeap

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

func (BusStopHeap) Len

func (h BusStopHeap) Len() int

func (BusStopHeap) Less

func (h BusStopHeap) Less(i, j int) bool

func (*BusStopHeap) Pop

func (h *BusStopHeap) Pop() interface{}

func (*BusStopHeap) Push

func (h *BusStopHeap) Push(x interface{})

func (BusStopHeap) Swap

func (h BusStopHeap) Swap(i, j int)

type BusTimes

type BusTimes struct {
	Services []Service `json:"Services"`
}

Structs for BusTiming

type ByStartDate

type ByStartDate Space

ByStartDate implements sort.Interface for Space based on the space[i].Start field

func (ByStartDate) Len

func (space ByStartDate) Len() int

func (ByStartDate) Less

func (space ByStartDate) Less(i, j int) bool

func (ByStartDate) Swap

func (space ByStartDate) Swap(i, j int)

type Cinnabot

type Cinnabot struct {
	Name string // The name of the bot registered with Botfather
	// contains filtered or unexported fields
}

Cinnabot is main struct that processes user requests.

func InitCinnabot

func InitCinnabot(configJSON []byte, lg *log.Logger) *Cinnabot

InitCinnabot initializes an instance of Cinnabot.

func (*Cinnabot) About

func (cb *Cinnabot) About(msg *message)

About returns a link to Cinnabot's source code.

func (*Cinnabot) AddFunction

func (cb *Cinnabot) AddFunction(command string, resp ResponseFunc) error

AddFunction binds a response function to a command string in Cinnabot's FuncMap

func (*Cinnabot) Broadcast

func (cb *Cinnabot) Broadcast(msg *message)

Broadcast broadcasts a message after checking for admin status [trial] Admins are to first send a message with tags before sending actual message

func (*Cinnabot) BusTimings

func (cb *Cinnabot) BusTimings(msg *message)

BusTimings checks the bus timings based on given location

func (*Cinnabot) CBS

func (cb *Cinnabot) CBS(msg *message)

func (*Cinnabot) Cancel

func (cb *Cinnabot) Cancel(msg *message)

func (*Cinnabot) Capitalize

func (cb *Cinnabot) Capitalize(msg *message)

Capitalize returns a capitalized form of the input string.

func (*Cinnabot) CheckArgCmdPair

func (cb *Cinnabot) CheckArgCmdPair(cmd string, args []string) bool

Checks if arg can be used with command Used to supplement cache as cache only records functions as states

func (*Cinnabot) CinnabotFeedback

func (cb *Cinnabot) CinnabotFeedback(msg *message)

func (*Cinnabot) DHSurvey

func (cb *Cinnabot) DHSurvey(msg *message)

function to send message when someone enters dhsurvey tag

func (*Cinnabot) DHSurveyFeedback

func (cb *Cinnabot) DHSurveyFeedback(msg *message)

function to add DH survey entry to database

func (*Cinnabot) DiningFeedback

func (cb *Cinnabot) DiningFeedback(msg *message)

func (*Cinnabot) Echo

func (cb *Cinnabot) Echo(msg *message)

Echo parrots back the argument given by the user.

func (*Cinnabot) Feedback

func (cb *Cinnabot) Feedback(msg *message)

Feedback allows users an avenue to give feedback. Admins can retrieve by searching the /feedback handler in the db

func (*Cinnabot) GetStats

func (cb *Cinnabot) GetStats(msg *message)

function to count number of users and messages

func (*Cinnabot) GoSafely

func (cb *Cinnabot) GoSafely(fn func())

GoSafely is a utility wrapper to recover and log panics in goroutines. If we use naked goroutines, a panic in any one of them crashes the whole program. Using GoSafely prevents this.

func (*Cinnabot) Help

func (cb *Cinnabot) Help(msg *message)

Help gives a list of handles that the user may call along with a description of them

func (*Cinnabot) Listen

func (cb *Cinnabot) Listen(timeout int) tgbotapi.UpdatesChannel

Listen exposes the telebot Listen API.

func (*Cinnabot) NUSBus

func (cb *Cinnabot) NUSBus(msg *message)

NUSBus retrieves the next timing for NUS Shuttle buses

func (*Cinnabot) ResidentialFeedback

func (cb *Cinnabot) ResidentialFeedback(msg *message)

func (*Cinnabot) Resources

func (cb *Cinnabot) Resources(msg *message)

Link returns useful resources

func (*Cinnabot) Router

func (cb *Cinnabot) Router(msg tgbotapi.Message)

Router routes Telegram messages to the appropriate response functions. Hack: Cache to store previous function information

func (*Cinnabot) SayHello

func (cb *Cinnabot) SayHello(msg *message)

Test functions [Not meant to be used in bot] SayHello says hi.

func (*Cinnabot) SendMessage

func (cb *Cinnabot) SendMessage(chattable tgbotapi.Chattable)

SendMessage sends messages which require non-default options such as reply markups.

func (*Cinnabot) SendTextMessage

func (cb *Cinnabot) SendTextMessage(recipient int, text string) error

SendTextMessage sends a basic text message back to the specified user.

func (*Cinnabot) Spaces

func (cb *Cinnabot) Spaces(msg *message)

Spaces is the primary Cinnabot Spaces method that the end user interacts with.

"/spaces" displays bookings for today.
"/spaces now" displays bookings right this moment.
"/spaces week" displays bookings in the next 7 days.
"/spaces dd/mm/yy" displays bookings on the given date.
"/spaces dd/mm/yy dd/mm/yy" displays bookings in the given interval (limited to one month++).
"/spaces help" informs the user of available commands.

Extra arguments are ignored.
Unparseable commands return the help menu.

func (*Cinnabot) Start

func (cb *Cinnabot) Start(msg *message)

Start initializes the bot

func (*Cinnabot) Subscribe

func (cb *Cinnabot) Subscribe(msg *message)

Subscribe subscribes the user to a broadcast channel [trial]

func (*Cinnabot) USCFeedback

func (cb *Cinnabot) USCFeedback(msg *message)

func (*Cinnabot) Unsubscribe

func (cb *Cinnabot) Unsubscribe(msg *message)

Unsubscribe unsubscribes the user from a broadcast channel [trial]

func (*Cinnabot) Weather

func (cb *Cinnabot) Weather(msg *message)

Weather checks the weather based on given location

type Event

type Event struct {
	Type   string  `json:"__type"`
	ID     float64 `json:"id"`
	Title  string  `json:"title"`
	Start  string  `json:"start"`
	End    string  `json:"end"`
	AllDay bool    `json:"allDay"`
	Color  string  `json:"colour"`
}

An Event is a single booking of a location.

func (Event) TimeInfo

func (event Event) TimeInfo() string

TimeInfo returns the time info of an Event in a readable format with duplicate info minimised

type EventPredicate

type EventPredicate func(Event) bool

EventPredicate is a predicate function for use with Restrict.

type ForecastData

type ForecastData struct {
	FMD []ForecastMetadata `json:"forecasts"`
}

type ForecastMetadata

type ForecastMetadata struct {
	Name     string `json:"area"`
	Forecast string `json:"forecast"`
}

type FuncMap

type FuncMap map[string]ResponseFunc

A FuncMap is a map of command strings to response functions. It is used for routing commands to responses.

type NextBus

type NextBus struct {
	EstimatedArrival string `json:"EstimatedArrival"`
}

type Response

type Response struct {
	Result ServiceResult `json:"ShuttleServiceResult"`
}

NUSBusTimes structs for unmarshalling

type ResponseFunc

type ResponseFunc func(m *message)

ResponseFunc is a handler for a bot command.

type Service

type Service struct {
	ServiceNum string  `json:"ServiceNo"`
	Next       NextBus `json:"NextBus"`
}

type ServiceResult

type ServiceResult struct {
	Shuttles []Shuttle `json:"shuttles"`
}

type Shuttle

type Shuttle struct {
	ArrivalTime string `json:"arrivalTime"`
	Name        string `json:"name"`
}

type Space

type Space []Event

Space stores a set of Events, all belonging to the same location ("Space"), such as TR1 or CTPH.

type Spaces

type Spaces []Space

func (Spaces) Restrict

func (spaces Spaces) Restrict(predicate EventPredicate) Spaces

Restrict returns a new Spaces object containing all events in the original Spaces for which predicate returns true.

type WeatherForecast

type WeatherForecast struct {
	AM []AreaMetadata `json:"area_metadata"`
	FD []ForecastData `json:"items"`
}

Structs for weather forecast function

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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