-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (60 loc) · 1.68 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"log"
"net/http"
"os"
"github.com/robfig/cron/v3"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func run_metrics_server() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
}
var (
messagesReceived = promauto.NewCounter(prometheus.CounterOpts{
Name: "telegram_messages_received_total",
Help: "The total number of received messages from telegram polling",
})
messagesSent = promauto.NewCounter(prometheus.CounterOpts{
Name: "telegram_messages_sent_total",
Help: "The total number of messages sent from bots",
})
)
const juChatId = 1
func main() {
go run_metrics_server()
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
if err != nil {
log.Panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
c := cron.New()
c.AddFunc("@daily", func() {
msg := tgbotapi.NewMessage(juChatId, "Mensagem do Caio. Teste")
sendMessage(bot, msg)
})
c.Start()
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message != nil {
messagesReceived.Inc()
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "miau")
msg.ReplyToMessageID = update.Message.MessageID
sendMessage(bot, msg)
}
}
}
func sendMessage(bot *tgbotapi.BotAPI, msg tgbotapi.MessageConfig) error {
_, err := bot.Send(msg)
if err == nil {
messagesSent.Inc()
}
return nil
}