Skip to content
This repository was archived by the owner on Sep 4, 2019. It is now read-only.

Commit 05dc4e4

Browse files
committed
Merge branch 'develop'
2 parents a99708d + 65efbd4 commit 05dc4e4

File tree

5 files changed

+190
-7
lines changed

5 files changed

+190
-7
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ devlunch me| Returns details for the next listed Chadev Lunch talk
9797
devlunch url (date) (url) | Set live stream url for the dev lunch talks
9898
link to devlunch | Returns the link to the dev lunch live stream
9999
is `username` alive | Returns status of user
100+
\[groups\|meetups\] list | Lists all groups that are known to Ash
101+
\[groups\|meetups\] add (group name): (url) | Adds a new group to Ash
102+
\[group\|meetup\] details | Returns details about a group
103+
\[group\|meetup\] remove (group name) | Removes a group that Ash knows about
100104

101105
## Contributing
102106

events.go

+4
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ func getCalendarEvents() (string, error) {
176176
return "", err
177177
}
178178
}
179+
hal.Logger.Debugf("Access Token: %s", accessToken.Token)
179180

180181
URL := fmt.Sprintf("%s/[email protected]/events?access_token=%s&singleEvents=true&orderBy=startTime&timeMin=%s&maxResults=7",
181182
baseURL, url.QueryEscape(accessToken.Token),
@@ -190,6 +191,7 @@ func getCalendarEvents() (string, error) {
190191
if err != nil {
191192
return "", err
192193
}
194+
hal.Logger.Debugf("Google calendal response: %v", string(body))
193195

194196
var Events Event
195197

@@ -322,6 +324,7 @@ func getNextEvent() (string, error) {
322324
return "", err
323325
}
324326
}
327+
hal.Logger.Debugf("Access Token: %s", accessToken.Token)
325328

326329
URL := fmt.Sprintf("%s/[email protected]/events?access_token=%s&singleEvents=true&orderBy=startTime&timeMin=%s&maxResults=1",
327330
baseURL, url.QueryEscape(accessToken.Token),
@@ -336,6 +339,7 @@ func getNextEvent() (string, error) {
336339
if err != nil {
337340
return "", err
338341
}
342+
hal.Logger.Debugf("Google calendal response: %v", string(body))
339343

340344
var E Event
341345

groups.go

+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// Copyright 2014-2015 Chadev. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"encoding/json"
9+
"fmt"
10+
"strings"
11+
12+
"github.com/danryan/hal"
13+
)
14+
15+
// Groups contains data on the various dev groups.
16+
type Groups struct {
17+
// Name of the group
18+
Name string `json:"name"`
19+
// URL for the group page/meetup page
20+
URL string `json:"url"`
21+
// Meetup is the group name from the meetup URL
22+
// this is used for Meetup API calls.
23+
Meetup string `json:"meetup_name"`
24+
}
25+
26+
var groupListHandler = hear(`(groups|meetups) list`, "(groups|meetups) list", "Lists all groups that are known to Ash", func(res *hal.Response) error {
27+
groups, err := res.Robot.Store.Get("GROUPS")
28+
if err != nil {
29+
res.Send("I am currently unaware of any groups, try adding some")
30+
return err
31+
}
32+
33+
var g []Groups
34+
err = json.Unmarshal(groups, &g)
35+
if err != nil {
36+
hal.Logger.Errorf("error parsing JSON: %v", err)
37+
return res.Send("I had an error parsing the groups")
38+
}
39+
40+
var gn []string
41+
for _, val := range g {
42+
gn = append(gn, val.Name)
43+
}
44+
names := strings.Join(gn, ", ")
45+
46+
return res.Send(fmt.Sprintf("Here is a list of groups: %s", names))
47+
})
48+
49+
var groupAddHandler = hear(`(groups|meetups) add (.+): (.+)`, "(groups|meetups) add [group name]: [group url]", "Adds a new group to Ash", func(res *hal.Response) error {
50+
name := res.Match[2]
51+
url := res.Match[3]
52+
53+
if name == "" {
54+
hal.Logger.Warn("no group name given")
55+
return res.Send("I need a name for the group to add it.")
56+
}
57+
if url == "" {
58+
hal.Logger.Warn("no group url given")
59+
return res.Send("I need the url for the groups webpage or meetup group")
60+
}
61+
62+
var g []Groups
63+
groups, err := res.Robot.Store.Get("GROUPS")
64+
if len(groups) > 0 {
65+
err := json.Unmarshal(groups, &g)
66+
if err != nil {
67+
hal.Logger.Errorf("faild to parse json: %v", err)
68+
return res.Send("Failed to parse groups list")
69+
}
70+
}
71+
72+
var meetupName string
73+
if strings.Contains(url, "meetup.com") {
74+
meetupName = parseMeetupName(url)
75+
}
76+
77+
g = append(g, Groups{Name: name, URL: url, Meetup: meetupName})
78+
groups, err = json.Marshal(g)
79+
if err != nil {
80+
hal.Logger.Errorf("faild to build json: %v", err)
81+
return res.Send("Failed write updated groups list")
82+
}
83+
err = res.Robot.Store.Set("GROUPS", groups)
84+
if err != nil {
85+
hal.Logger.Error(err)
86+
return res.Send("Failed writing to the datastore")
87+
}
88+
89+
return res.Send("Added new group")
90+
})
91+
92+
var groupDetailsHandler = hear(`(group|meetup) details (.+)`, "(group|meetup) details [group name]", "Returns details about a group", func(res *hal.Response) error {
93+
name := res.Match[2]
94+
95+
var g []Groups
96+
groups, _ := res.Robot.Store.Get("GROUPS")
97+
if len(groups) > 0 {
98+
err := json.Unmarshal(groups, &g)
99+
if err != nil {
100+
hal.Logger.Errorf("faild to parse json: %v", err)
101+
return res.Send("Failed to parse groups list")
102+
}
103+
}
104+
105+
if len(groups) == 0 {
106+
hal.Logger.Error("no groups currently defined")
107+
return res.Send("I currently don't know of any groups, try adding some first")
108+
}
109+
110+
group := searchGroups(g, strings.ToLower(name))
111+
if group.Name == "" {
112+
hal.Logger.Warnf("no group with the name %s found", name)
113+
return res.Send(fmt.Sprintf("I could not find a group with the name %s", name))
114+
}
115+
116+
return res.Send(fmt.Sprintf("Group name: %s URL: %s", group.Name, group.URL))
117+
118+
})
119+
120+
var groupRemoveHandler = hear(`(group|meetup) remove (.+)`, "(group|meetup) remove [group name]", "Removes a group that ash knows about", func(res *hal.Response) error {
121+
name := res.Match[2]
122+
123+
var g []Groups
124+
groups, err := res.Robot.Store.Get("GROUPS")
125+
if err != nil {
126+
hal.Logger.Error("no groups currently in the datastore")
127+
res.Send("Sorry I don't know of any groups.")
128+
return err
129+
}
130+
131+
err = json.Unmarshal(groups, &g)
132+
if err != nil {
133+
hal.Logger.Errorf("couldn't unmarshal json: %v", err)
134+
res.Send("Sorry I was unable to parse the json object")
135+
return err
136+
}
137+
138+
for i, group := range g {
139+
if group.Name == name {
140+
// remove the group from the slice
141+
var e Groups
142+
g[len(g)-1], g = e, append(g[:i], g[i+1:]...)
143+
}
144+
}
145+
groups, err = json.Marshal(g)
146+
if err != nil {
147+
hal.Logger.Errorf("couldn't marshal json: %v", err)
148+
res.Send("Sorry I was unable to generate json object")
149+
return err
150+
}
151+
err = res.Robot.Store.Set("GROUPS", groups)
152+
if err != nil {
153+
hal.Logger.Errorf("error writing to datastore: %v", err)
154+
res.Send("Sorry I was unable to update group listing")
155+
return err
156+
}
157+
158+
return res.Send("Group list updated")
159+
})
160+
161+
func parseMeetupName(u string) string {
162+
// meetup URLs are structured as www.meetup.com/(group name)
163+
parts := strings.Split(u, "/")
164+
165+
return parts[len(parts)-1]
166+
}
167+
168+
func searchGroups(g []Groups, n string) Groups {
169+
var group Groups
170+
for _, val := range g {
171+
if strings.ToLower(val.Name) == n {
172+
group = val
173+
}
174+
}
175+
176+
return group
177+
}

main.go

+4
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ func run() int {
121121
addTalkHandler,
122122
devTalkLinkHandler,
123123
isAliveHandler,
124+
groupListHandler,
125+
groupAddHandler,
126+
groupDetailsHandler,
127+
groupRemoveHandler,
124128
)
125129

126130
if err := robot.Run(); err != nil {

whois.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@ var isHandler = hear(`(.+) is (.+)`, "(name) is (role)", "Tell Ash who the user
2727
key := strings.ToUpper(name)
2828
role := res.Match[2]
2929

30-
storedRoles, err := res.Robot.Store.Get(key)
31-
roleToStore := role
32-
if len(storedRoles) > 0 {
33-
roleToStore = roleToStore + ", " + string(storedRoles)
34-
}
35-
36-
err = res.Robot.Store.Set(key, []byte(roleToStore))
30+
err := res.Robot.Store.Set(key, []byte(role))
3731
if err != nil {
3832
res.Send("There's something wrong")
3933
return err

0 commit comments

Comments
 (0)