Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed the timezone used for saving meetups and scheduling reminders #44

Merged
merged 8 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions telegram-bot/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc/go.mod h1:Jhud4/sHM
go.mongodb.org/mongo-driver v1.3.0 h1:ew6uUIeJOo+qdUUv7LxFCUhtWmVv7ZV/Xuy4FAUsw2E=
go.mongodb.org/mongo-driver v1.3.0/go.mod h1:MSWZXKOynuguX+JSvwP8i+58jYCXxbia8HS3gZBapIE=
go.mongodb.org/mongo-driver v1.3.2 h1:IYppNjEV/C+/3VPbhHVxQ4t04eVW0cLp0/pNdW++6Ug=
go.mongodb.org/mongo-driver v1.4.2 h1:WlnEglfTg/PfPq4WXs2Vkl/5ICC6hoG8+r+LraPmGk4=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190422162423-af44ce270edf/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
Expand Down
38 changes: 25 additions & 13 deletions telegram-bot/meetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,14 @@ func addmeetup(ID int64, msgtext string, client mongo.Client) {
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, time.Local),
Date: time.Date(year, time.Month(month), day, hour, minutes, 0, 0, location),
Venue: args[4],
}
_, err = collection.DeleteMany(context.TODO(), bson.D{{}})
Expand All @@ -114,7 +119,7 @@ func addmeetup(ID int64, msgtext string, client mongo.Client) {
remindTime := fmt.Sprintf("%d:%d", hour-2, minutes)

//making a new scheduler
s1 := gocron.NewScheduler(time.Local)
s1 := gocron.NewScheduler(location)

//assigning the scheduler a task and then starting it
s1.Every(1).Day().At(remindTime).Do(reminder, ID, client, s1)
Expand All @@ -134,12 +139,17 @@ func nextmeetup(ID int64, client mongo.Client) {
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 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.Local().Format("Mon _2 Jan 2006")
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.Local().Format("15:04") + "\n" + "Venue -" + "\t" + data.Venue
data.Date.In(location).Format("15:04") + "\n" + "Venue -" + "\t" + data.Venue
bot.Send(tbot.NewMessage(ID, nxtMeetupData))
}

Expand All @@ -150,20 +160,22 @@ func reminder(ID int64, client mongo.Client, s1 *gocron.Scheduler) {
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().Local().Day() == data.Date.Day() {
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.Local().Format("Mon _2 Jan 2006")
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.Local().Format("15:04") + "\n" +
"\t" + data.Date.In(location).Format("15:04") + "\n" +
"Venue -" + "\t" + data.Venue
if !time.Now().Local().Before(data.Date) {
//stops the scheduler when meetup is done
s1.Clear()
} else {
bot.Send(tbot.NewMessage(ID, nxtMeetupData))
}
bot.Send(tbot.NewMessage(ID, nxtMeetupData))
//stops the scheduler when reminder is sent
s1.Clear()
}
}