/
telegram.go
39 lines (33 loc) · 959 Bytes
/
telegram.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
package channels
import (
"crypto/tls"
"errors"
"github.com/aavzz/daemon/log"
"github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/spf13/viper"
"net/http"
)
var bot *tgbotapi.BotAPI
// InitTelegram initializes telegram bot
func InitTelegram() {
var err error
c := &http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}
bot, err = tgbotapi.NewBotAPIWithClient(viper.GetString("telegram.token"), c)
if err != nil {
log.Error(err.Error())
} else {
log.Info("Telegram bot authorized on account " + bot.Self.UserName)
}
}
///////////////////////////////////////////////////////////////
// SendMessageTelegram sends message to the specified telegram group
func SendMessageTelegram(chatID int64, message string) error {
if bot != nil {
_, err := bot.Send(tgbotapi.NewMessage(chatID, message))
if err != nil {
return err
}
return nil
}
return errors.New("Bot is not connected")
}