-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
76 lines (72 loc) · 1.66 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
// filters is a bot that can receive photos, text messages & geolocations.
package main
import (
"fmt"
"log"
"os"
tm "github.com/and3rson/telemux"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
func main() {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TG_TOKEN"))
if err != nil {
log.Fatal(err)
}
bot.Debug = true
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
log.Fatal(err)
}
mux := tm.NewMux().
AddHandler(tm.NewHandler(
tm.IsCommandMessage("start"),
func(u *tm.Update) {
bot.Send(tgbotapi.NewMessage(
u.Message.Chat.ID,
"Hello! Send me a text message, photo or geolocation.",
))
},
)).
AddHandler(tm.NewHandler(
tm.HasText(),
func(u *tm.Update) {
bot.Send(tgbotapi.NewMessage(
u.Message.Chat.ID,
"You sent me a text message: "+u.Message.Text,
))
},
)).
AddHandler(tm.NewHandler(
tm.HasPhoto(),
func(u *tm.Update) {
photo := (*u.Message.Photo)[0]
bot.Send(tgbotapi.NewMessage(
u.Message.Chat.ID,
fmt.Sprintf("You sent me a photo of size %d x %d", photo.Width, photo.Height),
))
},
)).
AddHandler(tm.NewHandler(
tm.HasLocation(),
func(u *tm.Update) {
bot.Send(tgbotapi.NewMessage(
u.Message.Chat.ID,
fmt.Sprintf("You sent me a geolocation: %f;%f", u.Message.Location.Latitude, u.Message.Location.Longitude),
))
},
)).
AddHandler(tm.NewHandler(
tm.Any(),
func(u *tm.Update) {
bot.Send(tgbotapi.NewMessage(
u.Message.Chat.ID,
"Sorry, I only accept text messages, photos & geolocations. :(",
))
},
))
for update := range updates {
mux.Dispatch(update)
}
}