Skip to content

Commit 384c0e0

Browse files
fix bot
1 parent 6c928c6 commit 384c0e0

File tree

6 files changed

+35
-65
lines changed

6 files changed

+35
-65
lines changed

.github/workflows/lines-of-code.yml

Lines changed: 0 additions & 22 deletions
This file was deleted.

cmd/main.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"flag"
56
"log"
67
"os/signal"
78
"syscall"
@@ -10,7 +11,23 @@ import (
1011
"github.com/cool-dudes-alpha-dynamite-wolves/chto-tam-po-peresdacham/internal/parser"
1112
)
1213

14+
var (
15+
16+
// хранить секреты в коде плохо, поэтому используем флаги
17+
// "1953480583:AAEU7eBaZnCUt525oUkCMRCQxK1TJmaoVd4"
18+
botToken = flag.String("tg.token", "", "token for telegram")
19+
20+
// это не секрет, но для простоты тоже выносим под конфиг
21+
// "https://5872-95-165-1-28.eu.ngrok.io"
22+
WebhookURL = flag.String("tg.webhook", "", "webhook addr for telegram")
23+
24+
// запуск выглядит так:
25+
// go run bot.go -tg.token="1953480583:AAEU7eBaZnCUt525oUkCMRCQxK1TJmaoVd4" -tg.webhook="https://5872-95-165-1-28.eu.ngrok.io"
26+
)
27+
1328
func main() {
29+
flag.Parse()
30+
1431
ctx := context.Background()
1532

1633
// Инициализация парсера
@@ -19,15 +36,18 @@ func main() {
1936
log.Fatal("failed to initialize parser", err)
2037
}
2138

22-
// Парсинг данных с использованием кэша
23-
data, err := parser.ParseOnce()
39+
// Парсинг данных
40+
data, err := parser.Parse()
2441
if err != nil {
2542
log.Fatal("failed to parse input data", err.Error())
2643
}
2744
log.Println("Parsing completed!")
2845

2946
// Инициализация и запуск бота
30-
bot := bot.NewTgBot()
47+
bot, err := bot.NewTgBot(*botToken)
48+
if err != nil {
49+
log.Fatal("failed to initialize bot", err)
50+
}
3151
ctx, stop := signal.NotifyContext(ctx, syscall.SIGTERM, syscall.SIGINT)
3252
defer stop()
3353

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ module github.com/cool-dudes-alpha-dynamite-wolves/chto-tam-po-peresdacham
33
go 1.23.0
44

55
require (
6+
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
67
github.com/xuri/excelize/v2 v2.9.0
7-
golang.org/x/sync v0.8.0
88
)
99

1010
require (
11-
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect
1211
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
1312
github.com/richardlehane/mscfb v1.0.4 // indirect
1413
github.com/richardlehane/msoleps v1.0.4 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
2525
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
2626
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
2727
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
28-
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
29-
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
3028
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
3129
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
3230
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

internal/bot/tg_bot.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import (
99
"strings"
1010
"time"
1111

12-
"github.com/cool-dudes-alpha-dynamite-wolves/chto-tam-po-peresdacham/internal"
1312
telegram "github.com/go-telegram-bot-api/telegram-bot-api/v5"
13+
14+
"github.com/cool-dudes-alpha-dynamite-wolves/chto-tam-po-peresdacham/internal"
1415
)
1516

1617
type TgBot struct {
@@ -19,26 +20,29 @@ type TgBot struct {
1920
bot *telegram.BotAPI
2021
subjects []*internal.Subject // Данные из парсера
2122
}
23+
2224
type Subject struct {
2325
Name string // Название предмета
2426
Date time.Time // Дата пересдачи
2527
}
2628

27-
func NewTgBot() *TgBot {
29+
func NewTgBot(token string) (*TgBot, error) {
2830
errChan := make(chan error, 1)
29-
botAPI, err := telegram.NewBotAPI(token)
31+
bot, err := telegram.NewBotAPI(token)
3032
if err != nil {
31-
log.Fatalf("failed to initialize Telegram Bot API: %v", err)
33+
return nil, fmt.Errorf("failed to initialize Telegram Bot API: %v", err)
3234
}
3335

36+
// bot.Debug = true
37+
3438
return &TgBot{
3539
server: &http.Server{ReadHeaderTimeout: 1 * time.Second},
3640
errChan: errChan,
37-
bot: botAPI,
38-
}
41+
bot: bot,
42+
}, nil
3943
}
4044

41-
func (b *TgBot) Start(ctx context.Context, subjects []*internal.Subject) error {
45+
func (b *TgBot) Start(_ context.Context, subjects []*internal.Subject) error {
4246
b.subjects = subjects
4347
updateConfig := telegram.NewUpdate(0)
4448
updateConfig.Timeout = 60

internal/parser/excel_parser.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ import (
1717
"github.com/cool-dudes-alpha-dynamite-wolves/chto-tam-po-peresdacham/pkg"
1818
)
1919

20-
var (
21-
cachedSubjects []*internal.Subject
22-
cacheMutex sync.RWMutex
23-
)
24-
2520
type ExcelParser struct {
2621
pathToRetakes string
2722
}
@@ -41,30 +36,6 @@ func NewExcelParser(pathToRetakes string) (*ExcelParser, error) {
4136
}, nil
4237
}
4338

44-
func (p *ExcelParser) ParseOnce() ([]*internal.Subject, error) {
45-
// Защита от конкурентного доступа
46-
cacheMutex.RLock()
47-
defer cacheMutex.RUnlock()
48-
if cachedSubjects != nil {
49-
cacheMutex.RUnlock()
50-
return cachedSubjects, nil
51-
}
52-
cacheMutex.RUnlock()
53-
54-
// Если кэш пустой, выполняем парсинг
55-
subjects, err := p.Parse()
56-
if err != nil {
57-
return nil, err
58-
}
59-
60-
// Сохраняем данные в кэш
61-
cacheMutex.Lock()
62-
cachedSubjects = subjects
63-
cacheMutex.Unlock()
64-
65-
return cachedSubjects, nil
66-
}
67-
6839
func (p *ExcelParser) Parse() ([]*internal.Subject, error) {
6940
var files []string
7041
err := filepath.WalkDir(p.pathToRetakes, func(path string, d fs.DirEntry, _ error) error {

0 commit comments

Comments
 (0)