-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/Abishnoi69/Force-Sub-Bot
- Loading branch information
Showing
8 changed files
with
524 additions
and
13 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: build | ||
|
||
on: [push,pull_request] | ||
|
||
jobs: | ||
|
||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: '1.22.1' | ||
|
||
- name: Build | ||
run: go build -v ./... |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package dispatcher | ||
|
||
import ( | ||
"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/modules" | ||
"github.com/PaulSonOfLars/gotgbot/v2/ext" | ||
) | ||
|
||
var Dispatcher = NewDispatcher() | ||
|
||
// NewDispatcher creates a new dispatcher and loads modules. | ||
func NewDispatcher() *ext.Dispatcher { | ||
dispatcher := ext.NewDispatcher(nil) | ||
modules.LoadModules(dispatcher) | ||
|
||
return dispatcher | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/config" | ||
"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/dispatcher" | ||
"github.com/PaulSonOfLars/gotgbot/v2" | ||
) | ||
|
||
var ( | ||
allowedTokens = strings.Split(os.Getenv("BOT_TOKEN"), " ") | ||
lenAllowedTokens = len(allowedTokens) | ||
) | ||
|
||
const ( | ||
statusCodeSuccess = 200 | ||
) | ||
|
||
// Bot Handles all incoming traffic from webhooks. | ||
func Bot(w http.ResponseWriter, r *http.Request) { | ||
url := r.URL.Path | ||
|
||
split := strings.Split(url, "/") | ||
if len(split) < 2 { | ||
fmt.Println(w, "url path too short") | ||
w.WriteHeader(statusCodeSuccess) | ||
|
||
return | ||
} | ||
|
||
botToken := split[len(split)-2] | ||
|
||
bot, _ := gotgbot.NewBot(botToken, &gotgbot.BotOpts{DisableTokenCheck: true}) | ||
|
||
// Delete the webhook in case token is unauthorized. | ||
if lenAllowedTokens > 0 && allowedTokens[0] != "" && !config.FindInStringSlice(allowedTokens, botToken) { | ||
_, _ = bot.DeleteWebhook(&gotgbot.DeleteWebhookOpts{}) // It doesn't matter if it errors | ||
w.WriteHeader(statusCodeSuccess) | ||
return | ||
} | ||
|
||
var update gotgbot.Update | ||
|
||
body, err := io.ReadAll(r.Body) | ||
if err != nil { | ||
_, err = fmt.Fprintf(w, "Error reading request body: %v", err) | ||
w.WriteHeader(statusCodeSuccess) | ||
return | ||
} | ||
|
||
err = json.Unmarshal(body, &update) | ||
if err != nil { | ||
fmt.Println("failed to unmarshal body ", err) | ||
w.WriteHeader(statusCodeSuccess) | ||
|
||
return | ||
} | ||
|
||
bot.Username = split[len(split)-1] | ||
|
||
err = dispatcher.Dispatcher.ProcessUpdate(bot, &update, map[string]any{}) | ||
if err != nil { | ||
fmt.Printf("error while processing update: %v", err) | ||
} | ||
|
||
w.WriteHeader(statusCodeSuccess) | ||
} |
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
Oops, something went wrong.