Skip to content

Commit

Permalink
Added reminder for OSDC meetups. (#24)
Browse files Browse the repository at this point in the history
* added reminder feature

* Changed variable names and added comments

* Changed input date and time format to include seperators

* Modified reminder schedule + minor changes
  • Loading branch information
DelusionalOptimist authored Jul 5, 2020
1 parent 3d293c6 commit 39543cf
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 12 deletions.
2 changes: 1 addition & 1 deletion telegram-bot/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.13

require (
github.com/anaskhan96/soup v1.1.1
github.com/go-co-op/gocron v0.1.1
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
go.mongodb.org/mongo-driver v1.3.0
golang.org/x/net v0.0.0-20191116160921-f9c825593386 // indirect
)
20 changes: 20 additions & 0 deletions telegram-bot/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ github.com/anaskhan96/soup v1.1.1/go.mod h1:pT5vs4HXDwA5y4KQCsKvnkpQd3D+joP7Iqpi
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-co-op/gocron v0.1.1 h1:OfDmkqkCguFtFMsm6Eaayci3DADLa8pXvdmOlPU/JcU=
github.com/go-co-op/gocron v0.1.1/go.mod h1:Y9PWlYqDChf2Nbgg7kfS+ZsXHDTZbMZYPEQ0MILqH+M=
github.com/go-redis/redis v6.15.5+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
Expand Down Expand Up @@ -32,10 +36,12 @@ github.com/gobuffalo/packd v0.1.0/go.mod h1:M2Juc+hhDXf/PnmBANFCqx4DM3wRbgDvnVWe
github.com/gobuffalo/packr/v2 v2.0.9/go.mod h1:emmyGweYTm6Kdper+iywB6YK5YzuKchGtJQZ0Odn4pQ=
github.com/gobuffalo/packr/v2 v2.2.0/go.mod h1:CaAwI0GPIAv+5wKLtv8Afwl+Cm78K/I/VCm/3ptBN+0=
github.com/gobuffalo/syncx v0.0.0-20190224160051-33c29581e754/go.mod h1:HhnNqWY95UYwwW3uSASeV7vtgYkT2t16hJgV3AEPUpw=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/karrick/godirwalk v1.8.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4=
Expand All @@ -51,6 +57,9 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE=
github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pelletier/go-toml v1.4.0/go.mod h1:PN7xzY2wHTK0K9p34ErDQMlFxa51Fk0OUruD3k1mMwo=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
Expand All @@ -70,6 +79,7 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
Expand All @@ -87,15 +97,22 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20190422162423-af44ce270edf/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5 h1:8dUaAV7K4uHsF56JQWkprecIQKdPHtR9jCHF5nB8uzc=
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20191116160921-f9c825593386 h1:ktbWvQrW08Txdxno1PiDpSxPXG6ndGsfnJjRRtkM0LQ=
golang.org/x/net v0.0.0-20191116160921-f9c825593386/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b h1:0mm1VjtFUOIlE1SbDlwjYaDxZVDP2S5ou6y0gSgXHu8=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190412183630-56d357773e84/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -114,4 +131,7 @@ golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgw
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
2 changes: 1 addition & 1 deletion telegram-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func help(ID int64) {
/irc - Find us on IRC.
/xkcd - Get a xkcd comic for you.
/dlmeetups - Get the list of upcoming meetups of Delhi/NCR communities.
/addmeetup* - Add the details of next OSDC Meetup.` + "\n" + `Format : /addmeetup <Title> <DD/MM/YYYY>
/addmeetup* - Add the details of next OSDC Meetup.` + "\n" + `Format : /addmeetup <Title> <DD/MM/YYYY Hr:Min> <Venue>
/nextmeetup - Get the details of next OSDC Meetup
To contribute to|modify this bot : https://github.com/vaibhavk/osdc-bots
* - Only for channel admins.
Expand Down
94 changes: 84 additions & 10 deletions telegram-bot/meetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ 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"
Expand All @@ -34,7 +38,7 @@ type meetuplist struct {
//struct to keep track of OSDC meetups using a JSON file
type meetupdata struct {
Name string
Date string
Date time.Time
Venue string
}

Expand Down Expand Up @@ -62,13 +66,39 @@ func addmeetup(ID int64, msgtext string, client mongo.Client) {
collection := client.Database("test").Collection("meetups")

args := strings.Fields(msgtext)
if len(args) == 4 {
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)
}
data := meetupdata{
Name: args[1],
Date: args[2],
Venue: args[3],
Date: time.Date(year, time.Month(month), day, hour, minutes, 0, 0, time.Local),
Venue: args[4],
}
_, err := collection.DeleteMany(context.TODO(), bson.D{{}})
_, err = collection.DeleteMany(context.TODO(), bson.D{{}})
if err != nil {
log.Fatal(err)
}
Expand All @@ -77,19 +107,63 @@ func addmeetup(ID int64, msgtext string, client mongo.Client) {
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(time.Local)

//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> <Date> <Venue>"))
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 result meetupdata
err := collection.FindOne(context.TODO(), bson.M{}).Decode(&result)
var data meetupdata
err := collection.FindOne(context.TODO(), bson.M{}).Decode(&data)
if err != nil {
log.Fatal(err)
}
nxtmeetupdata := "Details of next OSDC Meetup :" + "\n" + "Title -" + "\t" + result.Name + "\n" + "Date -" + "\t" + result.Date + "\n" + "Venue -" + result.Venue
bot.Send(tbot.NewMessage(ID, nxtmeetupdata))
//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")
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
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)
}
//reminder will be sent only if the the meetup is today
if time.Now().Local().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")
nxtMeetupData := "MEETUP REMINDER!" + "\n" + "Title -" + "\t" +
data.Name + "\n" + "Date -" + "\t" + timeString + "\n" + "Time -" +
"\t" + data.Date.Local().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))
}
}
}

0 comments on commit 39543cf

Please sign in to comment.