mirror of
https://github.com/aykhans/azal-bot.git
synced 2025-04-20 22:07:16 +00:00
🔨 General refactoring
This commit is contained in:
parent
8b1ec13e78
commit
8cd132d8fa
37
main.go
37
main.go
@ -21,6 +21,7 @@ const (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
ErrorNoFlightsAvailable = fmt.Errorf("no flights available")
|
ErrorNoFlightsAvailable = fmt.Errorf("no flights available")
|
||||||
|
ErrorFlowInterrupted = fmt.Errorf("flow interrupted")
|
||||||
)
|
)
|
||||||
|
|
||||||
var Colors = struct {
|
var Colors = struct {
|
||||||
@ -85,7 +86,7 @@ func (telegramRequest *TelegramRequest) sendTelegramMessage(message string) erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (telegramRequest *TelegramRequest) sendTelegramFlightNotification(avialableFlights AvialableFlights) error {
|
func (telegramRequest *TelegramRequest) sendTelegramFlightNotification(avialableFlights AvialableFlights) error {
|
||||||
message := "Azal Bot\n\n"
|
message := "Azal Bot Flights\n\n"
|
||||||
for day, flights := range avialableFlights {
|
for day, flights := range avialableFlights {
|
||||||
message += fmt.Sprintf("%s\n-----------\n", day)
|
message += fmt.Sprintf("%s\n-----------\n", day)
|
||||||
for _, flight := range flights {
|
for _, flight := range flights {
|
||||||
@ -110,6 +111,10 @@ func (telegramRequest *TelegramRequest) sendTelegramStartNotification(botConfig
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (telegramRequest *TelegramRequest) sendTelegramErrorNotification(err error) error {
|
||||||
|
return telegramRequest.sendTelegramMessage(fmt.Sprintf("Azal Bot Error: %s", err.Error()))
|
||||||
|
}
|
||||||
|
|
||||||
type UserInput struct {
|
type UserInput struct {
|
||||||
FirstDate time.Time
|
FirstDate time.Time
|
||||||
LastDate time.Time
|
LastDate time.Time
|
||||||
@ -303,12 +308,14 @@ func handleErrorResponse(errorResponse *ErrorResponse) error {
|
|||||||
switch errorResponse.Error.Code {
|
switch errorResponse.Error.Code {
|
||||||
case "no.flights.available":
|
case "no.flights.available":
|
||||||
return ErrorNoFlightsAvailable
|
return ErrorNoFlightsAvailable
|
||||||
|
case "flow.interrupted.error":
|
||||||
|
return ErrorFlowInterrupted
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unknown error: %s", errorResponse.Error.Code)
|
return fmt.Errorf("unknown error: %s", errorResponse.Error.Code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendRequest(queryConf *QueryConfig, headerConf *HeaderConfig) (*SuccessResponse, error) {
|
func sendRequest(client *http.Client, queryConf *QueryConfig, headerConf *HeaderConfig) (*SuccessResponse, error) {
|
||||||
req, err := http.NewRequest("GET", RequestURL, nil)
|
req, err := http.NewRequest("GET", RequestURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -317,7 +324,6 @@ func sendRequest(queryConf *QueryConfig, headerConf *HeaderConfig) (*SuccessResp
|
|||||||
headerConf.setToRequest(req)
|
headerConf.setToRequest(req)
|
||||||
queryConf.setToRequest(req)
|
queryConf.setToRequest(req)
|
||||||
|
|
||||||
client := &http.Client{}
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -461,7 +467,7 @@ func getUserInput() *UserInput {
|
|||||||
return userInput
|
return userInput
|
||||||
}
|
}
|
||||||
|
|
||||||
func startBot(botConfig *BotConfig, ifAvailable func(avialableFlights AvialableFlights) error) {
|
func startBot(botConfig *BotConfig, ifAvailable func(avialableFlights AvialableFlights) error, ifError func(err error) error) {
|
||||||
queryConf := QueryConfig{
|
queryConf := QueryConfig{
|
||||||
From: botConfig.From,
|
From: botConfig.From,
|
||||||
To: botConfig.To,
|
To: botConfig.To,
|
||||||
@ -470,17 +476,24 @@ func startBot(botConfig *BotConfig, ifAvailable func(avialableFlights AvialableF
|
|||||||
headerConf := HeaderConfig{}
|
headerConf := HeaderConfig{}
|
||||||
headerConf.setDefaults()
|
headerConf.setDefaults()
|
||||||
|
|
||||||
|
sendRequestClient := &http.Client{}
|
||||||
for {
|
for {
|
||||||
avialableFlights := make(AvialableFlights)
|
avialableFlights := make(AvialableFlights)
|
||||||
for _, day := range botConfig.days {
|
for _, day := range botConfig.days {
|
||||||
queryConf.DepartureDate = day
|
queryConf.DepartureDate = day
|
||||||
data, err := sendRequest(&queryConf, &headerConf)
|
data, err := sendRequest(sendRequestClient, &queryConf, &headerConf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == ErrorNoFlightsAvailable {
|
switch err {
|
||||||
|
case ErrorNoFlightsAvailable:
|
||||||
log.Println(Colored(Colors.Yellow, "No flights available for ", day))
|
log.Println(Colored(Colors.Yellow, "No flights available for ", day))
|
||||||
continue
|
case ErrorFlowInterrupted:
|
||||||
}
|
log.Println(Colored(Colors.Red, "The date entered has passed: ", day))
|
||||||
|
if err := ifError(fmt.Errorf("the date entered has passed: %s", day)); err != nil {
|
||||||
log.Println(Colored(Colors.Red, err.Error()))
|
log.Println(Colored(Colors.Red, err.Error()))
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
log.Println(Colored(Colors.Red, err.Error()))
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -521,6 +534,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ifAvailableFunc := func(avialableFlights AvialableFlights) error { return nil }
|
ifAvailableFunc := func(avialableFlights AvialableFlights) error { return nil }
|
||||||
|
ifErrorFunc := func(err error) error { return nil }
|
||||||
if userInput.TelegramBotKey != "" {
|
if userInput.TelegramBotKey != "" {
|
||||||
telegramRequest := &TelegramRequest{
|
telegramRequest := &TelegramRequest{
|
||||||
Client: &http.Client{},
|
Client: &http.Client{},
|
||||||
@ -536,10 +550,17 @@ func main() {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
ifErrorFunc = func(err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return telegramRequest.sendTelegramErrorNotification(err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
startBot(
|
startBot(
|
||||||
botConfig,
|
botConfig,
|
||||||
ifAvailableFunc,
|
ifAvailableFunc,
|
||||||
|
ifErrorFunc,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user