This repository was archived by the owner on Feb 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgroupmebot.go
More file actions
138 lines (119 loc) · 3.78 KB
/
groupmebot.go
File metadata and controls
138 lines (119 loc) · 3.78 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
package groupmebot
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"regexp"
"strings"
)
type GroupMeBot struct {
ID string `json:"bot_id"`
GroupID string `json:"group_id"`
Host string `json:"host"`
Port string `json:"port"`
TrackBotMessages bool `json:"trackbotmessages"`
Server string
Hooks map[string]func(InboundMessage) string
Logger
}
type InboundMessage struct {
Id string `json:"id"`
Avatar_url string `json:"avatar_url"`
Name string `json:"name"`
Sender_id string `json:"sender_id"`
Sender_type string `json:"sender_type"`
System bool `json:"system"`
Text string `json:"text"`
Source_guid string `json:"source_guid"`
Created_at int `json:"created_at"`
User_id string `json:"user_id"`
Group_id string `json:"group_id"`
Favorited_by []string `json:"favorited_by"`
Attachments []map[string]interface{} `json:"attachments"`
}
type OutboundMessage struct {
ID string `json:"bot_id"`
Text string `json:"text"`
}
// A CSVLogger comes with the bot, but any logger can be substituted so long as
// it satisfies this interface
type Logger interface {
LogMessage(msg InboundMessage)
}
/// This reads a json file containing the keys
/// See the example bot_cfg.json
/// Updates existing bot with parameters from JSON filename
/// Returns err from ioutil if file can not be read
func (b *GroupMeBot) ConfigureFromJson(filename string) error {
file, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal("Error reading bot configuration json file")
return err
}
// Parse out information from file
json.Unmarshal(file, b)
b.Server = b.Host + ":" + b.Port
log.Printf("Creating b at %s\n", b.Server)
b.Hooks = make(map[string]func(InboundMessage) string)
return err
}
func (b *GroupMeBot) SendMessage(outMessage string) (*http.Response, error) {
msg := OutboundMessage{b.ID, outMessage}
payload, err := json.Marshal(msg)
if err != nil {
return nil, err
}
j_payload := string(payload)
return http.Post("https://api.groupme.com/v3/bots/post", "application/json", strings.NewReader(j_payload))
}
func (b *GroupMeBot) AddHook(trigger string, response func(InboundMessage) string) {
b.Hooks[trigger] = response
}
func (b *GroupMeBot) HandleMessage(msg InboundMessage) {
resp := ""
for trig, hook := range b.Hooks {
matched, err := regexp.MatchString(trig, msg.Text)
if matched {
resp = hook(msg)
} else if err != nil {
log.Fatal("Error matching:", err)
}
}
if len(resp) > 0 {
log.Printf("Sending message: %v\n", resp)
_, err := b.SendMessage(resp)
if err != nil {
log.Fatal("Error when sending.", err)
}
}
}
/*
This is legitimate black magic, this is pretty cool, not usually able to do
things like this in other languages. This is a function that takes
a list of trigger functions and returns a function that can handle the Server
Requests
*/
func (b *GroupMeBot) Handler() http.HandlerFunc {
// Request Handler function
return func(w http.ResponseWriter, req *http.Request) {
if req.Method == "POST" {
//log.Println("Bot recieving and handling message.")
defer req.Body.Close()
var msg InboundMessage
err := json.NewDecoder(req.Body).Decode(&msg)
if err != nil {
log.Println("Couldn't parse the request body")
msg.Sender_type = "bot"
}
if msg.Sender_type != "bot" || b.TrackBotMessages {
b.LogMessage(msg)
// Find hook by running through hooklist
b.HandleMessage(msg)
}
} else {
//log.Println("Bot not responding to unknown message")
//io.WriteString(w, "GOTEM")
}
}
}