-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg_bot_handlers.go
152 lines (128 loc) · 4.15 KB
/
tg_bot_handlers.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
tele "gopkg.in/telebot.v3"
)
type HandlerFunc func(context.Context, []string, *tele.Chat, *tele.User) (string, error)
// lookupCity return city location if city is found. Otherwise bot text response or error
func lookupCity(ctx context.Context, requestedCity string) (*Location, string, error) {
loc, err := geoInfo.LookupCity(ctx, requestedCity)
if err != nil {
return nil, "", errors.WithMessagef(err, "LookupCity %s", requestedCity)
}
if loc == nil {
return nil, "Unknown city :(", nil
}
if strings.ToLower(loc.City) != strings.ToLower(requestedCity) {
return nil, fmt.Sprintf("Unknown city! Did you mean %s?", loc.City), nil
}
return loc, "", nil
}
func handleSetCity(ctx context.Context, args []string, chat *tele.Chat, sender *tele.User) (string, error) {
if len(args) < 1 {
return "invalid command! Use: /set_city Amsterdam", nil
}
loc, resp, err := lookupCity(ctx, strings.Join(args, " "))
if err != nil {
return "", err
}
if loc == nil {
return resp, nil
}
nl := NomadLocation{
ChatID: strconv.FormatInt(chat.ID, 10),
Username: sender.Username,
At: time.Now(),
Location: *loc,
}
err = storage.Save(ctx, nl)
if err != nil {
return "", errors.WithMessagef(err, "storage.Save")
}
return fmt.Sprintf("City %s set", loc.City), nil
}
func handleGetCity(ctx context.Context, args []string, chat *tele.Chat, _ *tele.User) (string, error) {
if len(args) < 1 {
return "invalid command! Use: /get_city New York", nil
}
loc, resp, err := lookupCity(ctx, strings.Join(args, " "))
if err != nil {
return "", err
}
if loc == nil {
return resp, nil
}
chatID := strconv.FormatInt(chat.ID, 10)
nls, err := storage.ListByCity(ctx, chatID, loc.City)
if err != nil {
return "", errors.WithMessagef(err, "ListByCity %s %s", chatID, loc.City)
}
if len(nls) == 0 {
return fmt.Sprintf("There's no nomads in city %s", loc.City), nil
}
var nomadList []string
for _, nl := range nls {
nomadList = append(nomadList, fmt.Sprintf("@%s", nl.Username))
}
return fmt.Sprintf("Nomads in city %s:\n%s", loc.City, strings.Join(nomadList, "\n")), nil
}
func handleGetCountry(ctx context.Context, args []string, chat *tele.Chat, _ *tele.User) (string, error) {
if len(args) < 1 {
return "invalid command! Use: /get_country Turkey", nil
}
requestedCountry := strings.Join(args, " ")
country, err := geoInfo.LookupCountry(ctx, requestedCountry)
if err != nil {
return "", errors.WithMessagef(err, "LookupCountry %s", requestedCountry)
}
if country == "" {
return "Unknown country :(", nil
}
if strings.ToLower(country) != strings.ToLower(requestedCountry) {
return fmt.Sprintf("Unknown country! Did you mean %s?", country), nil
}
chatID := strconv.FormatInt(chat.ID, 10)
nls, err := storage.ListByCountry(ctx, chatID, country)
if err != nil {
return "", errors.WithMessagef(err, "ListByCountry %s", country)
}
if len(nls) == 0 {
return fmt.Sprintf("There's no nomads in country %s", country), nil
}
var nomadList []string
for _, nl := range nls {
nomadList = append(nomadList, fmt.Sprintf("@%s", nl.Username))
}
return fmt.Sprintf("Nomads in country %s:\n%s", country, strings.Join(nomadList, "\n")), nil
}
func handleList(ctx context.Context, args []string, chat *tele.Chat, sender *tele.User) (string, error) {
if len(args) > 0 {
return "unsupported command", nil
}
chatID := strconv.FormatInt(chat.ID, 10)
nls, err := storage.List(ctx, chatID)
if err != nil {
return "", errors.WithMessagef(err, "List")
}
if len(nls) == 0 {
return fmt.Sprintf("Zero nomads in this chat!"), nil
}
var nomadList []string
for _, nl := range nls {
nomadList = append(nomadList, fmt.Sprintf("@%s - %s, %s", nl.Username, nl.City, nl.Country))
}
return fmt.Sprintf("Nomads in this chat:\n%s", strings.Join(nomadList, "\n")), nil
}
func handleMap(ctx context.Context, args []string, chat *tele.Chat, sender *tele.User) (string, error) {
if len(args) > 0 {
return "unsupported command", nil
}
chatID := strconv.FormatInt(chat.ID, 10)
url := fmt.Sprintf("%s/map/%s", CONFIG.MapServiceEndpoint, chatID)
return fmt.Sprintf("🧭 %s", url), nil
}