-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
96 lines (84 loc) · 2.53 KB
/
run.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
package main
import (
"bigboofer/database"
"bigboofer/handlers"
"log"
"time"
telegram "gopkg.in/tucnak/telebot.v2"
)
func main() {
log.Println("Connecting to Telegram...")
// Connect bot to Telegram
bot := connectBot()
// Set up database
database.OnboardDB()
// Register event handlers
bot.Handle(telegram.OnAddedToGroup, func(message *telegram.Message) {
handlers.OnAddedToGroup(bot, message)
})
bot.Handle(telegram.OnUserJoined, func(message *telegram.Message) {
handlers.OnUserJoined(bot, message)
})
bot.Handle("/setchannel", func(message *telegram.Message) {
handlers.OnSetChannelCommand(bot, message)
})
bot.Handle("/approve", func(message *telegram.Message) {
handlers.OnApproveCommand(bot, message)
})
bot.Handle(telegram.OnText, func(message *telegram.Message) {
handlers.OnMessage(bot, message)
})
bot.Handle(telegram.OnPhoto, func(message *telegram.Message) {
handlers.OnMessage(bot, message)
})
bot.Handle(telegram.OnAudio, func(message *telegram.Message) {
handlers.OnMessage(bot, message)
})
bot.Handle(telegram.OnDocument, func(message *telegram.Message) {
handlers.OnMessage(bot, message)
})
bot.Handle(telegram.OnSticker, func(message *telegram.Message) {
handlers.OnMessage(bot, message)
})
bot.Handle(telegram.OnVideo, func(message *telegram.Message) {
handlers.OnMessage(bot, message)
})
bot.Handle(telegram.OnVoice, func(message *telegram.Message) {
handlers.OnMessage(bot, message)
})
bot.Handle(telegram.OnVideoNote, func(message *telegram.Message) {
handlers.OnMessage(bot, message)
})
bot.Handle(telegram.OnContact, func(message *telegram.Message) {
handlers.OnMessage(bot, message)
})
bot.Handle(telegram.OnLocation, func(message *telegram.Message) {
handlers.OnMessage(bot, message)
})
bot.Handle(telegram.OnVenue, func(message *telegram.Message) {
handlers.OnMessage(bot, message)
})
// Schedule recurring job to purge people who take too long to
// respond to the challenge
go func(bot *telegram.Bot) {
for true {
time.Sleep(30 * time.Second)
database.PurgeOldChallengesForAllChats(bot)
}
}(bot)
log.Printf("Bot %v is connected!\n", bot.Me.Username)
bot.Start()
}
func connectBot() *telegram.Bot {
bot, err := telegram.NewBot(telegram.Settings{
Token: APIKey,
Poller: &telegram.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
log.Printf("Could not connect to Telegram. Make sure you are ")
log.Printf("connected to the internet and have set the API key ")
log.Println("in config.go. Error details follow:")
log.Panicln(err)
}
return bot
}