-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.go
357 lines (344 loc) · 10 KB
/
library.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package main
import (
"encoding/json"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/alfredxing/calc/compute"
"io"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
)
func init() {
//Add all additional functions created to this dictionary
ottomap = map[string]interface{}{"date": Date,
"dead chat": DeadChat,
"otto help": Help,
"otto random": Random, //generates random number
"otto say": Say,
"otto roll": Roll,
"otto mock": Mock, //repeats previous text with random capitalization, like spongebob mocking meme
"otto flip": Flip, //flips a coin
"otto magic": Magic,
"otto will": Magic,
"otto tod": ToD, //Truth or Dare
"otto ToD": ToD,
"otto Tod": ToD,
"otto weather": Weather,
"otto calc": Calc,
"egg": Egg, //eightball easter egg
"otto hello": "hello there!",
"otto version": "I am Version 1.3.0",
"otto what": "I am a imessage virtual assistant that runs when Peter's computer is on. Type 'otto help' to see all the commands I can do.",
"hi otto ": "hi there!",
"otto time": Time,
"otto thanks": "you're welcome",
"otto google": Google, //gets first span
"otto wiki": Wiki, //link
"otto info": Wikitext, //intro paragraph
}
}
//FUNCTIONS
func DeadChat() {
//dead chat is a game that we would play on our group message, when no one has texted for a while someone says
//"dead chat", and the person who replies with "dead chat" the quickest wins.
file, err := ioutil.ReadFile(location)
if err != nil {
panic(err)
}
Data := Results{}
if Data.Chat.Lastext == "dead chat" {
winner := Dat.Chat.Lastperson
return fmt.Sprintf(winner, "has won dead chat!")
}
else {
break
}
}
func ToD(message string) string {
//truth or dare
list := Data.TruthOrDare["dares"]
rand.Seed(time.Now().UTC().UnixNano())
if (message != " truth" && message != " dare" && randbool()) || (message == " truth") {
list = Data.TruthOrDare["truths"]
}
index, _ := strconv.Atoi(randint(0, len(list)-1))
return list[index]
}
func Wiki(message string) string {
if message == "" {
return "search wikipedia for what?"
}
url := fmt.Sprintf("https://en.wikipedia.org/w/api.php?action=opensearch&search=%s&limit=1&namespace=0&format=json", strings.Replace(message[1:], " ", "%20", -1))
resp, err := http.Get(url)
if err != nil {
return "read error wikisearch" + err.Error()
}
var result1 []interface{}
err = json.NewDecoder(resp.Body).Decode(&result1)
if err != nil {
return "decode error wikisearch " + err.Error()
}
defer resp.Body.Close()
urllist := result1[len(result1)-1].([]interface{})
if len(urllist) == 0 {
return "Wikipedia couldn't find that page"
}
newurl := urllist[0].(string)
return newurl
}
func Wikitext(message string) string {
newurl := Wiki(message)
if strings.Contains(newurl, "Wikipedia couldn't find that page") || strings.Contains(newurl, "error wikisearch") {
return newurl
}
page := strings.Split(newurl, "/")
pageurl := fmt.Sprintf("https://en.wikipedia.org/w/api.php?action=query&prop=extracts&exintro&explaintext&titles=%s&format=json", strings.Replace(page[len(page)-1], "_", "%20", -1))
//PART 2
type Wikipage struct {
Pageid int `json:"pageid"`
Ns int `json:"ns"`
Title string `json:"title"`
Extract string `json:"extract"`
}
type Wikidata struct {
Complete string `json:"batchcomplete"`
Query map[string]map[string]Wikipage `json:"query"`
}
resp, err := http.Get(pageurl)
if err != nil {
return "read error wikidata " + err.Error()
}
wikidata := &Wikidata{}
err = json.NewDecoder(resp.Body).Decode(wikidata)
if err != nil {
return "decode error wikidata " + err.Error()
}
defer resp.Body.Close()
var result string
for k, _ := range wikidata.Query {
for k1, _ := range wikidata.Query[k] {
result = wikidata.Query[k][k1].Extract
break
}
break
}
if len(result) > 500 {
return result[:500] + "..."
}
return result
}
func Google(message string) string {
if message == "" {
return "google what? NOTE:this doesn't work perfectly"
}
url := "http://www.google.com/search?q=" + strings.Replace(strings.Replace(strings.Replace(message[1:], " ", "|~|", -1), "+", "%2B", -1), "|~|", "+", -1)
response, err := http.Get(url)
if err != nil {
return err.Error()
}
defer response.Body.Close()
doc, err := goquery.NewDocumentFromReader(io.Reader(response.Body))
if err != nil {
return err.Error()
}
var returntext string
valid := true
doc.Find("span").Each(func(i int, s *goquery.Selection) {
text := s.Text()
if strings.Contains(text, "days ago") == false && strings.Contains(text, "day ago") == false && text != "" && valid == true {
valid = false
returntext = text
}
})
return returntext
}
func Date() string {
t := time.Now()
format := fmt.Sprintf("Today is %s, %s %d, %d", t.Weekday(), t.Month(), t.Day(), t.Year())
return format
}
func Help() string {
keys := make([]string, len(ottomap))
i := 0
for k := range ottomap {
keys[i] = k
i++
}
newmessage := "Commands include:\n" + strings.Join(keys, ", ")
return newmessage
}
func Random(message string) string {
//returns random number between two integers
if message == "" {
return "usage:random low high"
}
values := strings.Split(message[1:], " ")
low, err := strconv.Atoi(values[0])
if err != nil {
return "invalid low number"
}
high, err := strconv.Atoi(values[1])
if err != nil {
return "invalid high number"
}
if high-low <= 0 {
return "high must be bigger then low"
}
rand.Seed(time.Now().UTC().UnixNano())
return randint(low, high)
}
func Egg(message string) string {
//easter eggs "hidden" in program, you can change them in settings.json
if message == "" {
return "for example:`otto egg peace,hippie=Peace dude`"
}
parts := strings.Split(message[1:], "=")
if len(parts) != 2 {
return "Invalid easter egg for otto magic."
}
triggers := strings.Split(parts[0], ",")
for _, trigger := range triggers {
if len(trigger) < 4 {
return "all trigger words must be at least 4 characters."
}
}
egg := parts[1]
Data.Eightball.Eastereggs[egg] = triggers
return fmt.Sprintf("added easter egg for '%s' that is triggered by %v", egg, triggers)
}
func Say(message string) string {
if message == "" {
return "say what?"
}
return message[1:]
}
func Roll(message string) string {
//rolls given number of dice with given number of sides
if message == "" {
return "roll what? ex. 2d20"
}
sections := strings.Split(message[1:], "d")
dice, err := strconv.Atoi(sections[0])
if err != nil {
return "invalid dice amount"
}
num, err := strconv.Atoi(sections[1])
if err != nil {
return "invalid high roll"
}
if dice > 100 || num > 100 {
return "highest number to roll / dice to have is 100"
}
var result []string
rand.Seed(time.Now().UTC().UnixNano())
for i := 0; i < dice; i++ {
result = append(result, randint(1, num))
}
return strings.Join(result, ",")
}
func Mock(message string) string {
rand.Seed(time.Now().UTC().UnixNano())
ftext := ""
for _, v := range Data.Chat.Lasttext {
sv := string(v)
mybool := randbool()
if mybool == true {
ftext += strings.ToUpper(sv)
} else {
ftext += strings.ToLower(sv)
}
}
return ftext
}
func Flip(message string) string {
rand.Seed(time.Now().UTC().UnixNano())
state := randbool()
if state == true {
return "heads"
} else {
return "tails"
}
}
func Magic(message string) string {
rand.Seed(time.Now().UTC().UnixNano())
for key, value := range Data.Eightball.Eastereggs {
for _, keyword := range value {
if strings.Contains(message, keyword) {
return key
}
}
}
//normal, no secrets
num, _ := strconv.Atoi(randint(0, len(Data.Eightball.Phrases)-1))
return Data.Eightball.Phrases[num]
}
func Weather(message string) string {
var location string
if message == "" {
location = Data.Weather.Default
} else {
location = strings.ToLower(message)
}
url := fmt.Sprintf("http://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=imperial", location, Data.Weather.Apikey)
type WeatherDecoder struct {
Main map[string]interface{} `json:"main"`
Name string `json:"name"`
Weather []map[string]interface{} `json:"weather"` //description
Coord map[string]float64 `json:"coord"`
Base string `json:"base"`
Visibility int `json:"visibility"`
Wind map[string]float64 `json:"wind"`
Clouds map[string]int `json:"clouds"`
DateInSeconds int `json:"dt"`
Sys map[string]float64 `json:"sys"`
Id int `json:"id"`
Cod int `json:"cod"`
}
resp, err := http.Get(url)
if err != nil {
return err.Error()
}
weather := &WeatherDecoder{}
json.NewDecoder(resp.Body).Decode(weather)
response := fmt.Sprintf("Right now in %s, it is %.2f degrees. The weather is %s and there is %.0f%% humidity.",
weather.Name, weather.Main["temp"], weather.Weather[0]["main"], weather.Main["humidity"])
return response
}
func Calc(message string) string {
if message == "" {
return "Calculate what?"
}
res, err := compute.Evaluate(message)
if err != nil {
return err.Error()
}
return strconv.FormatFloat(res, 'f', 4, 64)
}
func Time() string {
return "It is " + time.Now().Format(time.Kitchen)
}
type WeatherSettings struct {
Default string `json:"default"`
Apikey string `json:"apikey"`
}
type EightballSettings struct {
Phrases []string `json:"phrases"`
Eastereggs map[string][]string `json:"eastereggs"`
}
type ChatSettings struct {
Lastperson string `json:"lastperson"`
Lastamount int `json:"lastamount"`
Lasttext string `json:"lasttext"`
Lasttextperson string `json:"lasttextperson"`
}
type Results struct {
Weather WeatherSettings `json:"weather"`
Chat ChatSettings `json:"chat"`
Errormessage string `json:"errormessage"`
Maxmessage string `json:"maxmessage"`
Eightball EightballSettings `json:"eightball"`
TruthOrDare map[string][]string `json:"truthordare"`
}