-
Notifications
You must be signed in to change notification settings - Fork 24
/
meetup.go
183 lines (169 loc) Β· 5.57 KB
/
meetup.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/go-co-op/gocron"
tbot "github.com/go-telegram-bot-api/telegram-bot-api"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
var finallist string //variable to store final list of meetups
//-------structs to parse json data from Meetup API
type venue struct {
Venue string `json:"name"`
}
type group struct {
GroupName string `json:"name"`
}
type meetuplist struct {
Name string `json:"name"`
Venue venue `json:"venue"`
Date string `json:"local_date"`
Time string `json:"local_time"`
Group group `json:"group"`
Link string `json:"link"`
}
//struct to keep track of OSDC meetups using a JSON file
type meetupdata struct {
Name string
Date time.Time
Venue string
}
//fetching details of meetup of the group's url from Meetup API
func getMeetups(url string) {
resp, err := http.Get(url)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
var meetups []meetuplist
json.Unmarshal([]byte(body), &meetups)
if (len(meetups)) > 0 {
finallist = finallist + "Title -" + "\t" + meetups[0].Name + "\n" + "Date -" + "\t" + meetups[0].Date + "\n" + "Link -" + "\t" + meetups[0].Link + "\n\n"
}
}
//Slicing the arguments (title & date of next meetup) from message text and writing them to a json file.
func addmeetup(ID int64, msgtext string, client mongo.Client) {
// Get a handle for your collection
collection := client.Database("test").Collection("meetups")
args := strings.Fields(msgtext)
if len(args) == 5 {
//separating the date and hour arguments so that we can use the
// dd/mm/yyyy and hh:min format
separatedDateStr := strings.Split(args[2], "/")
separatedHourStr := strings.Split(args[3], ":")
//converting the arguments into integers to pass them to time.Date
day, err := strconv.Atoi(separatedDateStr[0])
if err != nil {
log.Fatalln(err)
}
month, err := strconv.Atoi(separatedDateStr[1])
if err != nil {
log.Fatalln(err)
}
year, err := strconv.Atoi(separatedDateStr[2])
if err != nil {
log.Fatalln(err)
}
hour, err := strconv.Atoi(separatedHourStr[0])
if err != nil {
log.Fatalln(err)
}
minutes, err := strconv.Atoi(separatedHourStr[1])
if err != nil {
log.Fatalln(err)
}
location, err := time.LoadLocation("Asia/Kolkata")
if err != nil {
log.Print(err)
bot.Send(tbot.NewMessage(ID, "Couldn't detect the timezone, please try again"))
}
data := meetupdata{
Name: args[1],
Date: time.Date(year, time.Month(month), day, hour, minutes, 0, 0, location),
Venue: args[4],
}
_, err = collection.DeleteMany(context.TODO(), bson.D{{}})
if err != nil {
log.Fatal(err)
}
_, err = collection.InsertOne(context.TODO(), data)
if err != nil {
log.Fatal(err)
}
bot.Send(tbot.NewMessage(ID, "Meetup added successfully."))
//making a formatted string from time to pass it to s.At(). This will
//specify the time at which the reminder is sent. Currently 2 hours
//before the meetup
remindTime := fmt.Sprintf("%d:%d", hour-2, minutes)
//making a new scheduler
s1 := gocron.NewScheduler(location)
//assigning the scheduler a task and then starting it
s1.Every(1).Day().At(remindTime).Do(reminder, ID, client, s1)
s1.Start()
} else {
bot.Send(tbot.NewMessage(ID, "Please provide the details of meetup"+
" in this format - /addmeetup <Title> <DD/MM/YY Hr:Min> <Venue> "+
"\nTIME IN 24Hr FORMAT"))
}
}
//fetching the details (Next Meetup Title & Date) from the JSON file
func nextmeetup(ID int64, client mongo.Client) {
collection := client.Database("test").Collection("meetups")
var data meetupdata
err := collection.FindOne(context.TODO(), bson.M{}).Decode(&data)
if err != nil {
log.Print(err)
bot.Send(tbot.NewMessage(ID, "No meetup scheduled."))
} else {
location, err := time.LoadLocation("Asia/Kolkata")
if err != nil {
log.Print(err)
bot.Send(tbot.NewMessage(ID, "Couldn't detect the timezone while saving the meetup, please try again"))
}
//the string inside Format method is a sample string to specify the display
//format of meetupTimeString
timeString := data.Date.In(location).Format("Mon _2 Jan 2006")
nxtMeetupData := "Details of next OSDC Meetup :" + "\n" + "Title -" + "\t" +
data.Name + "\n" + "Date -" + "\t" + timeString + "\n" + "Time -" + "\t" +
data.Date.In(location).Format("15:04") + "\n" + "Venue -" + "\t" + data.Venue
bot.Send(tbot.NewMessage(ID, nxtMeetupData))
}
}
func reminder(ID int64, client mongo.Client, s1 *gocron.Scheduler) {
collection := client.Database("test").Collection("meetups")
var data meetupdata
err := collection.FindOne(context.TODO(), bson.M{}).Decode(&data)
if err != nil {
log.Fatal(err)
}
location, err := time.LoadLocation("Asia/Kolkata")
if err != nil {
log.Print(err)
bot.Send(tbot.NewMessage(ID, "Couldn't detect the timezone while processing the reminder, please try again"))
}
//reminder will be sent only if the the meetup is today
if time.Now().In(location).Day() == data.Date.Day() {
//the string inside Format method is a sample string to specify the display
//format of meetupTimeString
timeString := data.Date.In(location).Format("Mon _2 Jan 2006")
nxtMeetupData := "MEETUP REMINDER!" + "\n" + "Title -" + "\t" +
data.Name + "\n" + "Date -" + "\t" + timeString + "\n" + "Time -" +
"\t" + data.Date.In(location).Format("15:04") + "\n" +
"Venue -" + "\t" + data.Venue
bot.Send(tbot.NewMessage(ID, nxtMeetupData))
//stops the scheduler when reminder is sent
s1.Clear()
}
}