Skip to content

Instantly share code, notes, and snippets.

@smartass08
Last active October 19, 2020 11:41
Show Gist options
  • Save smartass08/7906415342fd9feb341f90d1a22c073d to your computer and use it in GitHub Desktop.
Save smartass08/7906415342fd9feb341f90d1a22c073d to your computer and use it in GitHub Desktop.
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