This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
devlunch.go
137 lines (112 loc) · 4.58 KB
/
devlunch.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
// Copyright 2014-2015 Chadev. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/chadev/Chadev_ircbot/meetup"
"github.com/danryan/hal"
)
// DevTalk contains the dev talk live stream details.
type DevTalk struct {
Date, URL string
}
var lunchHandler = hear(`is today (devlunch|dev lunch) day\b`, "is today devlunch day", "Tells if today is lunch day, and what the talk is", func(res *hal.Response) error {
d := time.Now().Weekday().String()
if d != "Thursday" {
msg, err := meetup.GetNextMeetup("chadevs")
if err != nil {
hal.Logger.Error(err)
return res.Send("Sorry I was unable to get details on the next dev lunch. Please check https://meetup.com/chadevs")
}
return res.Send(fmt.Sprintf("No, sorry! %s", msg))
}
msg, err := meetup.GetNextMeetup("chadevs")
if err != nil {
hal.Logger.Error(err)
return res.Send("Sorry I was unable to get details on the next dev lunch. Please check https://meetup.com/chadevs")
}
return res.Send(fmt.Sprintf("Yes! %s", msg))
})
var talkHandler = hear(`devlunch me`, "devlunch me", "Returns details on the next Chadev Lunch Talk", func(res *hal.Response) error {
msg, err := meetup.GetNextMeetup("chadevs")
if err != nil {
hal.Logger.Error(err)
return res.Send("Sorry I was unable to get details on the next dev lunch. Please check https://meetup.com/chadevs")
}
return res.Send(msg)
})
var addTalkHandler = hear(`devlunch url ([a-z0-9-\s]*)(http(s)?://.+)`, "devlunch url (date) (url)", "Set live stream url for dev lunch talks", func(res *hal.Response) error {
var d, u string
var date time.Time
// grab the arguments
d = strings.TrimSpace(res.Match[1])
u = res.Match[2]
// if d is empty or "today" use todays date
if d == "" || d == "today" {
date = time.Now()
} else {
var err error
date, err = time.Parse("2006-01-02", d)
if err != nil {
// could not parse the given date, fallback to today
hal.Logger.Error(err)
date = time.Now()
}
}
hal.Logger.Info(fmt.Sprintf("parsed date: %v", date.Format("2006-01-02")))
if !validateURL(u) {
return res.Send(fmt.Sprintf("%s is not a valid URL", u))
}
b, err := json.Marshal(DevTalk{Date: date.Format("2006-01-02"), URL: u})
if err != nil {
hal.Logger.Error(err)
return res.Send("I have failed you, I was unable to JSON")
}
err = res.Robot.Store.Set("devtalk", b)
if err != nil {
hal.Logger.Error(err)
return res.Send("I couldn't store the live stream details")
}
return res.Send("Dev Talk live stream details stored")
})
var devTalkLinkHandler = hear(`link to devlunch`, "link to devlunch", "Returns the link to the dev lunch live stream", func(res *hal.Response) error {
// check if today is Thursday
t := time.Now()
if t.Weekday().String() != "Thursday" {
return res.Send("Sorry today is not dev lunch day.")
}
// check if there is a url stored, and if the stored url is current
b, err := res.Robot.Store.Get("devtalk")
if err != nil || b == nil {
hal.Logger.Error(err)
return res.Send("Sorry, I don't have a URL for today's live stream. You can check if it is posted to the Meeup page at http://www.meetup.com/chadevs/ or our Google+ page at https://plus.google.com/b/103401260409601780643/103401260409601780643/posts")
}
var talk DevTalk
err = json.Unmarshal(b, &talk)
if err != nil {
hal.Logger.Error(err)
return res.Send("Sorry, I don't have a URL for today's live stream. You can check if it is posted to the Meeup page at http://www.meetup.com/chadevs/ or our Google+ page at https://plus.google.com/b/103401260409601780643/103401260409601780643/posts")
}
if talk.Date != t.Format("2006-01-02") {
return res.Send("Sorry, I don't have a URL for today's live stream. You can check if it is posted to the Meeup page at http://www.meetup.com/chadevs/ or our Google+ page at https://plus.google.com/b/103401260409601780643/103401260409601780643/posts")
}
return res.Send(fmt.Sprintf("You can access the live stream for the talk here %s", talk.URL))
})
var devlunchRSVPHandler = hear(`devlunch rsvps`, "devlunch rnvps", "Gets the RSVP count for Chadev Dev Lunches", func(res *hal.Response) error {
rsvp, err := meetup.GetMeetupRSVP("chadevs")
if err != nil {
hal.Logger.Errorf("failed fetching RSVP information: %v", err)
res.Send("I was unable to fetch the latest RSVP information for this group")
return err
}
if rsvp != "" {
res.Send(fmt.Sprintf("Dev Lunch RSVP breakdown: %s", rsvp))
} else {
res.Send("There are either no upcoming dev lunches or no RSVP for the event yet")
}
return nil
})