Documentation ¶
Index ¶
- Constants
- Variables
- func AboutHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, ...)
- func ChosenInlineResultHandler(ctx context.Context, bot *BusEtaBot, cir *tgbotapi.ChosenInlineResult) error
- func ETAMessageText(busStops BusStopRepository, etaService ETAService, formatter ETAFormatter, ...) (string, error)
- func EtaCallbackHandler(ctx context.Context, bot *BusEtaBot, cbq *tgbotapi.CallbackQuery, ...)
- func EtaDemoCallbackHandler(ctx context.Context, bot *BusEtaBot, cbq *tgbotapi.CallbackQuery, ...)
- func EtaHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, ...)
- func EtaTable(etas [][4]string) string
- func FallbackCommandHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message) error
- func FeedbackCmdHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, ...)
- func GetBotEnvironment() string
- func GetNearbyInlineQueryResults(ctx context.Context, streetView StreetViewProvider, busStops BusStopRepository, ...) ([]telegram.InlineQueryResult, error)
- func HelpHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, ...)
- func HideFavouritesCmdHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, ...)
- func InferEtaQuery(text string) (string, []string, error)
- func InlineQueryHandler(ctx context.Context, bot *BusEtaBot, ilq *tgbotapi.InlineQuery) error
- func LocationHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message) error
- func NewContext(r *http.Request) (ctx context.Context)
- func NewETAMessageReplyMarkup(busStopCode string, serviceNos []string, formatter string, inline bool) telegram.InlineKeyboardMarkup
- func NewEtaHandler(ctx context.Context, bot *BusEtaBot, cbq *tgbotapi.CallbackQuery, ...)
- func NewIncomingBusDetailsButton(busStopCode string, serviceNos []string) telegram.InlineKeyboardButton
- func NewIncomingBusSummaryButton(busStopCode string, serviceNos []string) telegram.InlineKeyboardButton
- func NewRefreshButton(busStopCode string, serviceNos []string, formatter string) telegram.InlineKeyboardButton
- func NewResendButton(busStopCode string, serviceNos []string, formatter string) telegram.InlineKeyboardButton
- func NewToggleFavouriteButton(busStopCode string, serviceNos []string) telegram.InlineKeyboardButton
- func PrivacyHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, ...)
- func RefreshCallbackHandler(ctx context.Context, bot *BusEtaBot, cbq *tgbotapi.CallbackQuery, ...)
- func ShowFavouritesCmdHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, ...)
- func SquaredEuclideanDistanceAtEquator(lat0, lon0, lat1, lon1 float64) float64
- func StartHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, ...)
- func TextHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message) error
- func ToggleFavouritesHandler(ctx context.Context, bot *BusEtaBot, cbq *tgbotapi.CallbackQuery, ...)
- func VersionHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, ...)
- type ArrivingBus
- type BusEtaBot
- type BusEtas
- type BusStop
- type BusStopGetter
- type BusStopRepository
- type CallbackData
- type CallbackQueryHandler
- type CommandHandler
- type DatastoreUserRepository
- func (r *DatastoreUserRepository) GetFormatter(ctx context.Context, userID int) Formatter
- func (r *DatastoreUserRepository) GetUserFavourites(ctx context.Context, userID int) (favourites []string, err error)
- func (r *DatastoreUserRepository) SetUserFavourites(ctx context.Context, userID int, favourites []string) error
- func (r *DatastoreUserRepository) UpdateUserLastSeenTime(ctx context.Context, userID int, t time.Time) error
- type ETA
- type ETAFormatter
- type ETAMessageFactory
- type ETARequest
- type ETAService
- type Favourites
- type Formatter
- type FormatterFactory
- type Handlers
- type HitParsingResult
- type InMemoryBusStopRepository
- type IncomingBuses
- type MeasurementProtocolClient
- type MessageHandler
- type NearbyBusStop
- type ParserMessage
- type Response
- type StreetViewAPI
- type StreetViewProvider
- type SummaryETAFormatter
- type TelegramService
- type TemplateFormatter
- type User
- type UserRepository
- type ValidationServerResponse
Constants ¶
const ( ApplicationName = "Bus Eta Bot" ApplicationID = "github.com/yi-jiayu/bus-eta-bot" ApplicationVersion = Version )
Application details
const ( MeasurementProtocolVersion = "1" MeasurementProtocolEndpoint = "https://www.google-analytics.com/collect" MeasurementProtocolValidationEndpoint = "https://www.google-analytics.com/debug/collect" )
Measurement Protocol constants
const ( CategoryCommand = "command" CategoryMessage = "message" CategoryInlineQuery = "inline_query" CategoryChosenInlineResult = "chosen_inline_result" CategoryCallback = "callback_query" CategoryError = "error" )
Event categories
const ( ActionEtaCommandWithArgs = "eta_command_with_args" ActionEtaCommandWithoutArgs = "eta_command_without_args" ActionStartCommand = "start_command" ActionAboutCommand = "about_command" ActionVersionCommand = "version_command" ActionHelpCommand = "help_command" ActionPrivacyCommand = "privacy_command" ActionFeedbackCommand = "feedback_command" ActionEtaTextMessage = "eta_text_message" ActionContinuedTextMessage = "continued_text_message" ActionIgnoredTextMessage = "ignored_text_message" ActionLocationMessage = "location_message" ActionNewInlineQuery = "new_inline_query" ActionNewNearbyInlineQuery = "new_nearby_inline_query" ActionOffsetInlineQuery = "offset_inline_query" ActionChosenInlineResult = "chosen_inline_result" ActionChosenNearbyInlineResult = "chosen_nearby_inline_result" ActionRefreshCallback = "refresh_callback" ActionResendCallback = "resend_callback" ActionEtaCallback = "eta_callback" ActionEtaDemoCallback = "eta_demo_callback" ActionEtaFromLocationCallback = "eta_from_location_callback" ActionAddFavouriteCalback = "add_favourite_callback" ActionRemoveFavouriteCalback = "remove_favourite_callback" ActionCommandError = "command_error" ActionMessageError = "message_error" ActionInlineQueryError = "inline_query_error" ActionCallbackError = "callback_error" )
Event actions
const ( RepoURL = "https://github.com/yi-jiayu/bus-eta-bot" PrivacyPolicyURL = "https://t.me/iv?url=https%3A%2F%2Fgithub.com%2Fyi-jiayu%2Fbus-eta-bot%2Fblob%2Fmaster%2FPRIVACY.md&rhash=a44cb5372834ee" HelpURL = "http://telegra.ph/Bus-Eta-Bot-Help-02-23" )
Links to relevant documents
const ( EnvironmentDev = devEnvironment EnvironmentStaging = stagingEnvironment EnvironmentProduction = productionEnvironment )
const ( FormatterSummary = "s" FormatterFeatures = "f" )
const ( EquatorialLatitude = 110574.0 EquatorialLongitude = 111320.0 )
const ( KindFavourites = "Favourites" KindUser = "User" )
const InlineQueryResultsLimit = 50
const (
LabelInlineMessage = "inline_message"
)
Event labels
const MaxMessageLength = 35
const NearbyBusStopsRadius = 1000.0
NearbyBusStopsRadius is the search range in metres for inline queries for nearby bus stops.
const ResponseBufferSize = 10
ResponseBufferSize is the size of the channel used to queue responses to be sent via the Telegram Bot API.
const StreetViewEndpoint = "https://maps.googleapis.com/maps/api/streetview"
StreetViewEndpoint is the endpoint for the Street View Image API.
const Version = "VERSION"
Version is the current Bus Eta Bot version
Variables ¶
var ( Formatters = map[string]Formatter{ FormatterSummary: summaryFormatter, FormatterFeatures: featuresFormatter, } )
var HTTPFormat = propagation.HTTPFormat{}
Functions ¶
func AboutHandler ¶
func AboutHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, responses chan<- Response)
AboutHandler handles the /about command
func ChosenInlineResultHandler ¶
func ChosenInlineResultHandler(ctx context.Context, bot *BusEtaBot, cir *tgbotapi.ChosenInlineResult) error
ChosenInlineResultHandler handles a chosen inline result
func ETAMessageText ¶
func ETAMessageText(busStops BusStopRepository, etaService ETAService, formatter ETAFormatter, t time.Time, code string, services []string) (string, error)
func EtaCallbackHandler ¶
func EtaCallbackHandler(ctx context.Context, bot *BusEtaBot, cbq *tgbotapi.CallbackQuery, responses chan<- Response)
EtaCallbackHandler handles callback queries from eta messages from old versions of the bot for backwards-compatibility.
func EtaDemoCallbackHandler ¶
func EtaDemoCallbackHandler(ctx context.Context, bot *BusEtaBot, cbq *tgbotapi.CallbackQuery, responses chan<- Response)
EtaDemoCallbackHandler handles an eta_demo callback from a start command.
func EtaHandler ¶
func EtaHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, responses chan<- Response)
EtaHandler handles the /eta command.
func FallbackCommandHandler ¶
FallbackCommandHandler catches commands which don't match any other handler.
func FeedbackCmdHandler ¶
func FeedbackCmdHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, responses chan<- Response)
FeedbackCmdHandler handles the /feedback command.
func GetBotEnvironment ¶
func GetBotEnvironment() string
func GetNearbyInlineQueryResults ¶
func GetNearbyInlineQueryResults(ctx context.Context, streetView StreetViewProvider, busStops BusStopRepository, lat, lon float64) ([]telegram.InlineQueryResult, error)
func HelpHandler ¶
func HelpHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, responses chan<- Response)
HelpHandler handles the /help command
func HideFavouritesCmdHandler ¶
func HideFavouritesCmdHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, responses chan<- Response)
HideFavouritesCmdHandler hides the favourites keyboard.
func InferEtaQuery ¶
InferEtaQuery extracts a bus stop ID and service numbers from a text message.
func InlineQueryHandler ¶
InlineQueryHandler handles inline queries
func LocationHandler ¶
LocationHandler handles messages contain a location
func NewEtaHandler ¶
func NewEtaHandler(ctx context.Context, bot *BusEtaBot, cbq *tgbotapi.CallbackQuery, responses chan<- Response)
NewEtaHandler sends etas for a bus stop when a user taps "Get etas" on a bus stop location returned from a location query
func NewIncomingBusDetailsButton ¶ added in v4.2.0
func NewIncomingBusDetailsButton(busStopCode string, serviceNos []string) telegram.InlineKeyboardButton
func NewIncomingBusSummaryButton ¶ added in v4.2.0
func NewIncomingBusSummaryButton(busStopCode string, serviceNos []string) telegram.InlineKeyboardButton
func NewRefreshButton ¶
func NewRefreshButton(busStopCode string, serviceNos []string, formatter string) telegram.InlineKeyboardButton
func NewResendButton ¶
func NewResendButton(busStopCode string, serviceNos []string, formatter string) telegram.InlineKeyboardButton
func NewToggleFavouriteButton ¶
func NewToggleFavouriteButton(busStopCode string, serviceNos []string) telegram.InlineKeyboardButton
func PrivacyHandler ¶
func PrivacyHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, responses chan<- Response)
PrivacyHandler handles the /privacy command.
func RefreshCallbackHandler ¶
func RefreshCallbackHandler(ctx context.Context, bot *BusEtaBot, cbq *tgbotapi.CallbackQuery, responses chan<- Response)
RefreshCallbackHandler handles the callback for the Refresh button on an eta message.
func ShowFavouritesCmdHandler ¶
func ShowFavouritesCmdHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, responses chan<- Response)
ShowFavouritesCmdHandler will display a reply keyboard for quick access to the user's favourites.
func SquaredEuclideanDistanceAtEquator ¶
EuclideanDistanceAtEquator returns the approximate squared distance between two points near the equator.
func StartHandler ¶
func StartHandler(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, responses chan<- Response)
StartHandler handles a /start command.
func TextHandler ¶
TextHandler handles incoming text messages
Types ¶
type ArrivingBus ¶ added in v4.2.0
type ArrivingBus struct { ServiceNo string datamall.ArrivingBus }
type BusEtaBot ¶
type BusEtaBot struct { Handlers Handlers Telegram *tgbotapi.BotAPI Datamall ETAService StreetView StreetViewProvider MeasurementProtocol *MeasurementProtocolClient NowFunc func() time.Time BusStops BusStopRepository Users UserRepository TelegramService TelegramService }
BusEtaBot contains all the bot's dependencies
func NewBot ¶
func NewBot(handlers Handlers, tg *tgbotapi.BotAPI, dm ETAService, sv *StreetViewAPI, mp *MeasurementProtocolClient) BusEtaBot
NewBot creates a new Bus Eta Bot with the provided tgbotapi.BotAPI and datamall.APIClient.
func (*BusEtaBot) Dispatch ¶
Dispatch makes requests to the Telegram Bot API for each response in responses.
func (*BusEtaBot) HandleUpdate ¶
HandleUpdate dispatches an incoming update to the corresponding handler depending on the update type
type BusEtas ¶
type BusEtas struct { BusStopID string UpdatedTime time.Time Services []IncomingBuses }
BusEtas represents the calculated time before buses arrive at a bus stop
func CalculateEtas ¶
CalculateEtas calculates the time before buses arrive from the LTA DataMall bus arrival response
type BusStop ¶
type BusStop struct { BusStopCode string `json:"code"` RoadName string `json:"road_name"` Description string `json:"description"` Latitude float64 `json:"latitude"` Longitude float64 `json:"longitude"` Services []string `json:"services"` }
BusStop represents a bus stop.
type BusStopGetter ¶ added in v4.2.0
type BusStopRepository ¶
type BusStopRepository interface { BusStopGetter Nearby(ctx context.Context, lat, lon, radius float64, limit int) (nearby []NearbyBusStop) Search(ctx context.Context, query string, limit int) []BusStop }
BusStopRepository provides bus stop information.
type CallbackData ¶
type CallbackData struct { Type string `json:"t"` BusStopID string `json:"b,omitempty"` ServiceNos []string `json:"s,omitempty"` Argstr string `json:"a,omitempty"` Formatter string `json:"f,omitempty"` }
CallbackData represents the data to be included with the Refresh inline keyboard button in eta messages.
type CallbackQueryHandler ¶
type CallbackQueryHandler func(ctx context.Context, bot *BusEtaBot, cbq *tgbotapi.CallbackQuery, responses chan<- Response)
CallbackQueryHandler is a handler for callback queries
type CommandHandler ¶
type CommandHandler func(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, responses chan<- Response)
CommandHandler is a handler for incoming commands.
type DatastoreUserRepository ¶
type DatastoreUserRepository struct { }
func (*DatastoreUserRepository) GetFormatter ¶ added in v4.2.0
func (r *DatastoreUserRepository) GetFormatter(ctx context.Context, userID int) Formatter
GetFormatter returns the user's preferred ETA formatter, or the default ETA formatter.
func (*DatastoreUserRepository) GetUserFavourites ¶
func (*DatastoreUserRepository) SetUserFavourites ¶
func (*DatastoreUserRepository) UpdateUserLastSeenTime ¶
type ETA ¶ added in v4.2.0
func NewETA ¶ added in v4.2.0
func NewETA(ctx context.Context, busStopGetter BusStopGetter, etaService ETAService, request ETARequest) (eta ETA)
type ETAFormatter ¶
type ETAMessageFactory ¶ added in v4.2.0
type ETAMessageFactory struct {
// contains filtered or unexported fields
}
func (ETAMessageFactory) Text ¶ added in v4.2.0
func (f ETAMessageFactory) Text(ctx context.Context, request ETARequest) (string, error)
type ETARequest ¶ added in v4.2.0
type ETAService ¶
type Favourites ¶
type Favourites struct {
Favourites []string
}
Favourites contains a user's saved favourites.
type FormatterFactory ¶ added in v4.2.0
type Handlers ¶
type Handlers struct { CommandHandlers map[string]CommandHandler FallbackCommandHandler MessageHandler TextHandler MessageHandler LocationHandler MessageHandler CallbackQueryHandlers map[string]CallbackQueryHandler InlineQueryHandler func(ctx context.Context, bot *BusEtaBot, ilq *tgbotapi.InlineQuery) error ChosenInlineResultHandler func(ctx context.Context, bot *BusEtaBot, cir *tgbotapi.ChosenInlineResult) error MessageErrorHandler func(ctx context.Context, bot *BusEtaBot, message *tgbotapi.Message, err error) CallbackErrorHandler func(ctx context.Context, bot *BusEtaBot, query *tgbotapi.CallbackQuery, err error) }
Handlers contains all the handlers used by the bot.
func DefaultHandlers ¶
func DefaultHandlers() Handlers
DefaultHandlers returns a default set of handlers.
type HitParsingResult ¶
type HitParsingResult struct { Valid bool `json:"valid"` Hit string `json:"hit"` ParserMessages []ParserMessage `json:"parserMessage"` }
HitParsingResult contains whether the a hit is valid and additional information fom the validation server
type InMemoryBusStopRepository ¶
type InMemoryBusStopRepository struct {
// contains filtered or unexported fields
}
func NewInMemoryBusStopRepository ¶
func NewInMemoryBusStopRepository(busStops []BusStop, synonyms map[string]string) *InMemoryBusStopRepository
func NewInMemoryBusStopRepositoryFromFile ¶
func NewInMemoryBusStopRepositoryFromFile(path, synonymsPath string) (*InMemoryBusStopRepository, error)
func (*InMemoryBusStopRepository) Get ¶
func (r *InMemoryBusStopRepository) Get(ID string) *BusStop
func (*InMemoryBusStopRepository) Nearby ¶
func (r *InMemoryBusStopRepository) Nearby(ctx context.Context, lat, lon, radius float64, limit int) (nearby []NearbyBusStop)
Nearby returns up to limit bus stops which are within a given radius from a point as well as their distance from that point.
type IncomingBuses ¶
type IncomingBuses struct { ServiceNo string Etas [3]string Loads [3]string Features [3]string Types [3]string }
IncomingBuses contains information about incoming buses for a single service at a bus stop.
type MeasurementProtocolClient ¶
MeasurementProtocolClient contains the endpoint, tracking id and http client to use to send hits to the Measurement Protocol
func NewMeasurementProtocolClientWithClient ¶
func NewMeasurementProtocolClientWithClient(tid string, client *http.Client) MeasurementProtocolClient
NewMeasurementProtocolClientWithClient constructs a MeasurementProtocolClient with the provided tid and http.Client
type MessageHandler ¶
MessageHandler is a handler for incoming messages
type NearbyBusStop ¶
type ParserMessage ¶
type ParserMessage struct { Type string `json:"messageType"` Description string `json:"description"` Parameter string `json:"parameter"` }
ParserMessage is a message from the validation server parser.
type StreetViewAPI ¶
StreetViewAPI contains information for making a request to the Street View Image API.
func NewStreetViewAPI ¶
func NewStreetViewAPI(key string) StreetViewAPI
NewStreetViewAPI returns a StreetViewAPI with an API key.
func (StreetViewAPI) GetPhotoURLByLocation ¶
func (s StreetViewAPI) GetPhotoURLByLocation(lat, lon float64, width, height int) (string, error)
GetPhotoURLByLocation returns a street view image url based on a latitude and longitude.
type StreetViewProvider ¶
type SummaryETAFormatter ¶
type SummaryETAFormatter struct{}
SummaryETAFormatter displays ETAs for up to the next 3 buses at a bus stop.
type TelegramService ¶
type TemplateFormatter ¶ added in v4.2.0
type TemplateFormatter struct {
// contains filtered or unexported fields
}
type UserRepository ¶
type ValidationServerResponse ¶
type ValidationServerResponse struct {
Results []HitParsingResult `json:"hitParsingResult"`
}
ValidationServerResponse is a response from the Measurement Protocol Validation Server