feat: Added telegram message sending

This commit is contained in:
2025-01-17 00:50:41 +03:00
parent 7bc567d432
commit 4aea2d0d07
4 changed files with 54 additions and 3 deletions

View File

@ -1,12 +1,21 @@
package main
import (
"log"
"git.inkling.su/mrqiz/schedule-poll/internal/config"
"git.inkling.su/mrqiz/schedule-poll/internal/schedule"
"git.inkling.su/mrqiz/schedule-poll/internal/telegram"
"github.com/robfig/cron/v3"
)
func main() {
err := telegram.GetMe()
if err != nil {
log.Fatalln("Unable to connect to telegram", err)
}
c := cron.New(cron.WithSeconds())
c.AddFunc(config.AppConfig.CronPattern, schedule.PollSchedule)
c.Run()

View File

@ -8,7 +8,10 @@ import (
)
type config struct {
TelegramBotToken string
Telegram struct {
Token string
ChatId string
}
Schedule struct {
BaseUrl string
FacultyId string
@ -32,6 +35,7 @@ func init() {
AppConfig.Schedule.FacultyId = os.Getenv("SCHEDULE_FACULTY_ID")
AppConfig.Schedule.GroupId = os.Getenv("SCHEDULE_GROUP_ID")
AppConfig.Schedule.StartDate = os.Getenv("SCHEDULE_START_DATE")
AppConfig.TelegramBotToken = os.Getenv("SCHEDULE_TELEGRAM_TOKEN")
AppConfig.Telegram.Token = os.Getenv("SCHEDULE_TELEGRAM_TOKEN")
AppConfig.Telegram.ChatId = os.Getenv("SCHEDULE_TELEGRAM_CHAT_ID")
AppConfig.CronPattern = os.Getenv("SCHEDULE_CRON_PATTERN")
}

View File

@ -6,6 +6,7 @@ import (
"os"
"git.inkling.su/mrqiz/schedule-poll/internal/config"
"git.inkling.su/mrqiz/schedule-poll/internal/telegram"
)
func PollSchedule() {
@ -39,6 +40,7 @@ func PollSchedule() {
siteUrl)
os.WriteFile(flagFilePath, []byte(msg), os.FileMode(0777))
err = telegram.SendMessage(config.AppConfig.Telegram.ChatId, msg)
log.Printf("Found %d days", len(week.Days))
}
}

36
internal/telegram/api.go Normal file
View File

@ -0,0 +1,36 @@
package telegram
import (
"fmt"
"git.inkling.su/mrqiz/schedule-poll/internal/config"
"github.com/go-resty/resty/v2"
)
var client = resty.New().SetBaseURL("https://api.telegram.org")
func GetMe() error {
_, err := client.R().
Get(fmt.Sprintf("/bot%s/getMe", config.AppConfig.Telegram.Token))
if err != nil {
return err
}
return nil
}
func SendMessage(chatId, message string) error {
_, err := client.R().
SetQueryParams(map[string]string{
"chat_id": chatId,
"text": message,
}).
Get(fmt.Sprintf("/bot%s/sendMessage", config.AppConfig.Telegram.Token))
if err != nil {
return err
}
return nil
}