jlelse
/
kis3
Archived
1
Fork 0

Add reports via Telegram

This commit is contained in:
Jan-Lukas Else 2019-05-26 09:12:30 +02:00
parent 022bbebe48
commit 86152eafb9
5 changed files with 66 additions and 14 deletions

View File

@ -80,9 +80,9 @@ The following filters are available:
`format`: the format to represent the data, default is `plain` for a simple plain text list, `json` for a JSON response or `chart` for a chart generated with ChartJS in the browser
## Daily email reports
## Daily reports
KISSS has a feature that can send you daily email reports. It basically requests the statistics and sends the response via email. You can configure it by adding report configurations to the configuration file:
KISSS has a feature that can send you daily reports. It basically requests the statistics and sends the response via your preferred communication channel (mail or Telegram). You can configure it by adding report configurations to the configuration file:
```json
{
@ -98,6 +98,13 @@ KISSS has a feature that can send you daily email reports. It basically requests
"smtpUser": "myemailaddress@mydomain.tld",
"smtpPassword": "mysecretpassword"
},
{
"name": "Daily stats from KISSS",
"time": "15:00",
"query": "view=pages&ordercol=second&order=desc",
"tgBotToken": "TelegramBotToken",
"tgUserId": 123456
},
{
// Additional reports...
}
@ -105,6 +112,8 @@ KISSS has a feature that can send you daily email reports. It basically requests
}
```
To use Telegram for reports, create a bot with the [Bot Father](https://t.me/BotFather) and request your user id from [@userinfobot](https://t.me/userinfobot).
## License
KISSS is licensed under the MIT license, so you can do basically everything with it, but nevertheless, please contribute your improvements to make KISSS better for everyone. See the LICENSE.txt file.

View File

@ -17,17 +17,6 @@ type config struct {
Reports []report `json:"reports"`
}
type report struct {
Name string `json:"name"`
Time string `json:"time"`
To string `json:"to"`
SmtpUser string `json:"smtpUser"`
SmtpPassword string `json:"smtpPassword"`
SmtpHost string `json:"smtpHost"`
From string `json:"from"`
Query string `json:"query"`
}
var (
appConfig = &config{
Port: "8080",

2
go.mod
View File

@ -4,6 +4,7 @@ go 1.12
require (
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/gobuffalo/packr v1.25.0 // indirect
github.com/gobuffalo/packr/v2 v2.2.0
github.com/google/uuid v1.1.1 // indirect
@ -14,6 +15,7 @@ require (
github.com/mattn/go-sqlite3 v1.10.1-0.20190510164106-2a192bf78237
github.com/mssola/user_agent v0.5.0
github.com/rubenv/sql-migrate v0.0.0-20190327083759-54bad0a9b051
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
github.com/whiteshtef/clockwork v0.0.0-20190417075149-ecf7d9abe8ec
github.com/ziutek/mymysql v1.5.4 // indirect
google.golang.org/appengine v1.6.0 // indirect

4
go.sum
View File

@ -1,6 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0=
github.com/gobuffalo/depgen v0.0.0-20190329151759-d478694a28d3/go.mod h1:3STtPUQYuzV0gBVOY3vy6CfMm/ljR4pABfrTeHNLHUY=
github.com/gobuffalo/envy v1.6.15/go.mod h1:n7DRkBerg/aorDM8kbduw5dN3oXGswK5liaSCx4T5NI=
@ -59,6 +61,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
github.com/whiteshtef/clockwork v0.0.0-20190417075149-ecf7d9abe8ec h1:4mCJZnO75zjolpdsj/ToKe7X1oLWm+JJHwS1ez8BkXY=
github.com/whiteshtef/clockwork v0.0.0-20190417075149-ecf7d9abe8ec/go.mod h1:6o8H8sci2q3QxZ4p/U88ggqZuhY3mg34+WE5BuazLsU=
github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0=

View File

@ -2,6 +2,7 @@ package main
import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/jordan-wright/email"
"github.com/whiteshtef/clockwork"
"io/ioutil"
@ -10,6 +11,20 @@ import (
"net/smtp"
)
type report struct {
Name string `json:"name"`
Time string `json:"time"`
Query string `json:"query"`
Type string `json:"type"`
To string `json:"to"`
From string `json:"from"`
SmtpUser string `json:"smtpUser"`
SmtpPassword string `json:"smtpPassword"`
SmtpHost string `json:"smtpHost"`
TGBotToken string `json:"tgBotToken"`
TGUserId int64 `json:"tgUserId"`
}
func setupReports() {
scheduler := clockwork.NewScheduler()
for _, r := range appConfig.Reports {
@ -39,10 +54,23 @@ func executeReport(r *report) {
fmt.Println("Executing report failed:", e)
return
}
sendMail(r, body)
sendReport(r, body)
}
func sendReport(r *report, content []byte) {
switch r.Type {
case "telegram":
sendTelegram(r, content)
default:
sendMail(r, content)
}
}
func sendMail(r *report, content []byte) {
if r.To == "" || r.From == "" || r.SmtpUser == "" || r.SmtpHost == "" {
fmt.Println("No valid report configuration")
return
}
smtpHostNoPort, _, _ := net.SplitHostPort(r.SmtpHost)
mail := email.NewEmail()
mail.From = r.From
@ -57,3 +85,23 @@ func sendMail(r *report, content []byte) {
fmt.Println("Report sent")
}
}
func sendTelegram(r *report, content []byte) {
if r.TGUserId == 0 || r.TGBotToken == "" {
fmt.Println("No valid report configuration")
return
}
bot, e := tgbotapi.NewBotAPI(r.TGBotToken)
if e != nil {
fmt.Println("Sending report failed:", e)
return
}
msg := tgbotapi.NewMessage(r.TGUserId, r.Name+"\n\n"+string(content))
_, e = bot.Send(msg)
if e != nil {
fmt.Println("Sending report failed:", e)
return
} else {
fmt.Println("Report sent")
}
}