-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (94 loc) · 3.27 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"log"
"regexp"
"strings"
apiclient "github.com/eryalito/vigo-bus-core-go-client"
"github.com/eryalito/vigo-bus-tg-bot/internal/api"
"github.com/eryalito/vigo-bus-tg-bot/internal/config"
"github.com/eryalito/vigo-bus-tg-bot/internal/handlers"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func main() {
log.Println("Starting Vigo Bus Telegram Bot")
log.Println("Initializing configuration")
config.Init()
log.Println("Configuration initialized")
client := api.GetAPIClient()
// Initialize the bot
bot, err := tgbotapi.NewBotAPI(config.BotToken)
if err != nil {
log.Fatalf("Failed to create bot: %v", err)
}
bot.Debug = config.Debug
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates {
go processUpdate(update, bot, client)
}
}
func processUpdate(update tgbotapi.Update, bot *tgbotapi.BotAPI, client *apiclient.APIClient) {
if update.Message != nil { // If we got a message
if config.Debug {
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
}
identity, err := handlers.InsertHandler(nil, bot, update.Message.Chat.ID, client)
if err != nil || identity == nil {
log.Println(err)
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Failed to process the message"))
return
}
// Handle commands
switch {
case strings.HasPrefix(update.Message.Text, "/help"):
handlers.HelpHandler(identity, bot, update.Message, client)
case strings.HasPrefix(update.Message.Text, "/start"):
handlers.StartHandler(identity, bot, update.Message, client)
case strings.HasPrefix(update.Message.Text, "/search"):
handlers.FindHandler(identity, bot, update.Message, client)
case strings.HasPrefix(update.Message.Text, "/fav"):
handlers.FavoriteHandler(identity, bot, update.Message, client)
case matchRegex(update.Message.Text, `^/\d+$`):
handlers.StopInfoHandler(identity, bot, update.Message, client)
default:
if update.Message.Location != nil {
handlers.LocationHandler(identity, bot, update.Message, client)
return
}
handlers.UnknownHandler(identity, bot, update.Message, client)
}
return
}
if update.CallbackQuery != nil {
if config.Debug {
log.Printf("[%s] %s", update.CallbackQuery.From.UserName, update.CallbackQuery.Data)
}
identity, err := handlers.InsertHandler(nil, bot, update.CallbackQuery.From.ID, client)
if err != nil || identity == nil {
log.Println(err)
bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Failed to process the message"))
return
}
switch {
case strings.HasPrefix(update.CallbackQuery.Data, "/fav"):
handlers.FavoriteCallbackHandler(identity, bot, update.CallbackQuery, client)
case strings.HasPrefix(update.CallbackQuery.Data, "/follow"):
handlers.FollowCallbackHandler(identity, bot, update.CallbackQuery, client)
case matchRegex(update.CallbackQuery.Data, `^/\d+$`):
handlers.StopInfoCallbackHandler(identity, bot, update.CallbackQuery, client)
default:
// Unknown command
}
return
}
}
// matchRegex checks if the text matches the given regex pattern
func matchRegex(text, pattern string) bool {
matched, err := regexp.MatchString(pattern, text)
if err != nil {
return false
}
return matched
}