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