-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
executable file
·159 lines (143 loc) · 3.95 KB
/
Copy pathmain.go
File metadata and controls
executable file
·159 lines (143 loc) · 3.95 KB
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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"os"
"time"
"rsc.io/quote"
)
type Text struct {
Content string `json:"content"`
Username string `json:"username"`
Date string `json:"created"`
}
// Given the greatest existing ID in the API, this creates json for a new post
func CreatePost(username string, message string) ([]byte, error) {
//Creating properly formatted time
current := time.Now().Format("Mon Jan 2 2006 15:04:05 GMT-0700 (MST)")
text := Text{message, username, current}
// Marshal the string to json
jsonReq, err := json.Marshal(text)
if err != nil {
return nil, err
}
return jsonReq, nil
}
func MainOutput(out io.Writer, username string, message string) {
//testing
apiurl := os.Getenv("CHATBACK_URL")
if apiurl == "" {
panic("You must set the API environment variable using 'export CHATBACK_URL={ chatback url }' or adding -e ... in docker run command")
}
// Use the imported net/http to 'get' request and read from the API
botAPI, err := http.Get(apiurl)
if err != nil {
panic(err)
}
defer botAPI.Body.Close()
// Create the post with the correct ID
jsonpost, err := CreatePost(username, message)
if err != nil {
panic(err)
}
// POST the json to the API
resp, err := http.Post(
apiurl,
"application/json; charset=utf-8",
bytes.NewBuffer(jsonpost))
if err != nil {
panic(err)
}
defer resp.Body.Close()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
// Convert response body to string
// (question) How to make this only print if there's an error?
bodyString := string(bodyBytes)
fmt.Fprintln(out, bodyString)
}
func main() {
fmt.Println("The program has started!")
current := time.Now().Format("Mon Jan 2 2006 15:04:05 GMT-0700 (MST)")
var (
username = flag.String("username", "Reclass Robot", "a string")
message = flag.String("message", current, "a string")
interval = flag.Int("interval", 3, "an int")
random = flag.Bool("random", false, "a boolean")
preset = flag.String("preset", "", "a string")
)
flag.Parse()
overrideMessage := "Overriding username, message, random with preset"
var altMsgArr [3]string
switch *preset {
case "Tay":
fmt.Println(overrideMessage)
*username = "Tay"
*message = quote.Hello()
*random = false
*interval = 3
case "Kunal":
fmt.Println(overrideMessage)
*username = "Kunal"
*message = quote.Glass()
*random = false
*interval = 5
case "Theo":
fmt.Println(overrideMessage)
*username = "Theo"
*message = quote.Go()
*random = false
*interval = 7
case "Scott":
fmt.Println(overrideMessage)
*username = "Scott"
*message = quote.Opt()
*random = false
*interval = 9
case "Sabine":
fmt.Println(overrideMessage)
*username = "Sabine"
altMsgArr = [3]string{"Good morning Project Reclass!","Have you tried opening the ports?","I'm going to move the ticket!"}
*random = false
*interval = 3
case "Jose":
fmt.Println(overrideMessage)
*username = "Jose"
altMsgArr = [3]string{"Friendly reminder to submit your social posts!","How's everyone doing?","When are we going to eat our MREs?"}
*random = false
*interval = 4
case "Josiah":
fmt.Println(overrideMessage)
*username = "Josiah"
altMsgArr = [3]string{"I think we should ask Theo","bruhhh","I pushed the updated Dockerfile!"}
*random = false
*interval = 5
case "":
fmt.Println("No bot presets in use. Using defaults.")
default:
fmt.Println("Invalid preset selected. No preset applied.")
fmt.Println("Presets include 'Tay','Kunal','Theo','Scott'.")
}
// Repeatedly call the MainOutput() function
var timer *time.Timer
upperRange := *interval
rand.Seed(time.Now().UnixNano())
for {
if *random {
*interval = rand.Intn(upperRange)
fmt.Println("delaying " + fmt.Sprint(*interval) + " seconds")
}
if altMsgArr != [3]string{} {
randMsg := rand.Intn(3)
*message = altMsgArr[randMsg]
}
timer = time.NewTimer(time.Duration(*interval) * time.Second)
<-timer.C
MainOutput(os.Stdout, *username, *message)
}
}