-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
162 lines (139 loc) · 4.69 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/metskem/zaptecbot/cmds"
"github.com/metskem/zaptecbot/conf"
"github.com/metskem/zaptecbot/util"
"log"
"os"
"strings"
"time"
)
func main() {
// used for memory profiling, import net/http/pprof
//go func() {
// log.Println(http.ListenAndServe("localhost:6060", nil))
//}()
conf.EnvironmentComplete()
log.SetOutput(os.Stdout)
var err error
conf.Bot, err = tgbotapi.NewBotAPI(conf.BotToken)
if err != nil {
log.Panic(err.Error())
}
conf.Bot.Debug = conf.Debug
conf.Me, err = conf.Bot.GetMe()
meDetails := "unknown"
if err == nil {
meDetails = fmt.Sprintf("BOT: ID:%d UserName:%s FirstName:%s LastName:%s", conf.Me.ID, conf.Me.UserName, conf.Me.FirstName, conf.Me.LastName)
log.Printf("Bot started: %s, buildtime:%s, commit hash:%s", meDetails, conf.BuildTime, conf.CommitHash)
log.Printf("Configured chat ids: %v", conf.ChatIDs)
} else {
log.Printf("Bot.GetMe() failed: %v", err)
}
newUpdate := tgbotapi.NewUpdate(0)
newUpdate.Timeout = 60
updatesChan, err := conf.Bot.GetUpdatesChan(newUpdate)
if err == nil {
// start schedule-handler
go func() {
for range time.Tick(5 * time.Second) {
for _, schedule := range conf.ChargeSchedules {
if schedule.StartTime.Before(time.Now()) && schedule.InProgress == false {
cmds.StartStopCharger("start")
schedule.InProgress = true
conf.ChargeSchedules[schedule.Key()] = schedule
msg := fmt.Sprintf("schedule \"%s\" started", schedule.Key())
util.Broadcast(msg)
log.Println(msg)
}
if schedule.StartTime.Add(schedule.ChargeDuration).Before(time.Now()) && schedule.InProgress == true {
cmds.StartStopCharger("stop")
delete(conf.ChargeSchedules, schedule.Key()) // delete the schedule
msg := fmt.Sprintf("schedule \"%s\" has ended, %d schedules left", schedule.Key(), len(conf.ChargeSchedules))
util.Broadcast(msg)
log.Println(msg)
}
}
}
}()
// announce that we are live again
util.Broadcast(fmt.Sprintf("%s has been (re)started, buildtime: %s", conf.Me.UserName, conf.BuildTime))
// start listening for messages, and optionally respond
for update := range updatesChan {
if update.Message == nil { // ignore any non-Message Updates
log.Println("ignored null update")
} else {
chat := update.Message.Chat
mentionedMe, cmdMe := TalkOrCmdToMe(update)
// check if someone is talking to me:
if (chat.IsPrivate() || (chat.IsGroup() && mentionedMe)) && update.Message.Text != "/start" {
log.Printf("[%s] [chat:%d] %s\n", update.Message.From.UserName, chat.ID, update.Message.Text)
if cmdMe {
fromUser := update.Message.From.UserName
if chat.IsPrivate() {
fromUser = chat.UserName
}
// /status can be done by anyone, for the other cmds you need admin role
if util.IsAuthorized(chat.ID) {
HandleCommand(update)
} else {
util.SendMessage(chat.ID, fmt.Sprintf("sorry, %s is not allowed to send me that command", fromUser), true)
}
}
}
}
}
} else {
log.Printf(
"failed getting Bot updatesChannel, error: %v", err)
os.Exit(8)
}
}
// TalkOrCmdToMe - Returns if we are mentioned and if we were commanded
func TalkOrCmdToMe(update tgbotapi.Update) (bool, bool) {
entities := update.Message.Entities
var mentioned = false
var botCmd = false
if entities != nil {
for _, entity := range *entities {
if entity.Type == "mention" {
if strings.HasPrefix(update.Message.Text, fmt.Sprintf("@%s", conf.Me.UserName)) {
mentioned = true
}
}
if entity.Type == "bot_command" {
botCmd = true
if strings.Contains(update.Message.Text, fmt.Sprintf("@%s", conf.Me.UserName)) || update.Message.Chat.Type == "private" {
mentioned = true
}
}
}
}
// if another bot was mentioned, the cmd is not for us
if update.Message.Chat.IsGroup() && mentioned == false {
botCmd = false
}
return mentioned, botCmd
}
func HandleCommand(update tgbotapi.Update) {
if strings.HasPrefix(update.Message.Text, "/start") {
util.SendMessage(update.Message.Chat.ID, fmt.Sprintf("Hi %s (%s %s), the bot admin needs to add the chatId first before you can command me", update.Message.From.UserName, update.Message.From.FirstName, update.Message.From.LastName), false)
}
if strings.HasPrefix(update.Message.Text, "/state") {
cmds.State(update)
}
if strings.HasPrefix(update.Message.Text, "/debug") {
cmds.Debug(update)
}
if strings.HasPrefix(update.Message.Text, "/sa") {
cmds.ScheduleAdd(update)
}
if strings.HasPrefix(update.Message.Text, "/sd") {
cmds.ScheduleDelete(update)
}
if strings.HasPrefix(update.Message.Text, "/sl") {
cmds.ScheduleList(update)
}
}