Уходим с Python на Go: первые успехи #5
2
main.go
2
main.go
|
@ -42,6 +42,8 @@ func main() {
|
||||||
bot.Start()
|
bot.Start()
|
||||||
} else if tg_user.PosTag == "add" {
|
} else if tg_user.PosTag == "add" {
|
||||||
bot.Find(msg.Text)
|
bot.Find(msg.Text)
|
||||||
|
} else if msg.Text == "Главное меню" {
|
||||||
|
bot.GetSummary()
|
||||||
} else {
|
} else {
|
||||||
bot.Etc()
|
bot.Etc()
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ type TgUser struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ShedulesInUser struct {
|
type ShedulesInUser struct {
|
||||||
UID int64 `xorm:"pk"`
|
UID int64 `xorm:"pk autoincr"` // Не забывать про автоинкремент!!!
|
||||||
L9Id int64
|
L9Id int64
|
||||||
IsTeacher bool
|
IsTeacher bool
|
||||||
SheduleId int64
|
SheduleId int64
|
||||||
|
|
|
@ -76,7 +76,7 @@ func Parse(doc *goquery.Document, isGroup bool, sheduleId int64, week int) (*She
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
idx := (len(raw_times)-2)*t/2 + d
|
idx := (len(raw_times)/2+1)*t/2 + d
|
||||||
lesson := Lesson{
|
lesson := Lesson{
|
||||||
Begin: begin,
|
Begin: begin,
|
||||||
End: end,
|
End: end,
|
||||||
|
|
|
@ -127,11 +127,11 @@ func (bot *Bot) Find(query string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bot *Bot) Confirm(query *tgbotapi.CallbackQuery) {
|
func (bot *Bot) Confirm(query *tgbotapi.CallbackQuery) error {
|
||||||
isGroup := bot.TG_user.PosTag == "confirm_group"
|
isGroup := bot.TG_user.PosTag == "confirm_group"
|
||||||
groupId, err := strconv.ParseInt(query.Data, 0, 64)
|
groupId, err := strconv.ParseInt(query.Data, 0, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
var groups []database.ShedulesInUser
|
var groups []database.ShedulesInUser
|
||||||
err = bot.DB.Find(&groups, &database.ShedulesInUser{
|
err = bot.DB.Find(&groups, &database.ShedulesInUser{
|
||||||
|
@ -140,7 +140,7 @@ func (bot *Bot) Confirm(query *tgbotapi.CallbackQuery) {
|
||||||
IsTeacher: !isGroup,
|
IsTeacher: !isGroup,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return err
|
||||||
}
|
}
|
||||||
if len(groups) == 0 {
|
if len(groups) == 0 {
|
||||||
shInUser := database.ShedulesInUser{
|
shInUser := database.ShedulesInUser{
|
||||||
|
@ -151,10 +151,15 @@ func (bot *Bot) Confirm(query *tgbotapi.CallbackQuery) {
|
||||||
bot.DB.InsertOne(shInUser)
|
bot.DB.InsertOne(shInUser)
|
||||||
bot.DeleteMsg(query)
|
bot.DeleteMsg(query)
|
||||||
msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Подключено!")
|
msg := tgbotapi.NewMessage(bot.TG_user.TgId, "Подключено!")
|
||||||
|
keyboard := tgbotapi.NewReplyKeyboard([]tgbotapi.KeyboardButton{tgbotapi.NewKeyboardButton("Главное меню")})
|
||||||
|
msg.ReplyMarkup = keyboard
|
||||||
bot.TG.Send(msg)
|
bot.TG.Send(msg)
|
||||||
|
|
||||||
bot.TG_user.PosTag = "ready"
|
bot.TG_user.PosTag = "ready"
|
||||||
bot.DB.Update(bot.TG_user)
|
err = bot.UpdateUserDB()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
var msg string
|
var msg string
|
||||||
if isGroup {
|
if isGroup {
|
||||||
|
@ -165,9 +170,10 @@ func (bot *Bot) Confirm(query *tgbotapi.CallbackQuery) {
|
||||||
callback := tgbotapi.NewCallback(query.ID, msg)
|
callback := tgbotapi.NewCallback(query.ID, msg)
|
||||||
bot.TG.Request(callback)
|
bot.TG.Request(callback)
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bot *Bot) Cancel(query *tgbotapi.CallbackQuery) {
|
func (bot *Bot) Cancel(query *tgbotapi.CallbackQuery) error {
|
||||||
shedules, err := bot.DB.Count(&database.ShedulesInUser{
|
shedules, err := bot.DB.Count(&database.ShedulesInUser{
|
||||||
L9Id: bot.TG_user.L9Id,
|
L9Id: bot.TG_user.L9Id,
|
||||||
})
|
})
|
||||||
|
@ -176,7 +182,10 @@ func (bot *Bot) Cancel(query *tgbotapi.CallbackQuery) {
|
||||||
}
|
}
|
||||||
if shedules == 0 {
|
if shedules == 0 {
|
||||||
bot.TG_user.PosTag = "add"
|
bot.TG_user.PosTag = "add"
|
||||||
bot.DB.Update(bot.TG_user)
|
err = bot.UpdateUserDB()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
bot.DeleteMsg(query)
|
bot.DeleteMsg(query)
|
||||||
msg := tgbotapi.NewMessage(
|
msg := tgbotapi.NewMessage(
|
||||||
bot.TG_user.TgId,
|
bot.TG_user.TgId,
|
||||||
|
@ -185,8 +194,13 @@ func (bot *Bot) Cancel(query *tgbotapi.CallbackQuery) {
|
||||||
bot.TG.Send(msg)
|
bot.TG.Send(msg)
|
||||||
} else {
|
} else {
|
||||||
bot.TG_user.PosTag = "ready"
|
bot.TG_user.PosTag = "ready"
|
||||||
|
err = bot.UpdateUserDB()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
bot.DeleteMsg(query)
|
bot.DeleteMsg(query)
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bot *Bot) Etc() {
|
func (bot *Bot) Etc() {
|
||||||
|
|
|
@ -0,0 +1,129 @@
|
||||||
|
package tg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
|
||||||
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
|
"xorm.io/xorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (bot *Bot) GetSummary() {
|
||||||
|
now := time.Now()
|
||||||
|
log.Println(now.Format("01-02-2006 15:04:05 -07"), now.Format("01-02-2006 15:04:05"))
|
||||||
|
|
||||||
|
var lessons []database.Lesson
|
||||||
|
var shedules []database.ShedulesInUser
|
||||||
|
bot.DB.ID(bot.TG_user.L9Id).Find(&shedules)
|
||||||
|
|
||||||
|
var groups []string
|
||||||
|
var teachers []string
|
||||||
|
|
||||||
|
for _, sh := range shedules {
|
||||||
|
if sh.IsTeacher {
|
||||||
|
teachers = append(teachers, strconv.FormatInt(sh.SheduleId, 10))
|
||||||
|
} else {
|
||||||
|
groups = append(groups, strconv.FormatInt(sh.SheduleId, 10))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
teachers_str := strings.Join(teachers, ",")
|
||||||
|
groups_str := strings.Join(groups, ",")
|
||||||
|
|
||||||
|
bot.DB.
|
||||||
|
Where("begin > ?", now.Format("2006-01-02 15:04:05")).
|
||||||
|
And("groupId in (?) or teacherId in (?)", groups_str, teachers_str).
|
||||||
|
OrderBy("begin").
|
||||||
|
Limit(6).
|
||||||
|
Find(&lessons)
|
||||||
|
|
||||||
|
log.Println(lessons)
|
||||||
|
|
||||||
|
if len(lessons) != 0 {
|
||||||
|
var firstPair, secondPair []database.Lesson
|
||||||
|
l_idx := 0
|
||||||
|
day := lessons[0].Begin
|
||||||
|
// Я хз, надо ли упарываться для случаев с более чем двумя подпарами
|
||||||
|
for lessons[l_idx].Begin == day && l_idx < len(lessons) {
|
||||||
|
firstPair = append(firstPair, lessons[l_idx])
|
||||||
|
l_idx++
|
||||||
|
}
|
||||||
|
if l_idx < len(lessons) {
|
||||||
|
day = lessons[l_idx].Begin
|
||||||
|
for lessons[l_idx].Begin == day && l_idx < len(lessons) {
|
||||||
|
secondPair = append(secondPair, lessons[l_idx])
|
||||||
|
l_idx++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Println(firstPair, secondPair)
|
||||||
|
|
||||||
|
var str string
|
||||||
|
if firstPair[0].Begin.Day() != time.Now().Day() {
|
||||||
|
str = "❗️Сегодня пар нет\nБлижайшие занятия "
|
||||||
|
if time.Until(firstPair[0].Begin).Hours() < 48 {
|
||||||
|
str += "завтра\n"
|
||||||
|
} else {
|
||||||
|
str += fmt.Sprintf("%s\n\n", firstPair[0].Begin.Format("02.01"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
firstPairStr, _ := PairToStr(firstPair, &bot.DB)
|
||||||
|
str += firstPairStr
|
||||||
|
|
||||||
|
if len(secondPair) != 0 && firstPair[0].Begin.Day() == secondPair[0].Begin.Day() {
|
||||||
|
secondPairStr, _ := PairToStr(secondPair, &bot.DB)
|
||||||
|
str += secondPairStr
|
||||||
|
}
|
||||||
|
msg := tgbotapi.NewMessage(bot.TG_user.TgId, str)
|
||||||
|
bot.TG.Send(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func PairToStr(pair []database.Lesson, db *xorm.Engine) (string, error) {
|
||||||
|
var str string
|
||||||
|
beginStr := pair[0].Begin.Format("15:04")
|
||||||
|
endStr := pair[0].End.Format("15:04")
|
||||||
|
str = fmt.Sprintf("📆 %s - %s\n", beginStr, endStr)
|
||||||
|
|
||||||
|
for _, sublesson := range pair {
|
||||||
|
var type_emoji string
|
||||||
|
switch sublesson.Type {
|
||||||
|
case "lect":
|
||||||
|
type_emoji = "📗"
|
||||||
|
case "pract":
|
||||||
|
type_emoji = "📕"
|
||||||
|
case "lab":
|
||||||
|
type_emoji = "📘"
|
||||||
|
case "other":
|
||||||
|
type_emoji = "📙"
|
||||||
|
default:
|
||||||
|
type_emoji = "📙"
|
||||||
|
}
|
||||||
|
str += fmt.Sprintf("%s%s\n", type_emoji, sublesson.Name)
|
||||||
|
if sublesson.Place != "" {
|
||||||
|
str += fmt.Sprintf("🧭%s\n", sublesson.Place)
|
||||||
|
}
|
||||||
|
if sublesson.TeacherId != 0 {
|
||||||
|
var t database.Teacher
|
||||||
|
_, err := db.ID(sublesson.TeacherId).Get(&t)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
name := fmt.Sprintf("%s %s.%s.", t.LastName, t.FirstName[0:2], t.MidName[0:2])
|
||||||
|
str += fmt.Sprintf("👤%s\n", name)
|
||||||
|
}
|
||||||
|
if sublesson.SubGroup != "" {
|
||||||
|
str += fmt.Sprintf("👥%s\n", sublesson.SubGroup)
|
||||||
|
}
|
||||||
|
if sublesson.Comment != "" {
|
||||||
|
str += fmt.Sprintf("💬%s\n", sublesson.Comment)
|
||||||
|
}
|
||||||
|
str += "--------------------------------\n"
|
||||||
|
}
|
||||||
|
return str, nil
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package tg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetSummary(t *testing.T) {
|
||||||
|
if err := godotenv.Load(); err != nil {
|
||||||
|
log.Fatal("No .env file found")
|
||||||
|
}
|
||||||
|
|
||||||
|
engine, err := database.Connect(os.Getenv("MYSQL_USER"), os.Getenv("MYSQL_PASS"), os.Getenv("MYSQL_DB"))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
bot := new(Bot)
|
||||||
|
err = bot.InitBot(os.Getenv("TELEGRAM_APITOKEN"), *engine)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
bot.TG_user = database.TgUser{
|
||||||
|
L9Id: 316268749,
|
||||||
|
TgId: 1086707888,
|
||||||
|
}
|
||||||
|
|
||||||
|
bot.GetSummary()
|
||||||
|
}
|
|
@ -46,3 +46,8 @@ func (bot *Bot) DeleteMsg(query *tgbotapi.CallbackQuery) {
|
||||||
delete := tgbotapi.NewDeleteMessage(query.From.ID, query.Message.MessageID)
|
delete := tgbotapi.NewDeleteMessage(query.From.ID, query.Message.MessageID)
|
||||||
bot.TG.Request(delete)
|
bot.TG.Request(delete)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bot *Bot) UpdateUserDB() error {
|
||||||
|
_, err := bot.DB.ID(bot.TG_user.L9Id).Update(bot.TG_user)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
Reference in New Issue