-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
189 lines (161 loc) · 4.45 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/mb-14/gomarkov"
tb "gopkg.in/tucnak/telebot.v2"
)
type Messages struct {
Name string `json:"name"`
Type string `json:"type"`
ID int64 `json:"id"`
Messages []struct {
ID int `json:"id"`
Type string `json:"type"`
Date string `json:"date"`
Actor string `json:"actor"`
ActorID int64 `json:"actor_id"`
Action string `json:"action"`
Title string `json:"title"`
Members []string `json:"members"`
Text string `json:"text"`
} `json:"messages"`
}
type SpaceApi struct {
State struct {
Open *bool `json:"open"`
} `json:"state"`
Sensors struct {
Temperature []struct {
Location string `json:"location"`
Unit string `json:"unit"`
Value float64 `json:"value"`
} `json:"temperature"`
Humidity []struct {
Location string `json:"location"`
Unit string `json:"unit"`
Value float64 `json:"value"`
} `json:"humidity"`
Carbondioxide []struct {
Location string `json:"location"`
Unit string `json:"unit"`
Value float64 `json:"value"`
} `json:"carbondioxide"`
}
}
func loadMarkovCorpus(chatHistoryFile string) *gomarkov.Chain {
var messages Messages
chain := gomarkov.NewChain(1)
// Parse json
jsonFile, _ := os.Open(chatHistoryFile)
byteValue, _ := io.ReadAll(jsonFile)
json.Unmarshal(byteValue, &messages)
// Add Lines to Chain
for i := 0; i < len(messages.Messages); i++ {
line := messages.Messages[i].Text
if len(line) > 1 {
chain.Add(strings.Split(line, " "))
}
}
return chain
}
func getMarkovSentence(chain *gomarkov.Chain) string {
tokens := []string{gomarkov.StartToken}
for tokens[len(tokens)-1] != gomarkov.EndToken {
next, _ := chain.Generate(tokens[(len(tokens) - 1):])
tokens = append(tokens, next)
}
return strings.Join(tokens[1:len(tokens)-1], " ")
}
func getStatusJson(url string) (status SpaceApi, err error) {
resp, err := http.Get(url)
if err != nil {
return status, err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
json.Unmarshal(body, &status)
return status, nil
}
func statusToString(status SpaceApi) string {
var info []string
// Door status
door := "Tuer: "
if status.State.Open != nil {
if *status.State.Open == true {
door += "offen"
} else {
door += "geschlossen"
}
} else {
door += "unbekannt"
}
info = append(info, door)
// Temperature sensor
if len(status.Sensors.Temperature) > 0 {
// FIXME: For now take the first available sensor
temp := status.Sensors.Temperature[0]
info = append(info, fmt.Sprintf("Temperatur: %.1f%s (%s)", temp.Value, temp.Unit, temp.Location))
}
// Humidity sensor
if len(status.Sensors.Humidity) > 0 {
// FIXME: For now take the first available sensor
humid := status.Sensors.Humidity[0]
info = append(info, fmt.Sprintf("Luftfeuchtigkeit: %.0f%s (%s)", humid.Value, humid.Unit, humid.Location))
}
// CO2 sensor
if len(status.Sensors.Carbondioxide) > 0 {
// FIXME: for now take the first available sensor
co2 := status.Sensors.Carbondioxide[0]
info = append(info, fmt.Sprintf("CO2: %.0f%s (%s)", co2.Value, co2.Unit, co2.Location))
}
return strings.Join(info[:], ", ")
}
func main() {
// Arguments
var (
apiToken = flag.String("apitoken", "", "Telegram API Token")
spacestatusUrl = flag.String("spacestatusurl", "", "The URL to the space status file")
chatHistoryFile = flag.String("chathistoryfile", "", "The JSON Telegram Chat Export to build markov chains from")
)
flag.Parse()
// Validate Arguments
if len(*apiToken) == 0 || len(*spacestatusUrl) == 0 || len(*chatHistoryFile) == 0 {
flag.Usage()
os.Exit(1)
}
b, err := tb.NewBot(tb.Settings{
Token: *apiToken,
// You can also set custom API URL. If field is empty it equals to "https://api.telegram.org"
// URL: "",
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
log.Fatal(err)
return
}
// Load and parse the telegram chat corpus at startup
chain := loadMarkovCorpus(*chatHistoryFile)
// K4CG Spacestatus in Channel
b.Handle("/status", func(m *tb.Message) {
status, err := getStatusJson(*spacestatusUrl)
if err != nil {
b.Send(m.Chat, "Oops... something went wrong. :(")
return
}
b.Send(m.Chat, statusToString(status))
})
// Markov Chain output in Channel
b.Handle("/sprachassistentin", func(m *tb.Message) {
sentence := getMarkovSentence(chain)
b.Send(m.Chat, sentence)
})
b.Start()
}