mirror of
https://github.com/pischule/go-mention-all-bot.git
synced 2025-12-19 06:56:43 +00:00
Improve error logging
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
.env
|
.env
|
||||||
|
data
|
||||||
51
main.go
51
main.go
@@ -2,16 +2,17 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gorm.io/driver/sqlite"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
"gorm.io/gorm/clause"
|
|
||||||
"html"
|
"html"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/driver/sqlite"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/clause"
|
||||||
|
|
||||||
tele "gopkg.in/telebot.v3"
|
tele "gopkg.in/telebot.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,14 +26,20 @@ var DB *gorm.DB
|
|||||||
|
|
||||||
func ConnectDB() {
|
func ConnectDB() {
|
||||||
var err error
|
var err error
|
||||||
DB, err = gorm.Open(sqlite.Open(path.Join("data", "db.sqlite3")), &gorm.Config{})
|
|
||||||
|
err = os.MkdirAll(filepath.Join(".", "data"), os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("failed to connect database")
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
DB, err = gorm.Open(sqlite.Open(filepath.Join(".", "data", "db.sqlite3")), &gorm.Config{})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = DB.AutoMigrate(&ChatUser{})
|
err = DB.AutoMigrate(&ChatUser{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("failed to migrate")
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,23 +49,11 @@ func InitBot() *tele.Bot {
|
|||||||
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
|
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
|
||||||
ConnectDB()
|
|
||||||
b := InitBot()
|
|
||||||
b.Use(logger)
|
|
||||||
b.Handle("/start", handleStart)
|
|
||||||
b.Handle("/in", handleIn)
|
|
||||||
b.Handle("/out", handleOut)
|
|
||||||
b.Handle("/all", handleAll)
|
|
||||||
b.Handle("/stats", handleStats)
|
|
||||||
b.Start()
|
|
||||||
}
|
|
||||||
|
|
||||||
func logger(next tele.HandlerFunc) tele.HandlerFunc {
|
func logger(next tele.HandlerFunc) tele.HandlerFunc {
|
||||||
return func(c tele.Context) error {
|
return func(c tele.Context) error {
|
||||||
log.Println("user", c.Sender().ID, "sent", c.Message().Text)
|
log.Println("user", c.Sender().ID, "sent", c.Message().Text)
|
||||||
@@ -118,8 +113,8 @@ func handleAll(c tele.Context) error {
|
|||||||
if end > len(mentions) {
|
if end > len(mentions) {
|
||||||
end = len(mentions)
|
end = len(mentions)
|
||||||
}
|
}
|
||||||
err := c.Send(strings.Join(mentions[i:end], " "), tele.ModeHTML)
|
|
||||||
if err != nil {
|
if err := c.Send(strings.Join(mentions[i:end], " "), tele.ModeHTML); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -138,3 +133,15 @@ func handleStats(c tele.Context) error {
|
|||||||
msg := fmt.Sprintf("`Users: %6d\nChats: %6d\nGroups: %6d`", userCount, chatCount, groupCount)
|
msg := fmt.Sprintf("`Users: %6d\nChats: %6d\nGroups: %6d`", userCount, chatCount, groupCount)
|
||||||
return c.Send(msg, tele.ModeMarkdownV2)
|
return c.Send(msg, tele.ModeMarkdownV2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ConnectDB()
|
||||||
|
b := InitBot()
|
||||||
|
b.Use(logger)
|
||||||
|
b.Handle("/start", handleStart)
|
||||||
|
b.Handle("/in", handleIn)
|
||||||
|
b.Handle("/out", handleOut)
|
||||||
|
b.Handle("/all", handleAll)
|
||||||
|
b.Handle("/stats", handleStats)
|
||||||
|
b.Start()
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user