-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (59 loc) · 1.31 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
package main
import (
"errors"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"time"
tele "gopkg.in/telebot.v3"
"gopkg.in/telebot.v3/react"
)
func main() {
bot, err := tele.NewBot(tele.Settings{
URL: botConfig().BotAPI,
Token: botConfig().BotToken,
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
OnError: func(err error, ctx tele.Context) {
fmt.Println(err.Error())
},
})
if !errors.Is(err, nil) {
panic(err)
}
bot.Handle(tele.OnText, func(c tele.Context) error {
messageText := c.Text()
if isURL(messageText) && urlExists(messageText) {
emoji := getEmojiFor(messageText)
errChan := make(chan error)
go func() {
errChan <- c.Bot().React(c.Recipient(), c.Message(), react.React(emoji))
}()
if err := <-errChan; err != nil {
return fmt.Errorf("error reacting to message: %w", err)
}
media, err := getMediaSource(messageText)
if err != nil {
return err
}
return c.SendAlbum(tele.Album{media})
}
return nil
})
var wg sync.WaitGroup
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT)
go func() {
<-sigChan
fmt.Println("\nReceived SIGINT, exiting...")
wg.Done()
}()
wg.Add(1)
go func() {
defer wg.Done()
bot.Start()
}()
fmt.Println("\nListening for updates. Interrupt (Ctrl+C) to stop.")
wg.Wait()
}