Skip to content

Commit

Permalink
GIGA: add helper bot support
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Aug 5, 2022
1 parent 3dfdada commit 02e016c
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 59 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<img src="./logo.png">
<img src="./assets/logo.png">
<b>A Powerful Telegram Userbot</b>
<br>
<a href="https://telegram.me/GIGAupdates"><img src="https://img.shields.io/badge/Updates%20Channel-blue?logo=telegram"></a>
Expand Down
Binary file added assets/giga.webp
Binary file not shown.
File renamed without changes
53 changes: 53 additions & 0 deletions bot/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package bot

import (
"net/http"
"time"

"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/PaulSonOfLars/gotgbot/v2/ext/handlers"
"github.com/anonyindian/logger"
)

func MakeBot(token string) (*gotgbot.Bot, error) {
return gotgbot.NewBot(token, &gotgbot.BotOpts{
Client: http.Client{},
DefaultRequestOpts: &gotgbot.RequestOpts{
Timeout: gotgbot.DefaultTimeout,
APIURL: gotgbot.DefaultAPIURL,
},
})
}

func StartClient(l *logger.Logger, b *gotgbot.Bot) {
log := l.Create("BOT")
// custom dispatcher handles all the updates
updater := ext.NewUpdater(&ext.UpdaterOpts{
ErrorLog: nil,
DispatcherOpts: ext.DispatcherOpts{
// If an error is returned by a handler, log it and continue going.
Error: func(b *gotgbot.Bot, ctx *ext.Context, err error) ext.DispatcherAction {
log.Println("an error occurred while handling update:", err.Error())
return ext.DispatcherActionNoop
},
MaxRoutines: ext.DefaultMaxRoutines,
},
})
dispatcher := updater.Dispatcher
dispatcher.AddHandler(handlers.NewCommand("start", func(b *gotgbot.Bot, ctx *ext.Context) error {
ctx.EffectiveMessage.Reply(b, "Started", nil)
return ext.EndGroups
}))

updater.StartPolling(b, &ext.PollingOpts{
DropPendingUpdates: true,
GetUpdatesOpts: gotgbot.GetUpdatesOpts{
Timeout: 9,
RequestOpts: &gotgbot.RequestOpts{
Timeout: time.Second * 10,
},
},
})
log.Println("STARTED")
}
5 changes: 3 additions & 2 deletions db/afk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ func UpdateAFK(toggle bool, reason string) {
set("afk", &Afk{Toggle: toggle, Reason: reason})
}

func GetAFK() (a *Afk) {
func GetAFK() *Afk {
var a = &Afk{}
get("afk", a)
return
return a
}
3 changes: 2 additions & 1 deletion db/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func UpdateBot(token string) {
set("settings", s)
}

func GetSettings() (s *Settings) {
func GetSettings() *Settings {
var s = &Settings{}
get("settings", s)
return s
}
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ module github.com/gigauserbot/giga
go 1.18

require (
github.com/anonyindian/gotgproto v1.0.0-beta08.0.20220804084457-c6884b6c2bb3
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.9
github.com/anonyindian/gotgproto v1.0.0-beta08.0.20220804181550-bbe7c2946407
github.com/anonyindian/logger v1.0.0-alpha.3
github.com/go-redis/redis v6.15.9+incompatible
github.com/gotd/td v0.63.0
github.com/gotd/td v0.64.0
github.com/joho/godotenv v1.4.0
)

Expand All @@ -28,8 +29,8 @@ require (
github.com/onsi/gomega v1.20.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/segmentio/asm v1.2.0 // indirect
go.opentelemetry.io/otel v1.8.0 // indirect
go.opentelemetry.io/otel/trace v1.8.0 // indirect
go.opentelemetry.io/otel v1.9.0 // indirect
go.opentelemetry.io/otel/trace v1.9.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.21.0 // indirect
Expand Down
18 changes: 10 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.9 h1:0pEtHIqipAj62qnGjg+sFRNw8+iR9LXP1cmF2YLZ+Ac=
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.9/go.mod h1:r815fYWTudnU9JhtsJAxUtuV7QrSgKpChJkfTSMFpfg=
github.com/allegro/bigcache v1.2.1 h1:hg1sY1raCwic3Vnsvje6TT7/pnZba83LeFck5NrFKSc=
github.com/allegro/bigcache v1.2.1/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
github.com/anonyindian/gotgproto v1.0.0-beta08.0.20220804084457-c6884b6c2bb3 h1:yRun92u1eX+XpdSblM3il31j2E+HvDxgv12mPynGxhM=
github.com/anonyindian/gotgproto v1.0.0-beta08.0.20220804084457-c6884b6c2bb3/go.mod h1:nKKR08rd8fUSTvHWEy5+sfNNIbL+aUXqhFTGxU8BoIY=
github.com/anonyindian/gotgproto v1.0.0-beta08.0.20220804181550-bbe7c2946407 h1:G7Naz/5/kEgPVFHrP9eemrQatMB8gNGnhpaFx9UqKrM=
github.com/anonyindian/gotgproto v1.0.0-beta08.0.20220804181550-bbe7c2946407/go.mod h1:nKKR08rd8fUSTvHWEy5+sfNNIbL+aUXqhFTGxU8BoIY=
github.com/anonyindian/logger v1.0.0-alpha.3 h1:fYQFvadiiMI7hxW3VZNJXFzEWgmz/wgRB9tVey3oLDo=
github.com/anonyindian/logger v1.0.0-alpha.3/go.mod h1:jb51uiKdlLOMtKakbyqsbrxTNr+Zt7qh2Va8sl5trTk=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
Expand Down Expand Up @@ -64,8 +66,8 @@ github.com/gotd/ige v0.2.2 h1:XQ9dJZwBfDnOGSTxKXBGP4gMud3Qku2ekScRjDWWfEk=
github.com/gotd/ige v0.2.2/go.mod h1:tuCRb+Y5Y3eNTo3ypIfNpQ4MFjrnONiL2jN2AKZXmb0=
github.com/gotd/neo v0.1.5 h1:oj0iQfMbGClP8xI59x7fE/uHoTJD7NZH9oV1WNuPukQ=
github.com/gotd/neo v0.1.5/go.mod h1:9A2a4bn9zL6FADufBdt7tZt+WMhvZoc5gWXihOPoiBQ=
github.com/gotd/td v0.63.0 h1:/wC2/cIfrfkQ1nh9DNhJlpM6OdeEtjcmtlR9wcZggXo=
github.com/gotd/td v0.63.0/go.mod h1:rMIw/4L6aXL/6iva2FZgC1/JMLq6+6toFA950Qi2MEw=
github.com/gotd/td v0.64.0 h1:nbe7PHX3ZJps+cj6mEiqaA86jMgZTLTP0w2LSlnGH14=
github.com/gotd/td v0.64.0/go.mod h1:uWa8zasSkTrNlmTu7CJCq29yH8fBnF4CsfJdFOC5rdw=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
Expand Down Expand Up @@ -123,10 +125,10 @@ github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.opentelemetry.io/otel v1.8.0 h1:zcvBFizPbpa1q7FehvFiHbQwGzmPILebO0tyqIR5Djg=
go.opentelemetry.io/otel v1.8.0/go.mod h1:2pkj+iMj0o03Y+cW6/m8Y4WkRdYN3AvCXCnzRMp9yvM=
go.opentelemetry.io/otel/trace v1.8.0 h1:cSy0DF9eGI5WIfNwZ1q2iUyGj00tGzP24dE1lOlHrfY=
go.opentelemetry.io/otel/trace v1.8.0/go.mod h1:0Bt3PXY8w+3pheS3hQUt+wow8b1ojPaTBoTCh2zIFI4=
go.opentelemetry.io/otel v1.9.0 h1:8WZNQFIB2a71LnANS9JeyidJKKGOOremcUtb/OtHISw=
go.opentelemetry.io/otel v1.9.0/go.mod h1:np4EoPGzoPs3O67xUVNoPPcmSvsfOxNlNA4F4AC+0Eo=
go.opentelemetry.io/otel/trace v1.9.0 h1:oZaCNJUjWcg60VXWee8lJKlqhPbXAPB51URuR47pQYc=
go.opentelemetry.io/otel/trace v1.9.0/go.mod h1:2737Q0MuG8q1uILYm2YYVkAyLtOofiTNGg6VODnOiPo=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
Expand Down
31 changes: 2 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package main

import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"os"

"github.com/anonyindian/gotgproto"
Expand Down Expand Up @@ -36,34 +33,11 @@ func main() {
runClient(l)
}

const BOT_FATHER_ID = 93372553

func runClient(l *logger.Logger) {
log := l.Create("CLIENT")
// custom dispatcher handles all the updates
dp := dispatcher.MakeDispatcher()
dp.AddHandlerToGroup(handlers.NewMessage(filters.Message.Text, func(ctx *ext.Context, u *ext.Update) error {
chat := u.EffectiveChat()
if chat.GetID() != BOT_FATHER_ID {
return nil
}
if !utils.TOKEN_REGEXP.MatchString(u.EffectiveMessage.Message) {
return nil
}
token := utils.TOKEN_REGEXP.FindString(u.EffectiveMessage.Message)
// let it panic if err arise
resp, _ := http.Get("api.telegram.org/bot%s/getMe")
b, _ := ioutil.ReadAll(resp.Body)
getme := struct {
UserId int64 `json:"user_id"`
}{}
if json.Unmarshal(b, &getme) != nil {
return nil
}
db.UpdateBot(token)
utils.BotSaved = true
return nil
}), -69)
dp.AddHandlerToGroup(handlers.NewMessage(filters.Message.Text, utils.GetBotToken(l)), 2)
gotgproto.StartClient(&gotgproto.ClientHelper{
// Get AppID from https://my.telegram.org/apps
AppID: config.ValueOf.AppId,
Expand All @@ -86,8 +60,7 @@ func runClient(l *logger.Logger) {
}
ctx := ext.NewContext(ctx, client.API(), gotgproto.Self, gotgproto.Sender, &tg.Entities{})
utils.TelegramClient = client

utils.StartupAutomations(ctx, client)
utils.StartupAutomations(l, ctx, client)
// Modules shall not be loaded unless the setup is complete
modules.Load(l, dp)
l.Println("GIGA HAS BEEN STARTED")
Expand Down
3 changes: 1 addition & 2 deletions modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package modules
import (
"reflect"

"github.com/anonyindian/gotgproto"
"github.com/anonyindian/gotgproto/dispatcher"
"github.com/anonyindian/gotgproto/dispatcher/handlers"
"github.com/anonyindian/gotgproto/ext"
Expand All @@ -26,7 +25,7 @@ func Load(l *logger.Logger, dispatcher *dispatcher.CustomDispatcher) {

func authorised(cback handlers.CallbackResponse) handlers.CallbackResponse {
return func(ctx *ext.Context, u *ext.Update) error {
if u.EffectiveUser() != nil && u.EffectiveUser().ID == gotgproto.Self.ID {
if u.EffectiveMessage.Out {
return cback(ctx, u)
}
return dispatcher.EndGroups
Expand Down
112 changes: 100 additions & 12 deletions utils/startup.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,64 @@ import (
"github.com/anonyindian/gotgproto/ext"
"github.com/anonyindian/gotgproto/storage"
"github.com/anonyindian/gotgproto/types"
"github.com/anonyindian/logger"
"github.com/gigauserbot/giga/bot"
"github.com/gigauserbot/giga/db"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/uploader"
"github.com/gotd/td/tg"
)

var (
TelegramClient *telegram.Client
BotSaved = false
botSaved = false
BotFatherId int64 = 93372553
)

// StartupAutomations includes the stuff to be done on each startup
func StartupAutomations(ctx *ext.Context, client *telegram.Client) {
func StartupAutomations(l *logger.Logger, ctx *ext.Context, client *telegram.Client) {
if group := setupLogsGroup(ctx, client); group != 0 {
_, err := ctx.SendMessage(group, &tg.MessagesSendMessageRequest{
Message: "Your GIGA is alive!",
Message: "Your GIGA is alive!",
ReplyToMsgID: trySendingFile(ctx, group),
})
if err != nil {
// check err in string because unwrapping didn't work
if strings.Contains(err.Error(), "PEER_ID_INVALID") {
db.UpdateLogs(0)
StartupAutomations(ctx, client)
StartupAutomations(l, ctx, client)
return
}
}
}
_ = setupBot
// setupBot(ctx, client, nil)
if db.GetSettings().Token == "" {
uname := setupBot(ctx, client, nil)
if uname == "BOT_NOT_CREATED" {
fmt.Println("failed to create bot")
return
}
u, _ := ctx.ResolveUsername(uname)
ctx.SendMessage(u.GetID(), &tg.MessagesSendMessageRequest{
Message: "/start",
})
} else {
b, err := bot.MakeBot(db.GetSettings().Token)
if err != nil {
return
}
bot.StartClient(l, b)
}
}

var TOKEN_REGEXP = regexp.MustCompile(`(\d+:[a-zA-Z0-9_\-]+)`)

func setupBot(ctx *ext.Context, client *telegram.Client, u types.EffectiveChat) int64 {
func setupBot(ctx *ext.Context, client *telegram.Client, u types.EffectiveChat) string {
if u == nil {
u, _ = ctx.ResolveUsername("botfather")
}
BotFatherId = u.GetID()
_, err := ctx.SendMessage(u.GetID(), &tg.MessagesSendMessageRequest{
Message: "/newbot",
Message: "/cancel",
})
if err != nil && strings.Contains(err.Error(), "YOU_BLOCKED_USER") {
if ok, _ := ctx.Client.ContactsUnblock(ctx, &tg.InputPeerUser{
Expand All @@ -57,19 +78,37 @@ func setupBot(ctx *ext.Context, client *telegram.Client, u types.EffectiveChat)

}
}
time.Sleep(time.Second)
ctx.SendMessage(u.GetID(), &tg.MessagesSendMessageRequest{
Message: "/newbot",
})
time.Sleep(time.Second * 1)
ctx.SendMessage(u.GetID(), &tg.MessagesSendMessageRequest{
Message: "GIGA Helper Bot",
})
time.Sleep(time.Second * 1)
uname := fmt.Sprintf("@GIGA_%s%dbot", string(gotgproto.Self.FirstName[0]), time.Now().Unix())
ctx.SendMessage(u.GetID(), &tg.MessagesSendMessageRequest{
Message: fmt.Sprintf("@GIGA_%s%dbot", string(gotgproto.Self.FirstName[0]), time.Now().Unix()),
Message: uname,
})
for !BotSaved {
for i := 1; !botSaved; i++ {
time.Sleep(time.Second * 1)
if i >= 5 {
return "BOT_NOT_CREATED"
}
}

return 0
ctx.SendMessage(u.GetID(), &tg.MessagesSendMessageRequest{
Message: "/setinline",
})
time.Sleep(time.Second * 1)
ctx.SendMessage(u.GetID(), &tg.MessagesSendMessageRequest{
Message: uname,
})
time.Sleep(time.Second * 1)
ctx.SendMessage(u.GetID(), &tg.MessagesSendMessageRequest{
Message: "giga query...",
})
return uname
}

func setupLogsGroup(ctx *ext.Context, client *telegram.Client) int64 {
Expand All @@ -94,3 +133,52 @@ func setupLogsGroup(ctx *ext.Context, client *telegram.Client) int64 {
db.UpdateLogs(group)
return group
}

func GetBotToken(l *logger.Logger) func(ctx *ext.Context, u *ext.Update) error {
return func(ctx *ext.Context, u *ext.Update) error {
if botSaved {
return nil
}
chat := u.EffectiveChat()
if chat.GetID() != BotFatherId {
return nil
}
if !TOKEN_REGEXP.MatchString(u.EffectiveMessage.Message) {
return nil
}
token := TOKEN_REGEXP.FindString(u.EffectiveMessage.Message)
b, err := bot.MakeBot(token)
if err != nil {
uname := setupBot(ctx, TelegramClient, nil)
if uname == "BOT_NOT_CREATED" {
return nil
}
u, _ := ctx.ResolveUsername(uname)
ctx.SendMessage(u.GetID(), &tg.MessagesSendMessageRequest{
Message: "/start",
})
}
bot.StartClient(l, b)
db.UpdateBot(token)
botSaved = true
return nil
}
}

func trySendingFile(ctx *ext.Context, chatId int64) int {
upload := uploader.NewUploader(ctx.Client)
f, err := upload.FromPath(ctx, "assets/giga.webp")
if err != nil {
return 0
}
m, err := ctx.SendMedia(chatId, &tg.MessagesSendMediaRequest{
Media: &tg.InputMediaUploadedDocument{
File: f,
MimeType: "image/webp",
},
})
if err != nil {
return 0
}
return m.ID
}

0 comments on commit 02e016c

Please sign in to comment.