Spaces:
Sleeping
Sleeping
File size: 1,422 Bytes
891b1b2 | 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | package api
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path"
"strings"
"github.com/Jisin0/Go-Filter-Bot/plugins"
"github.com/Jisin0/Go-Filter-Bot/utils"
"github.com/PaulSonOfLars/gotgbot/v2"
)
var (
allowedTokens = strings.Split(os.Getenv("BOT_TOKEN"), " ")
lenAllowedTokens = len(allowedTokens)
)
const (
statusCodeSuccess = 200
)
// Handles all incoming traffic from webhooks.
func Bot(w http.ResponseWriter, r *http.Request) {
url := r.URL.Path
_, botToken := path.Split(url)
bot, _ := gotgbot.NewBot(botToken, &gotgbot.BotOpts{DisableTokenCheck: true})
// Delete the webhook incase token is unauthorized.
if lenAllowedTokens > 0 && allowedTokens[0] != "" && !utils.Contains(allowedTokens, botToken) {
bot.DeleteWebhook(&gotgbot.DeleteWebhookOpts{}) //nolint:errcheck // It doesn't matter if it errors
w.WriteHeader(statusCodeSuccess)
return
}
var update gotgbot.Update
body, err := io.ReadAll(r.Body)
if err != nil {
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
}
err = plugins.Dispatcher.ProcessUpdate(bot, &update, map[string]interface{}{})
if err != nil {
fmt.Printf("error while processing update: %v", err)
}
w.WriteHeader(statusCodeSuccess)
}
|