добавил принтфы об ошибках при отправке сообщений/файлов в тг
parent
a2e6957b1b
commit
64f8ac87e1
4
main.go
4
main.go
|
@ -90,8 +90,8 @@ func startSiteProcess(ip string) {
|
|||
if err = setupRoutes(s); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// Start subprocesses
|
||||
s.RunSubProcesses()
|
||||
// Start tickers
|
||||
s.RunTickers()
|
||||
// Start server
|
||||
if err := s.Run(ip + ":80"); err != nil {
|
||||
log.Fatal(err)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"log"
|
||||
"main/config"
|
||||
"main/mvc/models"
|
||||
"net/http"
|
||||
|
@ -11,7 +12,13 @@ import (
|
|||
// Отстукивает в ТГ
|
||||
func Backup(tmplname string, group *gin.RouterGroup, s *models.Site) {
|
||||
group.POST(tmplname, func(c *gin.Context) {
|
||||
go s.Bot.Backup(config.Cfg.DBPath, s.LogEntries, s.Cache)
|
||||
|
||||
go func() {
|
||||
if err := s.Bot.Backup(config.Cfg.DBPath, s.LogEntries, s.Cache); err != nil {
|
||||
log.Println("s.Bot.Backup() error: ", err)
|
||||
}
|
||||
}()
|
||||
|
||||
c.Redirect(http.StatusFound, "/adm/1?Отстук инициирован")
|
||||
})
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package controllers
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"main/mvc/models"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -68,7 +69,7 @@ func DownloadCache(group *gin.RouterGroup, s *models.Site) {
|
|||
group.GET("/loadcachedump", func(c *gin.Context) {
|
||||
path := "cache.json"
|
||||
if err := models.WriteCacheDumpToFile(s.Cache, path); err != nil {
|
||||
s.Bot.SendMessage(fmt.Sprintf("🔴 Ошибка записи дампа кэша в файл: %s", err.Error()))
|
||||
log.Println("models.WriteCacheDumpToFile() error: ", err)
|
||||
} else {
|
||||
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%d%s", time.Now().Unix(), path))
|
||||
c.Header("Content-Type", "application/octet-stream")
|
||||
|
|
|
@ -2,6 +2,7 @@ package controllers
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"main/mvc/models"
|
||||
"net/http"
|
||||
|
||||
|
@ -22,6 +23,12 @@ func SendMesssage(tmplname string, group *gin.RouterGroup, s *models.Site) {
|
|||
}
|
||||
|
||||
message := fmt.Sprintf("Анонимное сообщение от юзера %s:\n\n%s", c.ClientIP(), requestData.Body)
|
||||
go s.Bot.SendMessage(message)
|
||||
|
||||
go func() {
|
||||
if err := s.Bot.SendMessage(message); err != nil {
|
||||
log.Println("s.Bot.SendMessage() error: ", err)
|
||||
}
|
||||
}()
|
||||
|
||||
})
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package models
|
|||
import (
|
||||
"database/sql"
|
||||
"html/template"
|
||||
"log"
|
||||
"main/config"
|
||||
"main/tools"
|
||||
"sync"
|
||||
|
@ -39,12 +40,16 @@ func NewSiteCtx() *Site {
|
|||
}
|
||||
|
||||
// Запуск параллельных процессов
|
||||
func (s *Site) RunSubProcesses() {
|
||||
func (s *Site) RunTickers() {
|
||||
go tools.Ticker(config.Cfg.TgTickerTime*time.Hour, func() {
|
||||
s.Bot.Backup(config.Cfg.DBPath, s.LogEntries, s.Cache)
|
||||
if err := s.Bot.Backup(config.Cfg.DBPath, s.LogEntries, s.Cache); err != nil {
|
||||
log.Println("s.Bot.Backup() error: ", err)
|
||||
}
|
||||
})
|
||||
go tools.Ticker(config.Cfg.LastFmTickerTime*time.Second, func() {
|
||||
if track, err := s.LFM.LastTrack(); err == nil {
|
||||
if track, err := s.LFM.LastTrack(); err != nil {
|
||||
log.Println("s.LFM.LastTrack() error: ", err)
|
||||
} else {
|
||||
s.LastTrackAjaxBlock = s.LFM.TrackAjax(track)
|
||||
}
|
||||
})
|
||||
|
|
|
@ -27,7 +27,7 @@ func TGNew(token, id string) *TGBot {
|
|||
}
|
||||
}
|
||||
|
||||
func (b *TGBot) SendMessage(text string) {
|
||||
func (b *TGBot) SendMessage(text string) error {
|
||||
apiURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", b.Token)
|
||||
|
||||
data := url.Values{}
|
||||
|
@ -36,24 +36,26 @@ func (b *TGBot) SendMessage(text string) {
|
|||
|
||||
req, err := http.NewRequest("POST", apiURL, strings.NewReader(data.Encode()))
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *TGBot) SendFile(filePath, caption string) {
|
||||
func (b *TGBot) SendFile(filePath, caption string) error {
|
||||
apiURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendDocument", b.Token)
|
||||
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
|
@ -63,12 +65,12 @@ func (b *TGBot) SendFile(filePath, caption string) {
|
|||
|
||||
part, err := writer.CreateFormFile("document", filepath.Base(file.Name()))
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = io.Copy(part, file)
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
writer.WriteField("chat_id", b.ID)
|
||||
|
@ -76,12 +78,12 @@ func (b *TGBot) SendFile(filePath, caption string) {
|
|||
|
||||
err = writer.Close()
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", apiURL, body)
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
|
@ -89,26 +91,34 @@ func (b *TGBot) SendFile(filePath, caption string) {
|
|||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Telegram backup. Takes database path, query log, cache
|
||||
func (b *TGBot) Backup(dbpath string, log []string, cache *candycache.Cache) {
|
||||
func (b *TGBot) Backup(dbpath string, log []string, cache *candycache.Cache) error {
|
||||
// Send db
|
||||
b.SendFile(dbpath, "📦 Бэкап базы данных")
|
||||
if err := b.SendFile(dbpath, "📦 Бэкап базы данных"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create and send log file
|
||||
if err := tools.StringSliceToFile(log, "log.txt"); err != nil {
|
||||
b.SendMessage(fmt.Sprintf("🔴 Ошибка записи логов в файл: %s", err.Error()))
|
||||
} else {
|
||||
b.SendFile("log.txt", "📃 Лог запросов за последний час")
|
||||
return err
|
||||
}
|
||||
if err := b.SendFile("log.txt", "📃 Лог запросов за последний час"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create and send cache dump
|
||||
if err := WriteCacheDumpToFile(cache, "cache.json"); err != nil {
|
||||
b.SendMessage(fmt.Sprintf("🔴 Ошибка записи дампа кэша в файл: %s", err.Error()))
|
||||
} else {
|
||||
b.SendFile("cache.json", "🗄 Дамп кэша")
|
||||
return err
|
||||
}
|
||||
if err := b.SendFile("cache.json", "🗄 Дамп кэша"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
|
@ -117,4 +127,6 @@ func (b *TGBot) Backup(dbpath string, log []string, cache *candycache.Cache) {
|
|||
}
|
||||
os.Remove("log.txt")
|
||||
os.Remove("cache.json")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue