Go-Filter-Bot / api /bot.go
Thaimozhi's picture
first commit
891b1b2
Raw History Blame
1.42 kB
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)
}