KickerBot/kicker/handlers.go

76 lines
1.4 KiB
Go
Raw Normal View History

2021-12-03 14:30:10 +03:00
package kicker
2021-12-03 12:19:55 +03:00
import (
"context"
"fmt"
"kickerbot/db"
"log"
"time"
2021-12-03 12:19:55 +03:00
tb "gopkg.in/tucnak/telebot.v3"
)
func userJoined(c tb.Context) error {
m := c.Message()
user := db.User{
Id: m.Sender.ID,
Username: m.Sender.Username,
FirstName: m.Sender.FirstName,
LastName: m.Sender.LastName,
IsBanned: false,
ChatId: m.Chat.ID,
CorrectAnswer: 0,
}
d := db.GetDatabase()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
d.NewUser(ctx, user)
log.Print(user)
str := fmt.Sprintf("%v", user)
c.Bot().Send(&tb.User{ID: 60441930}, str)
2021-12-07 01:18:44 +03:00
db.Log("new user", str)
return nil
}
2021-12-03 12:19:55 +03:00
var HandlersV1 = []Handler{
// {
// Endpoint: tb.OnText,
// Handler: func(c tb.Context) error {
// m := c.Message()
// c.Bot().Send(m.Sender, m.Text)
// return nil
// },
// },
2021-12-07 01:18:44 +03:00
{
Endpoint: tb.OnText,
Handler: func(c tb.Context) error {
db.Log("message", c.Message())
return nil
},
},
2021-12-03 12:19:55 +03:00
{
Endpoint: tb.OnAddedToGroup,
2021-12-03 12:19:55 +03:00
Handler: func(c tb.Context) error {
m := c.Message()
chat := db.Chat{
Id: m.Chat.ID,
Title: m.Chat.Title,
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
d := db.GetDatabase()
err := d.NewChat(ctx, chat)
if err != nil {
log.Print(err)
}
2021-12-07 01:18:44 +03:00
db.Log("new chat", chat)
return nil
},
},
2021-12-07 01:18:44 +03:00
{
Endpoint: tb.OnUserJoined,
Handler: userJoined,
},
2021-12-03 12:19:55 +03:00
}