-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (58 loc) · 1.27 KB
/
main.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
package main
import (
"database/sql"
"log"
"os"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/joho/godotenv"
_ "github.com/mattn/go-sqlite3"
)
var db *sql.DB
func InitDB(filepath string) {
var err error
db, err = sql.Open("sqlite3", filepath)
if err != nil {
log.Fatal(err)
}
createTableSQL := `
CREATE TABLE IF NOT EXISTS users (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"telegram_id" INTEGER NOT NULL UNIQUE,
"cookie" TEXT NOT NULL,
"donemid" TEXT NOT NULL,
"alarm" BOOLEAN NOT NULL DEFAULT TRUE,
"grades" TEXT
);`
_, err = db.Exec(createTableSQL)
if err != nil {
log.Fatal(err)
}
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_BOT_TOKEN"))
if err != nil {
log.Panic(err)
}
InitDB("users.db")
defer db.Close()
bot.Debug = true
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
ticker := time.NewTicker(10 * time.Minute)
go func() {
for range ticker.C {
CheckForUpdates(bot)
}
}()
for update := range updates {
if update.Message != nil {
HandleMessage(bot, db, update.Message)
}
}
}