Skip to content

Instantly share code, notes, and snippets.

@michaelfmnk
Created August 27, 2022 22:34
Show Gist options
  • Save michaelfmnk/fe6407f58ffffc418a91416be909d03f to your computer and use it in GitHub Desktop.
Save michaelfmnk/fe6407f58ffffc418a91416be909d03f to your computer and use it in GitHub Desktop.
bh notifier bot in go
package main
import (
"fmt"
"github.com/go-co-op/gocron"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"time"
)
import (
"encoding/csv"
"net/http"
)
var (
location = time.FixedZone("Europe/Kiev", +3*60*60)
chatId int64 = 000
token = "[bot token]"
layout = "1/2/2006 3:04:05"
csvLink = "https://docs.google.com/spreadsheets/d/[sheet-id]/gviz/tq?tqx=out:csv&sheet=Sh1"
)
func main() {
bot, _ := tgbotapi.NewBotAPI(token)
s := gocron.NewScheduler(time.UTC)
_, _ = s.Every(1).Day().At("21:00").Do(task(bot, 0))
_, _ = s.Every(1).Day().At("23:00").Do(task(bot, 0))
_, _ = s.Every(1).Day().At("01:00").Do(task(bot, -1))
s.StartBlocking()
}
func task(bot *tgbotapi.BotAPI, delta int) func() {
return func() {
lastSubmissionDate := getLastSubmissionDate()
date := time.Now().In(location).Add(time.Hour * 24 * time.Duration(delta))
if lastSubmissionDate.Year() < date.Year() ||
(lastSubmissionDate.Year() == date.Year() && lastSubmissionDate.YearDay() < date.YearDay()) {
msg := tgbotapi.NewMessage(chatId, fmt.Sprintf("Please, send your form for %s", date.Format("`2/1/2006`")))
_, _ = bot.Send(msg)
}
}
}
func getLastSubmissionDate() time.Time {
res, _ := http.Get(csvLink)
records, _ := csv.NewReader(res.Body).ReadAll()
lastDate := records[len(records)-1][1]
parse, _ := time.ParseInLocation(layout, lastDate, location)
return parse
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment