-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.go
40 lines (36 loc) · 985 Bytes
/
notify.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
package rice
import (
"fmt"
"log"
"time"
"github.com/chirichan/rice/telegram"
"github.com/nikoksr/notify"
)
func NewTelegramBotNotifier(token, proxy string, chatID ...int64) (*notify.Notify, error) {
telegramService, err := NewTelegramService(token, proxy)
if err != nil {
return nil, err
}
telegramService.AddReceivers(chatID...)
notifier := notify.New()
notifier.UseServices(telegramService)
return notifier, nil
}
func NewTelegramService(token, proxy string) (*telegram.Telegram, error) {
var connAttempts = 10
var telegramService *telegram.Telegram
var err error
for connAttempts > 0 {
telegramService, err = telegram.NewWithHttpClient(token, newHttpClient(httpProxy(proxy)))
if err == nil {
break
}
log.Printf("telegram service: trying connect, left %d\n", connAttempts)
time.Sleep(1 * time.Second)
connAttempts--
}
if telegramService == nil {
return nil, fmt.Errorf("telegramService init fail: %v", err)
}
return telegramService, nil
}