mirror of
https://github.com/aykhans/azal-bot.git
synced 2025-04-20 22:07:16 +00:00
🔨 Refactor Telegram message sending logic
This commit is contained in:
parent
db805c0aa4
commit
8b1ec13e78
114
main.go
114
main.go
@ -14,8 +14,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
RequestURL = "https://azal.az/book/api/flights/search/by-deeplink"
|
RequestURL = "https://azal.az/book/api/flights/search/by-deeplink"
|
||||||
Version = "0.1.0"
|
TelegramAPIURL = "https://api.telegram.org/bot%s/sendMessage"
|
||||||
|
Version = "0.1.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -52,6 +53,63 @@ func Colored(color string, a ...any) string {
|
|||||||
|
|
||||||
type AvialableFlights map[string][]time.Time
|
type AvialableFlights map[string][]time.Time
|
||||||
|
|
||||||
|
type TelegramRequest struct {
|
||||||
|
Client *http.Client
|
||||||
|
BotKey string
|
||||||
|
ChatID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (telegramRequest *TelegramRequest) sendTelegramMessage(message string) error {
|
||||||
|
url := fmt.Sprintf(TelegramAPIURL, telegramRequest.BotKey)
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
q := req.URL.Query()
|
||||||
|
q.Add("chat_id", telegramRequest.ChatID)
|
||||||
|
q.Add("text", message)
|
||||||
|
q.Add("parse_mode", "HTML")
|
||||||
|
req.URL.RawQuery = q.Encode()
|
||||||
|
|
||||||
|
resp, err := telegramRequest.Client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
return fmt.Errorf("error: telegram send message status code: %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (telegramRequest *TelegramRequest) sendTelegramFlightNotification(avialableFlights AvialableFlights) error {
|
||||||
|
message := "Azal Bot\n\n"
|
||||||
|
for day, flights := range avialableFlights {
|
||||||
|
message += fmt.Sprintf("%s\n-----------\n", day)
|
||||||
|
for _, flight := range flights {
|
||||||
|
message += fmt.Sprintf("%s\n", flight.Format("15:04:05"))
|
||||||
|
}
|
||||||
|
message += "\n"
|
||||||
|
}
|
||||||
|
message = message[:len(message)-1]
|
||||||
|
return telegramRequest.sendTelegramMessage(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (telegramRequest *TelegramRequest) sendTelegramStartNotification(botConfig *BotConfig) error {
|
||||||
|
return telegramRequest.sendTelegramMessage(
|
||||||
|
fmt.Sprintf(
|
||||||
|
"Azal Bot started\n\nFrom: %s\nTo: %s\nFirst Date: %s\nLast Date: %s\nRepetition Interval: %s",
|
||||||
|
botConfig.From,
|
||||||
|
botConfig.To,
|
||||||
|
botConfig.FirstDate.Format("2006-01-02T15:04:05"),
|
||||||
|
botConfig.LastDate.Format("2006-01-02T15:04:05"),
|
||||||
|
botConfig.RepetInterval.String(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
type UserInput struct {
|
type UserInput struct {
|
||||||
FirstDate time.Time
|
FirstDate time.Time
|
||||||
LastDate time.Time
|
LastDate time.Time
|
||||||
@ -89,7 +147,7 @@ func (responseTime *ResponseTime) UnmarshalJSON(b []byte) error {
|
|||||||
|
|
||||||
type SuccessResponse struct {
|
type SuccessResponse struct {
|
||||||
Warnings []any `json:"warnings"`
|
Warnings []any `json:"warnings"`
|
||||||
Search struct {
|
Search struct {
|
||||||
OptionSets []struct {
|
OptionSets []struct {
|
||||||
Options []struct {
|
Options []struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
@ -403,42 +461,6 @@ func getUserInput() *UserInput {
|
|||||||
return userInput
|
return userInput
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendTelegramMessage(avialableFlights AvialableFlights, botKey string, chatID string) error {
|
|
||||||
url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", botKey)
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
|
|
||||||
message := "Azal Bot\n\n"
|
|
||||||
for day, flights := range avialableFlights {
|
|
||||||
message += fmt.Sprintf("%s\n-----------\n", day)
|
|
||||||
for _, flight := range flights {
|
|
||||||
message += fmt.Sprintf("%s\n", flight.Format("15:04:05"))
|
|
||||||
}
|
|
||||||
message += "\n"
|
|
||||||
}
|
|
||||||
message = message[:len(message)-1]
|
|
||||||
q := req.URL.Query()
|
|
||||||
q.Add("chat_id", chatID)
|
|
||||||
q.Add("text", message)
|
|
||||||
q.Add("parse_mode", "HTML")
|
|
||||||
req.URL.RawQuery = q.Encode()
|
|
||||||
|
|
||||||
client := &http.Client{}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
if resp.StatusCode != 200 {
|
|
||||||
return fmt.Errorf("telegram send message status code: %d", resp.StatusCode)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func startBot(botConfig *BotConfig, ifAvailable func(avialableFlights AvialableFlights) error) {
|
func startBot(botConfig *BotConfig, ifAvailable func(avialableFlights AvialableFlights) error) {
|
||||||
queryConf := QueryConfig{
|
queryConf := QueryConfig{
|
||||||
From: botConfig.From,
|
From: botConfig.From,
|
||||||
@ -500,13 +522,17 @@ func main() {
|
|||||||
|
|
||||||
ifAvailableFunc := func(avialableFlights AvialableFlights) error { return nil }
|
ifAvailableFunc := func(avialableFlights AvialableFlights) error { return nil }
|
||||||
if userInput.TelegramBotKey != "" {
|
if userInput.TelegramBotKey != "" {
|
||||||
|
telegramRequest := &TelegramRequest{
|
||||||
|
Client: &http.Client{},
|
||||||
|
BotKey: userInput.TelegramBotKey,
|
||||||
|
ChatID: userInput.TelegramChatID,
|
||||||
|
}
|
||||||
|
if err := telegramRequest.sendTelegramStartNotification(botConfig); err != nil {
|
||||||
|
log.Println(Colored(Colors.Red, err.Error()))
|
||||||
|
}
|
||||||
ifAvailableFunc = func(avialableFlights AvialableFlights) error {
|
ifAvailableFunc = func(avialableFlights AvialableFlights) error {
|
||||||
if len(avialableFlights) > 0 {
|
if len(avialableFlights) > 0 {
|
||||||
return sendTelegramMessage(
|
return telegramRequest.sendTelegramFlightNotification(avialableFlights)
|
||||||
avialableFlights,
|
|
||||||
userInput.TelegramBotKey,
|
|
||||||
userInput.TelegramChatID,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user