cinnabot

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

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

Go to latest
Published: Oct 15, 2020 License: MIT Imports: 21 Imported by: 0

README ΒΆ

Cinnabot πŸ€–

Build Status Telegram

Telegram Bot for Cinnamon College. Telegram

Features:

  • Show NUS Internal Shuttle Bus Timings 🚍 /nusbus
  • Show Singapore Public Bus Timings 🚍 /publicbus
  • Check facilities booking/events in Cinnamon College 🏫 /spaces
  • 2h Weather Forecast based on your location. β˜€ β˜” /weather
  • Lost? View maps of various parts of NUS 🏫 /map
  • Check laundry machine availability in Cinnamon College πŸ‘• /laundry

Got a feature to suggest? πŸ’‘ Bug to report? πŸ› You are welcome to file an issue here.

Setting up the environment 🌏

0. Install essential packages

We will first need to install git and golang. First install git using your favourite package manager:

Ubuntu/Debian derivatives using APT
sudo apt install git
MacOS using Homebrew 🍺
brew install git

Then, install Golang version 1.15. You can either download it from the official website or download it using a package manager (remember to check the go version provided by the package manager).

For Linux systems, you can follow the steps if you downloaded go from the website:

cd Downloads # or wherever you downloaded the file
sudo tar -C /usr/local -xzf go1.15.2.linux-amd64.tar.gz
# change the lines below to the shell you are using
echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bashrc
source ~/.bashrc

To verify that Golang is installed properly:

$ which go # only for Mac/Linux
/usr/local/go/bin/go # it's okay for this to differ, it just shouldn't be blank
$ go version
go version go1.15.2 linux/amd64 # version, operating system/architecture
1. Clone the cinnabot repository

Go the the directory where you want to store the cinnabot code, and run this command to download the cinnabot repository into a new directory called 'cinnabot':

git clone https://github.com/usdevs/cinnabot.git 
# or git@github.com:usdevs/cinnabot.git if you are using a ssh key

Voila! Now we have cinnabot on our machine. Ready to go!! πŸŽ‰

Testing Cinnabot locally

Overview:

  1. Register for an API Token with BotFather
  2. Running a test bot on Telegram

All instructions below assume you are at the cinnabot root path, unless stated otherwise.

1. Register for an API Token with Botfather

Ask for the blessings of the Botfather here, as you register for one of the bots. You will be provided, with honor, an API token where you should put into main/config.json.

In other words, click on the link and choose a Telegram handle for your bot, which ends with ...bot and is not taken yet. Once you are done, you will be provided with the API Token.

Then, we create our config.json file using the example file config.json.example, and replacing the dummy API Token with the one we just registered.

cd main
cp config.json.example config.json

Fire up your favourite text editor and replace the dummy string in config.json with your API Token as a string.

2. Running a test bot on Telegram
cd main
go run main.go

And start testing! Fire up your favourite Telegram client, and find the bot by the name you registered it with. You can now test all cinnabot functionalities on your testbot.

When you are done, press Ctrl+C on your terminal to end testing.

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func EditedMessageWithButton ΒΆ

func EditedMessageWithButton(text string, keyboard tgbotapi.InlineKeyboardMarkup, chatID int64, msgID int) tgbotapi.EditMessageTextConfig

EditedMessageWithButton creates an edited text message with buttons (eg. refresh button)

func FormatDate ΒΆ

func FormatDate(t time.Time) string

FormatDate formats a time.Time into date in a standardised format. Does not change timezone.

func FormatTime ΒΆ

func FormatTime(localT time.Time) string

FormatTime formats a time.Time into a time in a standardised format. Does not change timezone.

func FormatTimeDate ΒΆ

func FormatTimeDate(t time.Time) string

FormatTimeDate formats a time.Time into a full time and date, in a standardised format. Does not change timezone.

func NewMessageWithButton ΒΆ

func NewMessageWithButton(text string, keyboard tgbotapi.InlineKeyboardMarkup, chatID int64) tgbotapi.MessageConfig

NewMessageWithButton creates a new text message with buttons (eg. refresh button)

func ParseDDMMYYDate ΒΆ

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

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

Types ΒΆ

type Callback ΒΆ

type Callback struct {
	ChatID int64
	MsgID  int
	Cmd    string
	Args   []string
	*tgbotapi.CallbackQuery
}

Wrapper struct for a callback query

func (Callback) GetArgString ΒΆ

func (qry Callback) GetArgString() string

GetArgStrings prints out the arguments for the callback in one string.

type CallbackFunc ΒΆ

type CallbackFunc func(*Callback)

CallbackFunc is a handler for a callback function

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) AddHandler ΒΆ

func (cb *Cinnabot) AddHandler(command string, resp CallbackFunc) error

AddHandler binds a handler function to a callback cmd string in Cinnabot's HandlerMap

func (*Cinnabot) Cancel ΒΆ

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

Cancel cancels the command

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

CheckArgCmdPair 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)

CinnabotFeedback forwards feedback to USDevs

func (*Cinnabot) DHSurvey ΒΆ

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

DHSurvey sends message when someone enters dhsurvey tag

func (*Cinnabot) DHSurveyFeedback ΒΆ

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

DHSurveyFeedback adds DH survey entry to database

func (*Cinnabot) DiningFeedback ΒΆ

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

DiningFeedback forwards feedback to DH Comm

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) Handle ΒΆ

func (cb *Cinnabot) Handle(qry tgbotapi.CallbackQuery)

Handle routes Telegram callback queries to the appropriate handlers.

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) Laundry ΒΆ

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

Laundry checks the washer and dryer availability.

func (*Cinnabot) LaundryRefresh ΒΆ

func (cb *Cinnabot) LaundryRefresh(qry *Callback)

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) NUSBusRefresh_Buttons ΒΆ

func (cb *Cinnabot) NUSBusRefresh_Buttons(qry *Callback)

NUSBusRefresh_Buttons handles the refresh button for messages from /nusbus -> location button

func (*Cinnabot) NUSBusRefresh_Location ΒΆ

func (cb *Cinnabot) NUSBusRefresh_Location(qry *Callback)

NUSBusRefresh_Location handles the refresh button for messages from /nusbus by GPS location

func (*Cinnabot) NUSMap ΒΆ

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

func (*Cinnabot) PublicBus ΒΆ

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

BusTimings checks the public bus timings based on given location

func (*Cinnabot) PublicBusRefresh ΒΆ

func (cb *Cinnabot) PublicBusRefresh(qry *Callback)

PublicBusRefresh handles the refresh button for messages from /publicbus

func (*Cinnabot) ResidentialFeedback ΒΆ

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

ResidentialFeedback forwards feedback to Residential Comm

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) USCFeedback ΒΆ

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

USCFeedback forwards feedback to USC

func (*Cinnabot) Weather ΒΆ

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

Weather checks the weather based on given location

type Event ΒΆ

type Event struct {
	Name  string
	Venue string
	Start time.Time
	End   time.Time
}

Event represents a booking

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 ResponseFunc ΒΆ

type ResponseFunc func(m *message)

ResponseFunc is a handler for a bot command.

type Space ΒΆ

type Space []Event

Space represents a list of Events at the same venue

type Spaces ΒΆ

type Spaces []Space

Spaces is a list of spaces

type WeatherForecast ΒΆ

type WeatherForecast struct {
	AM []areaMetadata `json:"area_metadata"`
	FD []forecastData `json:"items"`
}

Structs for weather forecast function

Directories ΒΆ

Path Synopsis
Package firestore reads from publically accessible Firestore databases using the REST API.
Package firestore reads from publically accessible Firestore databases using the REST API.

Jump to

Keyboard shortcuts

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