Last active
October 19, 2020 11:41
-
-
Save smartass08/7906415342fd9feb341f90d1a22c073d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package modules | |
import ( | |
"fmt" | |
"github.com/PaulSonOfLars/gotgbot" | |
"github.com/PaulSonOfLars/gotgbot/ext" | |
"go.uber.org/zap" | |
"go.uber.org/zap/zapcore" | |
"html" | |
"log" | |
"math/rand" | |
"net/http" | |
"os" | |
"strings" | |
"time" | |
) | |
type TG struct { | |
client *gotgbot.Updater | |
} | |
func (u *TG) Initialise() { | |
cfg := zap.NewProductionEncoderConfig() | |
cfg.EncodeLevel = zapcore.CapitalLevelEncoder | |
cfg.EncodeTime = zapcore.RFC3339TimeEncoder | |
logger := zap.New(zapcore.NewCore(zapcore.NewConsoleEncoder(cfg), os.Stdout, zap.InfoLevel)) | |
l := logger.Sugar() | |
updater, err := gotgbot.NewUpdater(logger, "bot_token") | |
l.Info("Got Updater") | |
updater.UpdateGetter = ext.BaseRequester{ | |
Client: http.Client{ | |
Transport: nil, | |
CheckRedirect: nil, | |
Jar: nil, | |
Timeout: time.Second * 65, | |
}, | |
ApiUrl: ext.ApiUrl, | |
} | |
updater.Bot.Requester = ext.BaseRequester{Client: http.Client{Timeout: time.Second * 65}} | |
if err != nil { | |
l.Fatalw("failed to start updater", zap.Error(err)) | |
} | |
u.client = updater | |
} | |
func (u *TG) Send(data map[string]string, retry bool) { | |
if retry { | |
rand.Seed(time.Now().UnixNano()) | |
random := rand.Intn(60 - 30 ) + 30 | |
log.Println("Sleeping for : ", random) | |
time.Sleep(time.Duration(random) * time.Second) | |
} | |
message := "" | |
send := u.client.Bot.NewSendableMessage(-123456789, message) | |
send.ParseMode = "HTML" | |
send.DisableWebPreview = true | |
_, err := send.Send() | |
if err != nil { | |
log.Printf("%v\n", err) | |
if strings.Contains(err.Error(), "Too Many Requests"){ | |
u.Send(data, true) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment