forked from SUGUS-GNULinux/minolobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·133 lines (122 loc) · 3.72 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
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
// Copyright 2017-2018 SUGUS GNU/Linux <[email protected]>
//
// This file is part of Minolobot.
//
// Minolobot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Minolobot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Minolobot. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"log"
"math/rand"
"strings"
"time"
"github.com/SUGUS-GNULinux/minolobot/command"
"github.com/SUGUS-GNULinux/minolobot/config"
"github.com/SUGUS-GNULinux/minolobot/interaction"
"gopkg.in/telegram-bot-api.v4"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
func main() {
bot, err := tgbotapi.NewBotAPI(config.Token)
if err != nil {
log.Fatal(err)
} else {
config.BotName = "@" + bot.Self.UserName
log.Printf("Authorized on account %s", bot.Self.UserName)
}
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
log.Fatal(err)
}
// needs time to get all the data
time.Sleep(time.Millisecond * 500)
// flush the updates when the bot starts
for len(updates) != 0 {
<-updates
}
// gets the channel to receive the pole signal
listTasks := []func(string) string{interaction.CheckPs, interaction.CheckCion,
interaction.AnswerDeGoma}
//////////////////
// starts routines
//////////////////
go func(bot *tgbotapi.BotAPI) {
doPole := interaction.StartPoleLogic()
// every pole signal sends pole message to every registered group
for range doPole {
chatsConfig, _ := config.FindAllChatConfigWithId()
for id, chatConfig := range chatsConfig {
if chatConfig.Enabled && chatConfig.Pole {
msg := tgbotapi.NewMessage(id, "pole")
bot.Send(msg)
}
}
}
}(bot)
nextUpdate:
for update := range updates {
if update.Message == nil {
continue
}
chatID := update.Message.Chat.ID
// chat id registration and configuration
chatConfig, registered := config.FindChatConfig(chatID)
if registered != nil {
chatConfig = config.CreateChatConfig(chatID, update.Message.Chat.IsGroup())
interaction.WelcomeInGroup(bot, update)
}
// command processing
if update.Message.IsCommand() {
ok := command.AnalyzeCommand(bot, update)
if ok {
continue nextUpdate
}
}
// who in sugus
interaction.Who(bot, update)
// detection of ignore not enable conditions
mentionOrPrivate := strings.Contains(update.Message.Text, config.BotName) ||
update.Message.Chat.IsPrivate()
// if activity is not enabled just tries to receive the commands.
// if the message starts with @<botName> or a private msg,
// it ignores the disabled state.
if !chatConfig.Enabled && !mentionOrPrivate {
continue
}
if interaction.CheckDisable(bot, update) {
continue nextUpdate
}
// pattern processing
s := string(update.Message.Text)
for _, task := range listTasks {
// if we get content to send from an interaction generator function we send it
if modString := task(s); modString != "" {
msg := tgbotapi.NewMessage(chatID, modString)
msg.ReplyToMessageID = update.Message.MessageID
msg.ParseMode = "MARKDOWN"
bot.Send(msg)
continue nextUpdate
}
}
// random answer
if rand.Intn(99) < chatConfig.PercentAnswer {
msg := tgbotapi.NewMessage(chatID, interaction.Reply())
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
}
}
}