This repository was archived by the owner on Feb 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrantimator.go
88 lines (71 loc) · 1.74 KB
/
rantimator.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
package main
import (
"fmt"
"math/rand"
"net/http"
"os"
"strings"
"time"
"github.com/joho/godotenv"
log "github.com/sirupsen/logrus"
tb "gopkg.in/tucnak/telebot.v2"
)
var responses = []string{"Glad you got that off your chest.", "Feel better now?", "I feel with you."}
type pile []string
func (p *pile) String() string {
var s string
for i := len(*p) - 1; i >= 0; i-- {
s += (*p)[i] + "\n\n"
}
return s
}
var rants pile
func main() {
err := godotenv.Load()
if err != nil {
log.WithError(err).Fatal("Error loading .env file")
}
token := os.Getenv("TOKEN")
address := os.Getenv("ADDRESS")
debug := os.Getenv("DEBUG")
if debug != "" {
log.SetLevel(log.DebugLevel)
}
log.WithFields(log.Fields{
"Token": token,
"Address": address,
}).Debug("Config")
b, err := tb.NewBot(tb.Settings{
Token: token,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
log.WithError(err).Fatal("Unable to create bot")
return
}
b.Handle(tb.OnText, func(m *tb.Message) {
log.WithFields(log.Fields{
"ID": m.ID,
"Sender": m.Sender.Username,
"Message": m.Text,
}).Info("Rant received")
msg := strings.TrimSpace(m.Text)
if msg != "" {
rants = append(rants, m.Sender.FirstName+" howled at the moon: \""+msg+"\"")
}
if _, err := b.Send(m.Sender, responses[rand.Intn(len(responses))]); err != nil {
log.WithError(err).Error("Unable to respond")
}
})
http.HandleFunc("/", handler)
go func() {
log.Fatal(http.ListenAndServe(address, nil))
}()
b.Start()
}
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-type", "text/plain")
if _, err := fmt.Fprintf(w, "RANTIMATOR\n\n"+rants.String()); err != nil {
log.WithError(err).Error("Unable to handle request")
}
}