-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriggers.go
160 lines (142 loc) · 4.17 KB
/
triggers.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
package main
import (
"fmt"
"log"
"log/slog"
"os"
"regexp"
"sort"
"strings"
tgbotapi "github.com/OvyFlash/telegram-bot-api"
"gopkg.in/yaml.v3"
)
type Trigger struct {
Name string `yaml:"name"`
Action string `yaml:"action"`
ActionText string `yaml:"actiontext"`
Conditions []Condition `yaml:"conditions"`
}
type Condition struct {
Type string `yaml:"type"`
Value string `yaml:"value"`
}
type combotTrigger struct {
Trigger []oldTrigger `yaml:"triggers"`
Section []Section `yaml:"sections"`
}
type oldTrigger struct {
Name string `yaml:"name"`
Conditions []oldCondition `yaml:"condition"`
CheckSubstring bool `default:"false" yaml:"substringSearch"`
CheckRegexp bool `default:"false" yaml:"regexpSearch"`
Actions string `yaml:"actiontext"`
ShowPreview bool `default:"false" yaml:"showpreview"`
Picture string `default:"" yaml:"picture"`
Section string `yaml:"section"`
}
type oldCondition struct {
Value string `yaml:"word"`
}
type Section struct {
Id string `yaml:"id"`
Name string `yaml:"name"`
}
var oldconfig combotTrigger
func CheckTriggerMessage(message *tgbotapi.Message) bool {
triggered := false
for _, trigger := range oldconfig.Trigger {
for _, condition := range trigger.Conditions {
if strings.EqualFold(message.Text, condition.Value) {
triggered = true
continue
}
if isQuestion(message.Text) && trigger.CheckSubstring {
if strings.Contains(strings.ToLower(message.Text), strings.ToLower(condition.Value)) {
triggered = true
continue
}
}
if isQuestion(message.Text) && trigger.CheckRegexp {
regex := regexp.MustCompile(condition.Value)
if regex.MatchString(message.Text) {
triggered = true
continue
}
}
}
if triggered {
trigger.Actions = strings.Replace(trigger.Actions, "{reply_to_namelink}", getNameLink(*message.From), -1)
var msg tgbotapi.Chattable
if len(trigger.Picture) > 0 {
photoConfig := tgbotapi.NewPhoto(message.Chat.ID, tgbotapi.FileID(trigger.Picture))
photoConfig.Caption = trigger.Actions
photoConfig.ParseMode = "HTML"
msg = photoConfig
} else {
messageConfig := tgbotapi.NewMessage(message.Chat.ID, trigger.Actions)
messageConfig.ParseMode = "HTML"
messageConfig.ReplyParameters.MessageID = message.MessageID
if !trigger.ShowPreview {
messageConfig.LinkPreviewOptions.IsDisabled = true
}
msg = messageConfig
}
if emulate {
log.Print("Emulate:TriggeredGood:", message.Text)
triggered = false
continue
}
_, err := bot.Request(msg)
if err != nil {
slog.Warn("TriggeredBad: ", "error", err, "message", message.Text)
}
triggered = false
slog.Info(fmt.Sprintf("TriggeredGood: %s", message.Text))
}
}
return triggered
}
func isQuestion(message string) bool {
return strings.Contains(message, "?")
}
func readTriggers() {
configFile, err := os.ReadFile("triggers.yaml")
if err != nil {
log.Panic(err)
}
err = yaml.Unmarshal(configFile, &oldconfig)
if err != nil {
log.Panic(err)
}
slog.Info(fmt.Sprintf("Triggers loaded: %d", len(oldconfig.Trigger)))
slog.Info(fmt.Sprintf("Sections loaded: %d", len(oldconfig.Section)))
sort.Slice(oldconfig.Trigger, func(i, j int) bool {
return oldconfig.Trigger[i].Name < oldconfig.Trigger[j].Name
})
}
func getSectionsList() []Section {
return oldconfig.Section
}
func getTriggersList() string {
message := ""
sections := getSectionsList()
sectionTriggers := make(map[string]string)
for _, trigger := range oldconfig.Trigger {
if len(trigger.Conditions) > 0 {
var words []string
for _, condition := range trigger.Conditions {
words = append(words, condition.Value)
}
//message = message + trigger.Name + ": " + strings.Join(words, "|") + "\r\n"
if len(trigger.Section) > 0 {
sectionTriggers[trigger.Section] = sectionTriggers[trigger.Section] + trigger.Name + ": " + strings.Join(words, "|") + "\r\n"
}
}
}
for _, section := range sections {
if len(sectionTriggers[section.Id]) > 0 {
message = message + "<b>" + section.Name + "</b>\r\n" + sectionTriggers[section.Id] + "\r\n"
}
}
return message
}